[Haskell-beginners] Enum for natural numbers

Deniz Dogan deniz.a.m.dogan at gmail.com
Mon Dec 21 05:25:46 EST 2009


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

You are on the right track.

-- 
Deniz Dogan


More information about the Beginners mailing list