Ilya Boltnev
Ilya Boltnev

Reputation: 1061

Haskell function that takes one argument and prints a string to the output

I'm learning haskell now. Now i want to write a function which takes one argument(Int, for example), prints some string to the output and returns this argument. I'm trying to do something like this:

test :: Int -> Int
test h = do
         putStrLn "Here will be number!"
         h

main = print $ test 200

Now i getting such error:

Couldn't match expected type `Int' with actual type `m0 b0'
Expected type: m0 a0 -> m0 b0 -> Int
  Actual type: m0 a0 -> m0 b0 -> m0 b0
In a stmt of a 'do' block: h
In the expression:
  do { putStrLn "Here will be number!";
       h }

Is there way to implement what I want?

Upvotes: 0

Views: 577

Answers (2)

md2perpe
md2perpe

Reputation: 3061

test :: Int -> IO ()
test n = putStrLn (show n)

main :: IO ()
main = test 200

Upvotes: 0

Benjamin Barenblat
Benjamin Barenblat

Reputation: 1311

Since test produces output visible to the user, it must return an IO Int, not an Int. Have a look at the introduction to IO on the Haskell wiki.

Upvotes: 4

Related Questions