[Haskell] Problem with types.

Martin Grabmueller magr at cs.tu-berlin.de
Wed Aug 16 03:41:22 EDT 2006

Szymon Ząbkiewicz wrote:
> Hi!
> I'm new to Haskell and FP so i've got some problems. I wrote this code :
> getNr :: Int -> Int
> getNr x = do
>     putStrLn ("enter " ++ x ++ " number:")
>     nr <- getLine
>     return nr
> And when I try to load it to GHCi I get an error (there is also plenty
> of others but they look the same):

There are several problems with this code:

1. You are using (++) to concatenate strings and the value x, which
   is of type Int.  Use 'show' to convert numbers to strings.

2. The result type of getNr is Int, but the body of the function is
   a do expression, which is always a monadic value.  Since you use
   the input/output operations putStrLn and getLine, the type of
   the function must be Int -> IO Int

3. The return value of getLine is a string, but you try to return
   it from the function, which (see 2) has a return value of IO Int.
   Use 'read' to convert the string to an integer value.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org//pipermail/haskell/attachments/20060816/fd8e8b6f/signature.bin

More information about the Haskell mailing list