i want to do something like:
succ' :: (Bounded a, Eq a, Enum a) => a -> a
succ' n
| n == (maxBound :: a) = minBound :: a
| otherwise = succ n
but this does not work. how to solve this?
i want to do something like:
succ' :: (Bounded a, Eq a, Enum a) => a -> a
succ' n
| n == (maxBound :: a) = minBound :: a
| otherwise = succ n
but this does not work. how to solve this?
You don't need the type annotations, and they're the source of the errors you're getting:
(This works in Haskell 98 and Haskell 2010, so pretty much any compiler you've got lying around .) Also, I indented the
|
a little, because they can't line up with the start of the function; they're part of the definition ofsucc'
, not standalone code.Here's some test data:
I got
[B,C,A]
.Several possibilities, the Haskell2010 way,
the types are determined from the use, the type of both,
maxBound
andminBound
must be the type of the argument.Or you can use the
ScopedTypeVariables
extension, bring the type variable into scope so it can be used in local type signatures,but, as seen above, there's no need for that here.
A third possibility is to use
asTypeOf :: a -> a -> a
,which, again, is not needed here, but can be useful in other situations.