
kane96@gmx.de wrote:
-------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 21:28:21 +0100 Von: Daniel Fischer
An: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers Am Sonntag 20 Dezember 2009 21:06:36 schrieb kane96@gmx.de:
So,
toEnum n | n < 0 = Z toEnum 0 = Z toEnum n = ? -- here, we know n > 0
fromEnum should be the correspondnece the other way round, so
fromEnum Z = 0 fromEnum (S p) = ? -- which Int corresponds to the successor of p? what is P? Any element of Nat (p for Peano).
Now I read some short textes about it and think I know more or less what I have to do for the exercise. But I don't know really how. Do you know any examples for it, how it normally looks like? Deniz Dogan posted a few links to recursion earlier today, if you look at them, you should get the general idea. As a further example,
replicate :: Int -> a -> [a] replicate n x | n <= 0 = [] | otherwise = x:replicate (n-1) x
may help.
my problem is that I don't know how to use Enum correctly and didn't find any helpfull example
Enum is used for things that are in sequence and can be numbered likewise. For example, (if you consider Monday the first day of the week) data Weekday = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday probably Monday would correspond to 0, Tuesday to 1, Wednesday to 2, and so on through Sunday corresponding to 6. In your problem, data Nat = Z | S Nat Z corresponds to 0, (S Z) corresponds to 1, (S (S Z)) corresponds to 2, and so on forever. Perhaps you can see this by seeing "Z" as "0" and "S" as "1 +". Z <-> 0 (S Z) <-> (1 + 0) (S (S Z)) <-> (1 + (1 + 0)) etc... but you'll have to find a way to define this pattern with just a small number of cases, not a huge infinite number. -Isaac