Erlang 是“单赋值"吗?与 Haskell 的“不可变值"不同? [英] Is Erlang "single assignment" different from Haskell "immutable values"?

查看:21
本文介绍了Erlang 是“单赋值"吗?与 Haskell 的“不可变值"不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Programming Erlang"一书中,据说该语言使用单赋值"变量.在其他关于函数式编程语言的文章中,我总是读到不可变值".

In the "Programming Erlang" book it's said that the language uses "single assignment" variables. In other articles about functional programming languages I always read of "immutable values" instead.

单一赋值"一词与不可变值"的含义是否不同?

Does the wording "single assignment" mean something different from "immutable values"?

推荐答案

在 erlang 中,变量可以是绑定的,也可以是未绑定的.您只能为未绑定的变量赋值.这就是单一赋值的来源,因为一旦变量被绑定,您就不能再为其分配新值.所以在 erlang 中,即使 01 是不可变的值,你也不能做下面的事情.

In erlang, a variable can be either bound or unbound. You can only assign a value to an unbound variable. And that's where the single assignment comes from, because once the variable is bound, you can no longer assign a new value to it. So in erlang, you can't do the following, even if 0 and 1 are immutable values.

X = 1.
X = 2. // This is not a valid operation

术语不可变是相对于变量的值,而不是变量本身.因此,在某些语言中,您可以为同一变量分配不同的不可变值:

The term immutable is relative to the value of the variable, and not the variable itself. So in some languages, you can assign to the same variable different values that are immutable:

X = immutableValue;
X = anotherImutableValue; // This is a valid operation

来自维基百科

不可变对象:

在面向对象和函数式编程中,不可变对象是指在创建后状态无法修改的对象.

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.

单一作业:

单次赋值是名称绑定的一个例子,与本文中描述的赋值不同,它只能做一次,通常是在创建变量时;不允许后续重新分配.[...] 一旦通过单一赋值创建,命名值就不是变量而是不可变对象.

Single assignment is an example of name binding and differs from assignment as described in this article in that it can only be done once, usually when the variable is created; no subsequent re-assignment is allowed. [...] Once created by single assignment, named values are not variables but immutable objects.

这篇关于Erlang 是“单赋值"吗?与 Haskell 的“不可变值"不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆