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

Sebastian Sylvan sebastian.sylvan at gmail.com
Thu Jun 23 05:58:23 EDT 2005


On 6/23/05, kynn at panix.com <kynn at panix.com> wrote:

> 
> 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)

This will give you an error as well. 'num' is of type Integer, whereas
getList is of type IO [Integer].
So consing an integer with an IO-action returning a list of integer is
a type error.

Try something like:

>   if num == 0
>      then return []
>      else do rest <- getList
                    return (num:rest)

So if there are more numbers to be read you _run_ the action getList,
retrieve the result, and then return that result with the  number you
just read in front of it.

/S

-- 
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862


More information about the Haskell-Cafe mailing list