onu*_*tas -1 monads haskell scope functional-programming do-notation
我想转换IO [String]到[String]具有<-约束力.但是,我需要do在一个where声明中使用一个块来做到这一点,但是Haskell一直抱怨缩进.这是代码:
decompEventBlocks :: IO [String] -> IO [[String]]
decompEventBlocks words
| words' /= [] = block : (decompEventBlocks . drop $ (length block) words')
| otherwise = []
where
do
words' <- words
let block = (takeWhile (/="END") words')
Run Code Online (Sandbox Code Playgroud)
这是什么原因?我们如何do在where声明中使用块?而且,我们是否有机会在警卫面前发表一些声明?
您无法将 IO String 转换为String.
但是,您可以做的是将IO String的内容绑定到'变量',但这仍然会导致整个计算嵌入到IO中.
foo = do
x <- baz -- here baz is the IO String
let x' = doStuff x
return x' -- embeds the String inside IO, as otherwise the computation would result in IO ()
Run Code Online (Sandbox Code Playgroud)
回答你的问题
foo x = baz x -- x here is your 'IO String'
where
baz x = do
x' <- x
return $ doStuff x'
Run Code Online (Sandbox Code Playgroud)