Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Hashing a custom data type


Even a custom-defined data type can be hashed easily. Dealing with hashed digests is often useful when the data itself is too space consuming to manage directly. By referencing a data by its digest, we can easily skip the cost of carrying around whole data types. This is especially useful in data analysis.

Getting ready

Install the Data.Hashable package from Cabal as follows:

$ cabal install hashable

How to do it…

  1. Use the GHC language extension DeriveGeneric to autodefine the hash functions for our custom data types as follows:

    {-# LANGUAGE DeriveGeneric #-}
  2. Import the relevant packages using the following lines of code:

    import GHC.Generics (Generic)
    import Data.Hashable
  3. Create a custom data type and let GHC autodefine its hashable instance as follows:

    data Point = Point Int Int
               deriving (Eq, Generic)
    
    instance Hashable Point
  4. In main, create three points. Let two of them be the same, and let the third point be different, as shown in the following code snippet...