你能把 Haskell 列表变成一系列 do 指令吗?
您可以创建一个函数列表,然后按顺序执行它们,也许将它们传递给 do 表示法?
我目前正在通过映射数据列表来执行此操作,并且想知道是否可以以某种方式调用将结果作为一系列顺序调用传递?
Can you create a list of functions and then execute them sequentially, perhaps passing them into do notation?
I'm currently doing this by mapping over a list of data and am wondering if I can call somehow pass the result as a series of sequential calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
到目前为止,答案很好,但如果您还希望每个函数不作用于原始数据,而是作用于前一个函数的结果,请查看折叠函数,例如foldl、foldl1和foldr:
并且您可能需要monadic版本、foldM 和 FoldM_ :
good answers so far, but if you also want each function to act not on the original data but on the result of the previous function, look at the foldding functions, such as foldl, foldl1, and foldr:
and you may need the monadic version, foldM and foldM_ :
像这样的东西吗?
Something like this?
如果这些是函数,即纯函数,那么您可以使用
($)
或“apply”:当然不能保证这种情况会按顺序发生,但您将获得返回值的列表。
如果这些是动作,即不纯的,那么你想要的就是
sequence_
:sequence_
很容易自己编写:还有一个
sequence
(不带下划线)运行一系列操作并返回其结果:If these are functions, ie pure, then you can use
($)
or "apply":There's no guarantee that this happens sequentially of course, but you'll get a list of the return values.
If these are actions, ie impure, then what you want is called
sequence_
:sequence_
is pretty easy to write yourself:There is also a
sequence
(without the underscore) that runs a bunch of actions and returns their results: