It compiles fine here with GHC 6.8.2:

*Main> getNums
Enter a number (zero to stop):
4

Enter a number (zero to stop):
3

Enter a number (zero to stop):
0
[4,3]

Are you sure the problem is in this function?

Fernando Henrique Sanches


On Tue, Jun 9, 2009 at 1:05 AM, George Huber <geohuber@verizon.net> wrote:
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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners