[Haskell-beginners] Enum for natural numbers

kane96 at gmx.de kane96 at gmx.de
Mon Dec 21 09:02:16 EST 2009


-------- Original-Nachricht --------
> Datum: Mon, 21 Dec 2009 11:25:46 +0100
> Von: Deniz Dogan <deniz.a.m.dogan at gmail.com>
> An: kane96 at gmx.de
> CC: beginners at haskell.org
> Betreff: Re: [Haskell-beginners] Enum for natural numbers

> 2009/12/21  <kane96 at gmx.de>:
> > Now everything works but to print Z also for negative Integers. Don't
> know how to implement the <=0 or le0 in this case again:
> >
> > instance Enum Nat where
> >        toEnum 0 = Z
> >        toEnum (n+1) = S(toEnum n)
> >        fromEnum Z = 0
> >        fromEnum (S n) = 1 + fromEnum n
> >
> 
> Again, look into guards. You are definitely on the right track.
> 
> Again, you should look into guards.
> 
> instance Enum Nat where
>        toEnum n | n <= 0 = Z
>                 | otherwise = ... -- do what with n?
>        fromEnum Z = 0
>        fromEnum (S n) = 1 + fromEnum n

I did it that way:
instance Enum Nat where
    toEnum n | n <= 0 = Z
             | otherwise = S(toEnum (n-1))
   fromEnum Z = 0
   fromEnum (S n) = 1 + fromEnum n

In another task I should create an infinite list: allNats :: [Nat]
I would have done it in a recursion but there issn't a parameter to call allNats with


-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


More information about the Beginners mailing list