I would like to write a function with type next :: Enum a => a -> a which will wrap a values around; that is next value == toEnum 0 where value is the last value of a. I have not found any functionality on Enum types to do this. In particular how to specify the last element of any Enum type, or how to determine the number of elements in an Enum type. Thanks. Jaime Nino
On Fri, 07 Jan 2005 18:57:01 -0600, Jaime Nino <jaime@cs.uno.edu> wrote:
where value is the last value of a. ^^^^^^^^^^^^^^^^
I have not found any functionality on Enum types to do this. In particular how to specify the last element of any Enum type, or how to determine the ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
number of elements in an Enum type. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It looks as if you really want to use the type class Bounded as well. You can use maxBound and minBound to find the last and first value of a Bounded type. If you impose the additional constraint Eq and Bounded you can write next like so: next :: (Enum a, Bounded a, Eq a) => a -> a next x | x == maxBound = minBound | otherwise = succ x /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
Jaime Nino writes:
I would like to write a function with type next :: Enum a => a -> a which will wrap a values around [...].
You'll need (Bounded a, Enum a) to implement that. The function you are looking for is 'maxBound'. Enumerable types are not necessarily bounded in the general case, just think of the natural numbers for an example of an enumerable set that is not. Peter
On 09 Jan 2005 07:56:58 +0100, Peter Simons <simons@cryp.to> wrote:
Jaime Nino writes:
I would like to write a function with type next :: Enum a => a -> a which will wrap a values around [...].
You'll need (Bounded a, Enum a) to implement that. The function you are looking for is 'maxBound'. Enumerable types are not necessarily bounded in the general case, just think of the natural numbers for an example of an enumerable set that is not.
He'll also need Eq a, right? How else does he investigate wether the value is equal to maxBound? One could use exceptions, I suppose, and catch the exception which arises from trying to use succ on maxBound. But then you'll need to be in the IO monad, unless you use unsafePerformIO =) /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
participants (3)
-
Jaime Nino -
Peter Simons -
Sebastian Sylvan