[Haskell-cafe] Why does Enum succ and pred functions throw exception
f at mazzo.li
Thu Jun 21 11:40:50 CEST 2012
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.
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)
More information about the Haskell-Cafe