Hello, thanks a lot for the expeditious replies to my trifling question. Here is another one: Can you tell me why the following does not function? I mean the last part for converting number into the new data type. If this is not a quick fix, please ignore it. As an utter beginner, I cannot estimate the significance of my questions. data Nat = Zero | Succ Nat natToInt Zero = 0 natToInt (Succ n) = (natToInt n) + 1 number n = case n of 0 -> Zero _ -> Succ(number(n-1)) Thank you very much. -- "Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ... Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
Hi
number n = case n of 0 -> Zero _ -> Succ(number(n-1))
Looks fine to me, what exactly doesn't work? Type error, parse error? Gives the wrong result? For what values? You might also get more success if you email haskell-cafe -AT- haskell.org, which is for general Haskell stuff and gets a lot more traffic and readers than hugs-users. Either than or the Haskell IRC channel for instant answers. Thanks Neil
At 10:14 PM +0200 2006/8/3, aldirithms@gmx.net wrote:
Hello,
thanks a lot for the expeditious replies to my trifling question. Here is another one:
Can you tell me why the following does not function? I mean the last part for converting number into the new data type. If this is not a quick fix, please ignore it. As an utter beginner, I cannot estimate the significance of my questions.
data Nat = Zero | Succ Nat
natToInt Zero = 0 natToInt (Succ n) = (natToInt n) + 1
number n = case n of 0 -> Zero _ -> Succ(number(n-1))
Most likely the problem you encountered looked something like Main> number 0 ERROR - Cannot find "show" function for: *** Expression : number 0 *** Of type : Nat The easy solution is to add deriving (Show) to the definition of Nat. Or, if you want to do it yourself, instance Show Nat where show Zero = "Zero" show (Succ n) = "(Succ " ++ show n ++ ")" Either way, you're providing a conversion from Nat to String, which is necessary if Nat values are ever to be displayed. Cheers, --Ham -- ------------------------------------------------------------------ Hamilton Richards, PhD Department of Computer Sciences Senior Lecturer (retired) The University of Texas at Austin ham@cs.utexas.edu hrichrds@swbell.net http://www.cs.utexas.edu/users/ham/richards ------------------------------------------------------------------
participants (3)
-
aldirithms@gmx.net -
Hamilton Richards -
Neil Mitchell