[Haskell-cafe] nested maybes

Lemmih lemmih at gmail.com
Sun Feb 4 09:32:52 EST 2007


On 2/4/07, Martin DeMello <martindemello at gmail.com> wrote:
> I have a Data.Map.Map String -> (Layout, [String]) as follows:
>
> type Anagrams = [String]
> type Cell = (Layout, Anagrams)
> type WordMap = Map.Map String Cell
>
> exists str wmap =
>   let a = Map.lookup (sort str) wmap in
>       case a of
>            Nothing -> False
>            Just x -> case (find (== str) (snd x)) of
>                           Nothing -> False
>                           _       -> True
>
> the existence test looks ugly - any more compact way to write it?

How about:
exists str = fromMaybe False . fmap (elem str.snd) . Map.lookup (sort str)

-- 
Cheers,
  Lemmih


More information about the Haskell-Cafe mailing list