纯函数式编程语言中的副作用方法
目前,我知道以下将副作用集成到纯函数式编程语言中的方法:
- 效果系统
- 延续
- 独特类型
- monad
Monad 通常被认为是实现此目的的最有效和最通用的方法。
还有哪些其他方法? 他们如何比较?
At the moment I'm aware of the following methods to integrate side-effects into purely functional programming languages:
- effect systems
- continuations
- unique types
- monads
Monads are often cited to be the most effective and most general way to do this.
Which other methods exist? How do they compare?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
箭头,比 monad 更通用。
Arrows, which are more general than monads.
最简单的方法是在函数之间传递环境。 这通常用于教学计划。
The very simplest method is to simply pass around the environment between the functions. This is often used to teach scheme.
对我来说,更通用的方法是通过 monad/comonad 对。 这概括了常见的“单子”方法,应该正确地称为“强单子”方法,因为它只适用于强单子。
转向单子/共单子对允许对导致某些变量不再可用的效应进行建模。 一个有用的示例是将线程迁移到分布式设置中的另一台主机的效果。
历史上感兴趣的另一种方法是使整个程序成为将输入事件流/列表映射到输出事件流/列表的函数。 请参阅:Phil Wadler 的“如何声明命令”: http://www.cs.bell-labs.com/~wadler/topics/monads.html#monadsdeclare
To me a more general way is via a monad/comonad pair. This generalizes the common "monad" approach which should correctly be called the "strong monad" approach, since it only works with strong monads.
Moving to a monad/comonad pair allows effects to be modeled that result in some variables no longer being available. An example where this is useful is the effect of migrating a thread to another host in a distributed setting.
An additional method of historical interest is to make the whole program a function mapping a stream/list of input events to a stream/list of output events. See: "How to Declare an Imperative" by Phil Wadler: http://www.cs.bell-labs.com/~wadler/topics/monads.html#monadsdeclare