IO作为副作用

All*_*lan 3 io haskell

我能写一个执行IO作为副作用的函数吗?例如:

f :: Int -> Int
f n = putStr "text" >> return n*2
Run Code Online (Sandbox Code Playgroud)

显然我没有任何方法来编写代码,如果它完全不正确,但这应该至少显示我想要做的事情.

Tho*_*son 11

你的功能几乎是正确的.如果它有副作用,那么它需要类型IO.此外,功能应用程序绑定比中缀更严格.修复这些结果:

f :: Int -> IO Int
f n = putStr "text" >> return (n*2)
Run Code Online (Sandbox Code Playgroud)

  • @ dave4420 Upvoting需要15个代表,OP需要更多的赞成他(她)自我之前他可以投票. (5认同)