
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 -------- Original-Nachricht --------
Datum: Sun, 20 Dec 2009 14:45:02 +0100 Von: Deniz Dogan
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] Enum for natural numbers
Maybe I didn't understand the exercise if have to do. It says: "Write the instance Enum Nat where toEnum is defined as a total function
2009/12/20
: that returns Z for negative integers. Some examples: *Main> toEnum (-1) :: Nat Z *Main> toEnum 0 :: Nat Z *Main> toEnum 1 :: Nat S Z *Main> fromEnum (S (S Z)) 2
so I did: data Nat = Z | S Nat deriving (Eq,Ord,Show) instance Enum Nat where toEnum x|x > 0 = S Z |otherwise = Z
somehow it looks really wrong. Do you understand want I have to do and how it should look like?
In your current definition of toEnum, you always return "S Z" (in other words, "1", "the successor of zero") for any integer i > 0. This is clearly incorrect as you said. Your "otherwise" guard is correct, but the other one is not. I get the feeling that this exercise is about learning how to use recursion, so look into that.
A few relevant links: http://en.wikibooks.org/wiki/Haskell/Recursion (the factorial example) http://learnyouahaskell.com/recursion http://book.realworldhaskell.org/read/functional-programming.html
Hope that helps
-- Deniz Dogan
-- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser