Hashing a primitive data type

This recipe demonstrates how to use a simple hash function on various primitive data types.

Getting ready

Install the Data.Hashable package from Cabal as follows:

$ cabal install hashable

How to do it…

  1. Import the hashing function with the following line:
    import Data.Hashable
  2. Test the hash function on a string as follows; this function is actually a wrapper around the hashWithSalt function with a default salt value:
    main = do
      print $ hash "foo" 
  3. Test out the hashWithSalt functions using different initial salt values as follows:
      print $ hashWithSalt 1 "foo"
      print $ hashWithSalt 2 "foo"
  4. We can also hash tuples and lists as follows:
      print $ hash [ (1 :: Int, "hello", True)
                   , (0 :: Int, "goodbye", False) ]
  5. Notice in the following ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.