我是Haskell的新手。我的Haskell脚本使用GHCi
。
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
输入的内容?
m a
中有一个值,以及一个返回其值在IO单子a -> m b
中的函数。你想要最终得到一个IO操作m b
。总之,这是m a -> (a -> m b) -> m b
。停一下,Hoogle时间。>>=
是其中的第一个结果。 :) - Dan BurtonIO String -> (String -> IO ()) -> IO ()
没有结果。 - luquiputStrLn getLine
的意图,可以使用类似的反向绑定:putStrLn =<< getLine
。这是fmap
后跟着join
,所以我们可以写成join (putStrLn <$> getLine)
。但我希望有比(=<<)
更优雅的方式。 - applicative