[Haskell-cafe] nested maybes

Udo Stenzel u.stenzel at web.de
Sun Feb 4 14:25:50 EST 2007


J. Garrett Morris wrote:
> 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

Small improvement (Data.Maybe is underappreciated):

> exists str wmap = isJust exists'
>    where exists' =
>              do x <- Map.lookup (sort str) wmap
>                 find (== str) (snd x)

and maybe another improvement, though this is dependent on your tastes:

> exists s wmap = isJust $ Map.lookup (sort s) wmap >>= find (== s) . snd


-Udo
-- 
Catproof is an oxymoron, childproof nearly so.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070204/dd6b255a/attachment.bin


More information about the Haskell-Cafe mailing list