Haskell函数,它接受一个参数并将字符串输出到输出

时间:2013-12-01 20:47:45

标签: haskell

我现在正在学习哈斯克尔。 现在我想编写一个带有一个参数的函数(例如Int),将一些字符串输出到输出并返回该参数。我正在尝试做这样的事情:

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

main = print $ test 200

现在我收到了这样的错误:

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 }

有没有办法实现我想要的东西?

2 个答案:

答案 0 :(得分:4)

由于测试产生的输出对用户可见,因此必须返回IO Int,而不是Int。看看Haskell wiki上的introduction to IO

答案 1 :(得分:0)

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

main :: IO ()
main = test 200
相关问题