[Haskell-beginners] Enum for natural numbers
Deniz Dogan
deniz.a.m.dogan at gmail.com
Sun Dec 20 15:39:39 EST 2009
2009/12/20 <kane96 at gmx.de>:
>
> -------- Original-Nachricht --------
>> Datum: Sun, 20 Dec 2009 21:28:21 +0100
>> Von: Daniel Fischer <daniel.is.fischer at web.de>
>> An: beginners at haskell.org
>> Betreff: Re: [Haskell-beginners] Enum for natural numbers
>
>> Am Sonntag 20 Dezember 2009 21:06:36 schrieb kane96 at 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
>
If I may say so, I don't think that your understanding of Enum is the
problem here, it's the lack of understanding of Haskell in general,
but specifically algebraic data types[1] and recursion[2].
[1] data Maybe a = Just a | Nothing
[2] f n = f (n - 1) + f (n - 2)
--
Deniz Dogan
More information about the Beginners
mailing list