[Haskell] more newbie questions regarding do syntax, mondic context & Data.HashTable

Hunter Kelly retnuh at gmail.com
Thu Dec 29 09:06:01 EST 2005


Hi there, I'm having some trouble trying to get information out
of a HashTable.

I'm using it to represent a set (I'm using a hash table for O(1)
speed reasons to compare against Data.Sets O(lgN) operations).

I'm trying to write a simple function, member, which returns whether
or not a key is in the HashTable.

I've tried the following:

member :: HashTable String Int -> String -> Bool
member hash x = let r = do return (Data.HashTable.lookup hash x )
                          in case r of
                               Nothing -> False
                               (Just v) -> True

But this seems to always return True.  I _think_ the type of r here is
IO (Maybe Int), but I'm trying to unwrap the IO part.

However, if I try:

member :: HashTable String Int -> String -> Bool
member hash x = let r = do Data.HashTable.lookup hash x
                           in case r of
                               Nothing -> False
                               (Just v) -> True

I get the following error:


figt.hs:46:31:
    Couldn't match `IO (Maybe Int)' against `Maybe a'
      Expected type: IO (Maybe Int)
      Inferred type: Maybe a
    When checking the pattern: Nothing
    In a case alternative: Nothing -> False



How can I get at the underlying value?  Can I only access it from
within a "do" construct?
Is there anyway to get at this function to return just True or False? 
Or has using something that uses an IO monad "polluted" everything
else that depends on the answer?

H


More information about the Haskell mailing list