[Haskell-beginners] Enum for natural numbers

Daniel Fischer daniel.is.fischer at web.de
Sun Dec 20 15:28:21 EST 2009


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.


More information about the Beginners mailing list