At Thu, 21 Jun 2012 10:11:24 +0100 (BST),
Rouan van Dalen wrote:
> Hi everyone,
> 
> Can anyone shed some light on why the succ and pred functions of the Enum 
> typeclass throw
> exceptions if we go over the upper or lower boundary, and not return Maybe a?
> 
> I was hoping to have some functions like:
> 
>   safeSucc :: (Enum a) => a -> Maybe a
> 
> Because the succ and pred functions throw exceptions I can only catch them in
> the IO monad.  This makes it hard to deal with this situation in pure code.
> 
> Regards
> 
> Rouan.

That decision was most likely dictated by the convenience of now
having the Maybe - and remember that many Enum data types are not
bounded anyway, so they would never need that.

In any case, you can easily roll your own

    safeSucc :: (Enum a, Bounded a) => a -> Maybe a
    safeSucc x | x == maxBound = Nothing
               | otherwise     = Just (succ x)

Francesco.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to