[Haskell-cafe] Simple hash table creation

michael rice nowgate at yahoo.com
Tue Nov 17 16:00:58 EST 2009


Hi Gregory,



I was wondering about that, because of the following:



[1 of 1] Compiling Main             ( hash1.hs, interpreted )

Ok, modules loaded: Main.

*Main> ht <- new (==) dummy :: IO MyHashTable

*Main> dummy "mike"

7

*Main> dummy "michael"

7

*Main> insert ht "mike" 1

*Main> toList ht

[("mike",1)]

*Main> insert ht "michael" 2

*Main> toList ht

[("michael",2),("mike",1)]

*Main> insert ht "miguel" 3

*Main> toList ht

[("miguel",3),("michael",2),("mike",1)]

*Main> :t dummy "miguel"

dummy "miguel" :: Int32

*Main> 



It seems my dummy function is being ignored. I figured I would only be able to store a single value with a hash function that always returns 7. Why ask for a hash function and not use it?



Also, it's said that it is good programming practice  to include type
information in function definitions, so I always try to do that, even
though it usually leads to my code being rejected. I need to step back
and use the :t  and module functions defs to figure out what types are
returned and required as arguments, instead of trying to puzzle it out
myself.

Like juggling, there's a lot of balls in the air w/Haskell, lots of things to remember, but it's the most intriguing computer language I've looked at in a long time.



Thanks for your input.



Michael





--- On Tue, 11/17/09, Gregory Crosswhite <gcross at phys.washington.edu> wrote:

From: Gregory Crosswhite <gcross at phys.washington.edu>
Subject: Re: [Haskell-cafe] Simple hash table creation
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org, "Daniel Fischer" <daniel.is.fischer at web.de>
Date: Tuesday, November 17, 2009, 3:30 PM

Look in Data.Int for a list of the Int types.
Basically you generally only use Int unless you are working at a lower-level which has an explicit requirement for the number of bits.  In this case, HashTable has such a requirement, so you have two choices:  you can either change the type annotation on dummy:

import Data.Int
dummy:: String -> Int32
dummy s = 7


or you can just leave it off entirely, and GHC will automatically infer the correct type (without you needing to import Data.Int):
dummy s = 7

On Nov 17, 2009, at 12:09 PM, michael rice wrote:
Hi Daniel,



Thanks for the IO monad reminder.



What is GHC.Int.Int32? Can't find it on Hoogle.



Michael



==================



*Main> ht <- new (==) dummy :: IO MyHashTable



<interactive>:1:15:

    Couldn't match expected type `GHC.Int.Int32'

           against inferred type `Int'

    In the second argument of `new', namely `dummy'

    In a stmt of a 'do' expression:

        ht <- new (==) dummy :: IO MyHashTable

*Main> :t dummy

dummy :: String -> Int

*Main> 




--- On Tue, 11/17/09, Daniel Fischer <daniel.is.fischer at web.de> wrote:

From: Daniel Fischer <daniel.is.fischer at web.de>
Subject: Re: [Haskell-cafe] Simple hash table creation
To: haskell-cafe at haskell.org
Date: Tuesday, November 17, 2009, 2:45 PM

Am Dienstag 17 November 2009 20:36:46 schrieb Daniel Fischer:
> What you probably wanted was
>
> type MyHashTable = HashTable String Int -- not data MyHashTable
>

Just in case it's not clear:

> ht <- new (==) dummy :: IO MyHashTable

only works at the prompt or in an IO do-block, not at the top level of the module.

>
> then ht is a hashtable of type MyHashTable.


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe





      _______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091117/19e33fad/attachment.html


More information about the Haskell-Cafe mailing list