为什么 `putStrLn getLine` 不起作用?
我是 Haskell 的新手。 我的带有 GHCi
的 Haskell 脚本
Prelude> let a = putStrLn getLine
会出现这样的错误。
<interactive>:1:17:
Couldn't match expected type `String'
against inferred type `IO String'
In the first argument of `putStrLn', namely `getLine'
In the expression: putStrLn getLine
In the definition of `a': a = putStrLn getLine
Prelude>
为什么它不起作用以及如何打印从stdin
输入的内容?
I'm complete newbie on Haskell.
My Haskell script with GHCi
,
Prelude> let a = putStrLn getLine
makes an error like this.
<interactive>:1:17:
Couldn't match expected type `String'
against inferred type `IO String'
In the first argument of `putStrLn', namely `getLine'
In the expression: putStrLn getLine
In the definition of `a': a = putStrLn getLine
Prelude>
Why doesn't it work and how can I print something input from stdin
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类型不匹配。
getLine
是一个IO
操作,putStrLn
接受一个纯字符串。您需要做的是将行绑定到 IO monad 内,以便将其传递给 putStrLn 。以下是等效的:
The types do not match.
getLine
is anIO
action, andputStrLn
takes a plain string.What you need to do is bind the line inside the
IO
monad in order to pass it toputStrLn
. The following are equivalent: