为什么我不能这样做?
禁止在这个问题中使用“做”:/
我如何在我的列表中调用单词并同时产生 IO?
谢谢..这是我的实际代码:/
main :: IO()
main =
putStr "Name of File: " >>
getLine >>=
\st ->
openFile st ReadMode >>=
\handle ->
hGetContents handle >>=
\y ->
words y >>=
\strings ->
strings !! 1 >>=
\string->
putStr string
[编辑] 解决方案:
main :: IO()
main =
putStr "Name of File: " >>
getLine >>=
\st ->
openFile st ReadMode >>=
\handle ->
hGetContents handle >>=
\y ->
return (words y) >>=
\strings ->
return (strings !! 1) >>=
\string->
putStr string
最佳答案
使用 return (words y)
而不仅仅是 words y
。 return
将一个纯值(例如 [String]
返回的 words
)包装到一个 monad 中。
从你的措辞来看,这个问题听起来像是家庭作业。如果是这样,它应该被标记为这样。
关于Haskell,我可以在没有 IO 输出的情况下使用 monad 调用函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9961778/