我有这样一个函数:
sequence :: [IO a] -> IO [a]
sequence [] = pure []
sequence (op:ops) = do
x <- op
xs <- sequence ops
return (x:xs)
这个函数只写了一系列的IO操作。
问题在于,我想编写同样的函数,但是完全不使用'do notation',只使用运算符 >> 和 >>=。
我已经有这个版本:
mySequence:: [IO a]-> IO [a]
mySequence [] = pure []
mySequence (op:ops) =
op >> sequence ops
但是,例如对于输入[pure 1,pure 2],它无法运行。
有人能帮我解决这个问题吗?
提前致谢。
do
版本中,你有两个绑定和一个返回语句。在没有do
的版本中,你使用>>
,它会丢弃其左操作数的结果,你唯一返回的是[]
。do
版本还使用了:
运算符,而没有do
的版本则没有。 - sepp2k