
Hi, I am new to Haskell. I wrote the following code: module Main where import IO main = do hSetBuffering stdin LineBuffering numList <- processInputs foldr (+) 0 numList processInputs = do putStrLn "Enter a number:" strNum <- getLine let num = read strNum if num == 0 then return [] else do rest <- processInputs return (num : rest) And am getting the following errors: ERROR "Ex310.hs":6 - Type error in final generator *** Term : foldr (+) 0 numList *** Type : Integer *** Does not match : IO a Any pointers to the source of this error would be appreciated. Thanks, Logo

Loganathan Lingappan wrote:
main = do hSetBuffering stdin LineBuffering numList <- processInputs foldr (+) 0 numList
The type of main is understood to be IO (), so it can't return anything. You could work around this by rewriting the last line above as follows: print (foldr (+) 0 numList) This prints the number, which is presumably what you want, and print has type IO (), so it works out nicely here.

On Sat, 2007-12-08 at 16:39 -0800, Bryan O'Sullivan wrote:
Loganathan Lingappan wrote:
main = do hSetBuffering stdin LineBuffering numList <- processInputs foldr (+) 0 numList
The type of main is understood to be IO (), so it can't return anything. You could work around this by rewriting the last line above as follows:
print (foldr (+) 0 numList)
This prints the number, which is presumably what you want, and print has type IO (), so it works out nicely here.
Nitpicking: Actually, as the error message says, the type of main is IO a so it can 'return' -anything- and that will be discarded as the type makes clear. The issue is that foldr (+) 0 numList :: Integer and that's not IO a for any a. Either way, Bryan's suggestion is probably what you want.
participants (3)
-
Bryan O'Sullivan
-
Derek Elkins
-
Loganathan Lingappan