[Haskell-beginners] exercise 3.10 in YAHT
George Huber
geohuber at verizon.net
Tue Jun 9 00:05:21 EDT 2009
I'm currently working through "Yet Another Haskell Tutorial" by H.
Daume and have a question on how to proceed with his exercise 3.10.
The exercise question is: Write a program that will repeatedly ask the
user for numbers until he types in zero, at which point it will tell him
the sum of all the numbers, the product of all the numbers, and, for
each number, its factorial.
The hint that is given is to write an IO action that reads a number and
either returns an empty list (if the number is zero) or recurses itself
making a list from the number and the result of the recursive call.
Daume presents and example of such a function:
askForWords = do
putStrLn "Please enter a word:"
word <- getLine
if word == ""
then return []
else do
rest <- askForWords
return (word : rest)
so, based on his example I created the function:
getNums = do
putStrLn "Enter a number (zero to stop):";
strNum <- getLine
let num = read strNum
if num == 0
then return []
else do
rest <- getNums
return (num:rest)
loading the file that contains the above function into GHCi (version
6.8.3) gives the following error "The last statement in a 'do' construct
must be an expression"
being a beginner, I'm at a loss at the cause of the error and how to fix
it -- any suggestions?
thanks,
george
More information about the Beginners
mailing list