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.
Use the GHC language extension
DeriveGeneric
to autodefine the hash functions for our custom data types as follows:{-# LANGUAGE DeriveGeneric #-}
Import the relevant packages using the following lines of code:
import GHC.Generics (Generic) import Data.Hashable
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
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...