【发布时间】:2017-04-22 11:31:14
【问题描述】:
我有这个功能:
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版本中,您有2 个绑定和一个return 语句。在没有do的版本中,您使用>>,它会丢弃其左操作数的结果,并且您唯一返回的是[]。do版本还使用:运算符,do-less 版本没有。