[Haskell-beginners] Enum for natural numbers

Daniel Fischer daniel.is.fischer at web.de
Mon Dec 21 05:28:14 EST 2009


Am Montag 21 Dezember 2009 10:56:06 schrieb 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)

Note that many frown upon (n+k)-patterns and they may be removed from the language in 
future. Use guards:

toEnum n
    | condition1 = rhs1
    | condition2 = rhs2

>         fromEnum Z = 0
>         fromEnum (S n) = 1 + fromEnum n



More information about the Beginners mailing list