Newbie question
Einar Karttunen
ekarttun at cs.helsinki.fi
Sun Jan 9 08:45:24 EST 2005
Dmitri Pissarenko <mailing-lists at dapissarenko.com> writes:
> a) asking the user to enter several numbers (while the end of the sequence is
> indicated by entering 0)
> b) calculate the sum of those numbers.
>
> ...
>
Here is a corrected version:
> module Main where
>
> import IO
Delete this.
> main = do
> hSetBuffering stdin LineBuffering
This is extraneous - stdin should be line buffered by default.
> words <- askForNumbers
> printWords words
> map read words
> putStrLn "The sum is"
> foldl (+) 0 words
Here you map read over words and discard the result.
Then you wold over the words and produce an interger,
whereas you should produce an IO value from the do block.
Here is a complete implementation (untested):
main = do
words <- askForNumbers
mapM_ putStrLn words
putStrLn "EOL"
let nums = map read words
print nums
putStrLn "The sum is"
print (foldl (+) 0 nums)
- Einar Karttunen
More information about the Glasgow-haskell-users
mailing list