[Haskell-cafe] Simple hash table creation
michael rice
nowgate at yahoo.com
Tue Nov 17 14:54:33 EST 2009
Thanks all,
Got it! type rather than data and <- rather than = (should have remembered this from monad stuff). Also, don't need the qualification.
Onward and upward.
Thanks,
Michael
--- 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:36 PM
Am Dienstag 17 November 2009 20:16:50 schrieb michael rice:
> I'm trying to create a hash table. Yeah, I know, don't use hash tables, but
> I need to create something I'm familiar with, not something I've never
> worked with before. What's wrong with this code?
>
> Michael
>
> ====================
>
> import Prelude hiding (lookup)
> import Data.HashTable
>
> data MyHashTable = HashTable String Int
>
> dummy:: String -> Int
> dummy s = 7
>
> ht = MyHashTable.new (==) dummy
>
> ====================
MyHashTable.new is parsed as a qualified function, 'new' from the module MyHashTable. But
there's no module MyHashTable imported, hence there's no function 'new' from that module
in scope.
>
> [michael at localhost ~]$ ghci hash1
> GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer ... linking ... done.
> Loading package base ... linking ... done.
> [1 of 1] Compiling Main ( hash1.hs, interpreted )
>
> hash1.hs:9:5: Not in scope: `MyHashTable.new'
> Failed, modules loaded: none.
> Prelude>
If we look at the type of Data.HashTable.new:
new :: (key -> key -> Bool) -> (key -> GHC.Int.Int32) -> IO (HashTable key val)
we see that
new (==) dummy
(or Data.HashTable.new (==) dummy, but we don't need to qualify new)
has type
IO (HashTable String val), so is an IO-action returning a hashtable.
What you probably wanted was
type MyHashTable = HashTable String Int -- not data MyHashTable
ht <- new (==) dummy :: IO MyHashTable
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091117/718dd4ba/attachment.html
More information about the Haskell-Cafe
mailing list