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

Performing fast comparisons between data types


The StableName package allows us to establish constant time comparisons of arbitrary data types. The Hackage documentation elegantly describes this (http://hackage.haskell.org/package/base-4.7.0.0/docs/System-Mem-StableName.html):

"Stable names solve the following problem: suppose you want to build a hash table with Haskell objects as keys, but you want to use pointer equality for comparison; maybe because the keys are large and hashing would be slow, or perhaps because the keys are infinite in size. We can't build a hash table using the address of the object as the key, because objects get moved around by the garbage collector, meaning a re-hash would be necessary after every garbage collection."

How to do it…

  1. Import the built-in StableName package as follows:

    import System.Mem.StableName
  2. Create a custom data type as follows:

    data Point = Point [Int]
  3. In main, define two points as follows:

      main = do
        let p1 = Point [1..]
        let p2 = Point [2,4...