Is Erlang “single assignment” different from Haskell “immutable values”?

后端 未结 5 1057
失恋的感觉
失恋的感觉 2020-12-15 07:51

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

5条回答
  •  离开以前
    2020-12-15 08:24

    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
    

    Edit: From wikipedia

    Immutable Object:

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

    Single Assignment:

    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.

提交回复
热议问题