exercise 3.10 in YAHT

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

exercise 3.10 in YAHT

George Huber
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

Reply | Threaded
Open this post in threaded view
|

exercise 3.10 in YAHT

Fernando Henrique Sanches
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 <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090609/d8b741a9/attachment-0001.html
Reply | Threaded
Open this post in threaded view
|

exercise 3.10 in YAHT

Brent Yorgey-2
In reply to this post by George Huber
On Tue, Jun 09, 2009 at 12:05:21AM -0400, George Huber wrote:
>
> 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"
>

This error can also sometimes be triggered by indentation problems.
Check to make sure you are using only spaces and not tabs to
indent. (Tabs are interpreted as a certain number of spaces by ghc---I
forget how many---which may be different than the number of spaces
your editor is configured to show them as, which can lead to
indentation being screwy.  Good Haskell practice is to never use tabs;
in practice most editors have an option to turn tabs into spaces.)

Dunno if that's really your problem but it's worth a shot!

-Brent
Reply | Threaded
Open this post in threaded view
|

Re: exercise 3.10 in YAHT

Maurí­cio
In reply to this post by George Huber
> 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

No problem here too, ghc 6.8.2.

Maybe seeing how things are when layout style is not used
helps you see a possible layout problem. Maybe it doesn't,
and you can just forget the example below :)

module Main (main) where {
   getNums = putStrLn "Enter a number (zero to stop):" >> do {
     num <- readLn ;
     if num == 0 then return [] else
       do {rest <- getNums ; return (num:rest)}
   } ;

   main = getNums >>= putStrLn . show
}

Best,
Maur?cio