<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
<RTE_TEXT>Hello all. This is my first post and I'm essentially a rank beginner with Haskell, having used it briefly some 7 years ago. Glad to be back but struggling with syntax and understanding error messages.<BR>
<BR>
This post actually ties to a prior post, <A href="http://www.haskell.org/pipermail/beginners/2008-December/000583.html">http://www.haskell.org/pipermail/beginners/2008-December/000583.html</A><BR>
<BR>
My troubles are with exercise 3.10, p45 of from HDIII's "Yet Another Haskell Tutorial" (YAHT). The exercise states:<BR>
<BR><FONT face="Times New Roman" size=2><FONT style="FONT-SIZE: 10pt" face="Times New Roman" size=2>
<P align=left><FONT size=3><STRONG><EM>Write a program that will repeatedly ask the user for numbers until she</EM></STRONG></FONT></P>
<P align=left><FONT size=3><STRONG><EM>types in zero, at which point it will tell her the sum of all the numbers, the product of</EM></STRONG></FONT></P>
<P align=left><FONT size=3><STRONG><EM>all the numbers, and, for each number, its factorial. For instance, a session might look</EM></STRONG></FONT></P>
<FONT size=3><STRONG><EM>like:</EM></STRONG></FONT><BR>
<STRONG><FONT size=3></FONT></STRONG> <BR>
<FONT face=Verdana>Note that the sample session accompanying the exercise suggests a session such as:</FONT><BR>
<FONT face=Verdana></FONT> <BR>
<FONT face=Verdana>Give me a number (or 0 to stop):</FONT><BR>
<FONT face=Verdana>5</FONT><BR><FONT face=Verdana>
<FONT face=Verdana>Give me a number (or 0 to stop):</FONT><BR>
8<BR>
<FONT face=Verdana>Give me a number (or 0 to stop):</FONT><BR>
2<BR>
<FONT face=Verdana>Give me a number (or 0 to stop):</FONT><BR>
0<BR>
The sum is 15<BR>
The product is 80<BR>
5 factorial is 120<BR>
8 factorial is 40320<BR>
2 factorial is 2<BR>
<BR></FONT>
<FONT face=Verdana>The following code handles the sum and products pieces--I built the module incrementally--but fails on the factorial part. In fact my current code, if it worked, would only output something like:</FONT><BR>
<FONT face=Verdana></FONT> <BR><FONT face=Verdana>
The sum is 15<BR>
The product is 80<BR>
120<BR>
40320<BR>
2<BR>
<BR>
But I'm not even getting that much. Here's the code:<BR>
<BR>
--begin code<BR>
<BR>
module AskForNumbers<BR> where<BR>import IO<BR> <BR>askForNums = do<BR> putStrLn "Enter a pos int or 0 to end: "<BR> numStr <- getLine<BR> let num = read numStr<BR> if num == 0<BR> then return []<BR> else do<BR> rest <- askForNums<BR> return (num:rest)<BR>
<BR>
listFactorial l = <BR> if length l == 0<BR> then return 1<BR> else do<BR> fact (head l)<BR> listFactorial (tail l)<BR>
<BR>
fact n = <BR> if n == 0 <BR> then return 1<BR> else return foldr (*) 1 [1..n]<BR> <BR>f = do<BR> nums <- askForNums<BR> putStr ("Sum is " ++ (show (foldr (+) 0 nums)) ++ "\n")<BR> putStr ("Product is " ++ (show (foldr (*) 1 nums)) ++ "\n")<BR> listFactorial nums<BR>
<BR>
--end code<BR>
<BR>
Here is the error msg I get when I load into WinHugs (Sept 2006 version):<BR>
<BR><FONT color=#af0000 size=2><FONT color=#af0000 size=2>
<EM>ERROR file:.\AskForNumbers.hs:22 - Ambiguous type signature in inferred type</EM><BR>
<EM>*** ambiguous type : (Num a, Num [a], Monad ((->) [b]), Num c, Num (b -> [a] -> [a]), Enum a, Monad ((->) (c -> c -> c))) => a -> [b] -> [a]</EM><BR>
<EM>*** assigned to : fact</EM><BR></FONT></FONT>
<BR>
Ambiguous type assigned to fact. Ok. Not sure what to make of that or how to correct it. I though I was passing fact an Integer, since I think nums is a list of Integers, since the sum and product lines in f work ok. <BR>
<BR>
Help? Thanks.<BR>
<BR>
David<BR>
<BR></FONT></FONT></FONT></RTE_TEXT></body>
</html>