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 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 output how the first three hashes produce different results even though their input is the same:

    $ runhaskell Main.hs
    
    7207853227093559468
    367897294438771247
    687941543139326482
    6768682186886785615

How it works…

Hashing with a salt...