[Haskell-beginners] Finding the first successful Map.lookup

John Obbele john.obbele at gmail.com
Thu Dec 2 13:04:17 CET 2010


Hi every one !

A long time ago, I got a problem about Map.lookup and lazyness (the
programmer's one). Since it's still bugging me, I'd like to ask for some
external advice.

Let's say I've build a big documentation index (:: Map Key URL)
where both Key and URL are Strings. The problem is that my keys
have some random prefixes (ie the key "foo" could be store either
as "foo", "a_foo", "b_foo", "g_foo", "gtk_foo" or "pango_foo").
How could I implement effectively in Haskell a function which
return me the first (and only the first!) correct URL ?

For now, I'm doing something like this:

{{{
type Key = String
type URL = String

lookupDocumentation :: Map Key URL -> Key -> Maybe URL
lookupDocumentation myMap key =
        let r = map (lookupSymbols myMap) [ key
                                          , "a_" ++ key
                                          , "b_" ++ key
                                          , "g_" ++ key
                                          , "gtk_" ++ key
                                          , "pango_" ++ key
                                          ]
        in case catMaybes r of
             []    -> Nothing
             (x:_) -> Just x
}}}

My problem is: will `lookupDocumentation` perform n=6 lookups in my Map,
or will Haskell-built-in-lazyness only evaluate till an URL is found ?

regards,
/John



More information about the Beginners mailing list