[Haskell-cafe] Noob error: Type b -> c b Does not match IO a

kynn at panix.com kynn at panix.com
Thu Jun 23 00:17:58 EDT 2005


--text follows this line--


I'm trying to learn Haskell from YAHT.

My attempt at a solution of Exercise 3.10 is failing with a "Type does
not match" error.  The exercise is to write a function that will read
numbers (one per line) from the command line, until the number 0 is
entered.  At this point the program is supposed to print out the sum
of the numbers, their product, and for each number, the factorial.
The interaction should resemble this:

Give me a number (or 0 to stop):
5
Give me a number (or 0 to stop):
8
Give me a number (or 0 to stop):
2
Give me a number (or 0 to stop):
0
The sum is 15
The product is 80
5 factorial is 120
8 factorial is 40320
2 factorial is 2

The code I have for this is given next (the line that fails is
indicated by a comment):

module Main
    where

import IO

main = do
  hSetBuffering stdin LineBuffering
  numberList <- getList
  let s = sumList numberList
  let p = prodList numberList
  putStrLn ("The sum is " ++ s)
  putStrLn ("The product is " ++ p)
  printFact numberList


fact 0 = 1
fact 1 = 1
fact x = x * fact x - 1

sumList [] = 0
sumList (x:xs) = x + sumList xs

prodList [] = 1
prodList (0:xs) = 0
prodList (x:xs) = x * prodList xs

printFact [] = return
printFact (x:xs) = do  -- triggers error message
  putStrLn (x ++ " factorial is " ++ fact x)
  printFact xs
  return

getList = do
  putStrLn "Give me a number (or 0 to stop):"
  numString <- getLine
  let num = read numString
  if num == 0
     then return []
     else return (num:getList)



Here's the full error I get (from HUGS):

IO> :l Ex3_10
ERROR "./Ex3_10.hs":28 - Type error in generator
*** Term           : printFact xs
*** Type           : b -> c b
*** Does not match : IO a



If anyone can explain to me how to fix this error I'd appreciate it.

Also, what is the difference between <- and let?

Lastly, any comments on any other detail of the code, particularly
coding, style are most welcome.

Thanks!

kj



More information about the Haskell-Cafe mailing list