putStr

Jason Wilcox jasonw at cat.pdx.edu
Mon Oct 13 12:00:05 EDT 2003


On Mon, Oct 13, 2003 at 06:32:14PM +0100, Jose Morais wrote:
> Hi,
> 
> 	What I needed was actualy something more like
> 
> f3 :: Int -> String
> f3 x = show x ++ f4 x
>  
> f4 :: Int -> IO String
> f4 x = do
>           putStr ("initial value is " ++ show x)
>           return (show x)
> 
> 
> but this gives the error
> 
> 
> Type checking
> ERROR "teste.hs":11 - Type error in application
> *** Expression     : show x ++ f4 x
> *** Term           : f4 x
> *** Type           : IO String
> *** Does not match : [a]
> 
> 
> 	Can you help me?
> 
> 
> 	Thank you

Your problem is that once you use the IO Monad in a function, every
function that calls it must use IO as well. (If a function you call
has side-effects then you have side-effects by association.) The
following version of f3 should fix your problem:

f3 :: Int -> IO String
f3 x = do x' <- f4 x
	  return (show x ++ x')

This code first extracts the String from the IO String returned by f4
and binds it to the variable x'. x' is now just a plain String which
can be concat'd onto other strings, which is what we do in the second line.

I hope this helps,

-- 
--------------------------------------------------
Jason Wilcox		    CS Tutor Coordinator
jasonw at cs.pdx.edu                   503.725.4023
tutors at cs.pdx.edu  www.cat.pdx.edu    FAB 135-01
--------------------------------------------------


More information about the Haskell-Cafe mailing list