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

Johan Tibell johan.tibell at gmail.com
Thu Dec 2 13:15:02 CET 2010


On Thu, Dec 2, 2010 at 1:04 PM, John Obbele <john.obbele at gmail.com> wrote:
> {{{
> 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 ?

You can test this by creating a Map that contains undefined values e.g.

{{{
lookupDocumentation (insert "b_somekey" undefined (singleton
"a_somekey" "somevalue")) "somekey"
}}}

Since "a_somekey" is checked before "b_somekey" and "a_somekey" is in
the map, this should work if the function is lazy enough.

Johan



More information about the Beginners mailing list