[Haskell-cafe] nested maybes

J. Garrett Morris trevion at gmail.com
Sun Feb 4 10:40:37 EST 2007


Maybe has a Monad instance, so you can write this as follows (untested):

exists str wmap = boolFromMaybe exists'
    where exists' =
              do x <- Map.lookup (sort str) wmap
                 find (== str) (snd x)
          boolFromMaybe (Just _) = True
          boolFromMaybe Nothing  = False

 /g


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?
>
> martin
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


-- 
It is myself I have never met, whose face is pasted on the underside of my mind.


More information about the Haskell-Cafe mailing list