[Haskell-cafe] beginners question about fromMaybe

Toby Hutton toby.hutton at gmail.com
Tue Jun 2 19:32:14 EDT 2009


On Wed, Jun 3, 2009 at 8:59 AM, Nico Rolle <nrolle at web.de> wrote:
> hi there
>
> heres a code snipped, don't care about the parameters.
> the thing is i make a lookup on my map "m" and then branch on that return value
>
> probePhase is sc [] m = []
> probePhase is sc (x:xs) m
>    | val == Nothing  = probePhase is sc xs m
>    | otherwise       = jr ++ probePhase is sc xs m
>        where
>            jr  = joinTuples sc x (fromMaybe [] val)
>            key = getPartialTuple is x
>            val = Map.lookup key m
>
>
> the line "jr  = joinTuples sc x (fromMaybe [] val)" is kind of ugly
> because i know that it is not Nothing.

Although pattern matching is probably nicer, there's also fromJust
which will throw an exception if you pass it Nothing.

I prefer:
case Map.lookup key m of
     Nothing -> next
     Just val -> (joinTuples sc x val) ++ next
  where next = probePhase ...
            key = ...


More information about the Haskell-Cafe mailing list