[Haskell-cafe] Re: Types and hashes of hashes, trouble for a Java-programmer...

Achim Schneider barsoap at web.de
Mon Apr 13 13:49:46 EDT 2009


John Smith <smithsnorth at gmail.com> wrote:

> Yes, I see. Good points in both answers, but I still would like to see
> how to do it with the mutable hash, if possible...
>

test = do
    h <- H.new (==) id
    h1 <- H.new (==) id
    H.insert h 3 h1
    H.insert h1 1 1000
    inner <- H.lookup h 3
    case inner of
        Nothing -> return Nothing
        Just outer -> H.lookup outer 1000

you forgot to lift the Nothing into the IO monad. I like this one:

lookup' :: key -> b -> HashTable key (HashTable b val) -> IO (Maybe val)
lookup' k l h = 
	H.lookup h k >>= maybe (return Nothing) ((flip H.lookup) l)


test' = do
    h <- H.new (==) id
    h1 <- H.new (==) id
    H.insert h 3 h1
    H.insert h1 1 1000
    lookup' 3 1000 h


-- 
(c) this sig last receiving data processing entity. Inspect headers
for copyright history. All rights reserved. Copying, hiring, renting,
performance and/or quoting of this signature prohibited.




More information about the Haskell-Cafe mailing list