为什么惰性语言不支持变异?
我正在研究编程语言理论,但我无法找出惰性语言没有突变的坚实原因。有人知道原因吗?
I'm studying programming language theory and I can't figure out a solid reason why lazy languages don't have mutation. Anyone know the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
惰性意味着直到(或除非)使用函数的返回值,才真正计算函数的值。这意味着函数调用不一定按照它们在代码中出现的顺序进行计算。这也意味着不能有 void 函数,因为它们永远不会被计算(因为不可能使用不存在的返回值)。
然而,对于执行副作用的函数(例如突变,但也只是打印到屏幕),它们的执行顺序确实很重要。更重要的是他们被处决了。这意味着惰性语言需要一种方法来模拟特殊类型的副作用,以确保它们被执行并以正确的顺序执行。
由于完全无副作用的程序是无用的(您需要能够打印到屏幕),惰性语言实际上确实支持副作用。他们只是用 IO monad 或唯一性类型封装它们。例如,haskell 确实有可变数组,但它们只能在 IO monad 内部使用。
Laziness means that a function is not actually evaluated until (or unless) its return value is used. This means that function calls aren't necessarily evaluated in the order in which they appear in the code. It also means that there can't be void functions because they would never be evaluated (as its not possible to use a return value that does not exist).
However for functions that perform side effects (like mutation, but also just printing to the screen) it does matter in which order they're executed. It matters even more that they're executed at all. This means that lazy languages need a way to emulate side effects in special types that ensure that they are executed and executed in the right order.
Since entirely side effect-free programs are useless (you need to be able to print to the screen at all), lazy languages actually do support side effects. They just encapsulate them with the IO monad or uniqueness types. As an example haskell does have mutable arrays, but they can be only used inside the IO monad.
突变意味着您无法随时确定程序的状态,并且必须担心任何操作的副作用。我实际上已经考虑过了,但我想不出任何方法来拥有支持突变的完全惰性语言。 (不过我不是计算机科学家。)
Mutation means you cannot be sure of the state of the program at any time and will have to worry about side effects from any action. I've actually thought about it and I can't think of any way to have a completely lazy language that supports mutation. (I am no computer scientist though.)