[Haskell-cafe] generating Maybe

Henning Thielemann lemming at henning-thielemann.de
Thu Nov 8 02:19:46 EST 2007


On Wed, 7 Nov 2007, Tim Newsham wrote:

> Data.Maybe has functions for processing Maybe's but nothing useful
> for creating maybe.  I think the following would be a very useful
> addition, a guarded function:
>
>      guarded :: (a -> Bool) -> (a -> b) -> a -> Maybe b
>      guarded p f x | p x       = Just (f x)
>                    | otherwise = Nothing
>
> such a function in the std libs would make functions like "unfoldr"
> more attractive -- uses of foldr nearly always encapsulate this
> notion.

This would be

guarded p f x = toMaybe (p x) (f x)

in terms of my beloved but already rejected 'toMaybe':
  http://www.haskell.org/pipermail/libraries/2004-July/002326.html


More information about the Haskell-Cafe mailing list