listexceptionerror-handlingerlangself-modifying

Why can I not rewrite in a list in Erlang?


I have a list L = [1,2,3].

I perform the following on L:

lists:sublist(L,2) ++ [10] ++ lists:nthtail(3,L).

Instead of storing the result in a new list, I want to store the result in L itself. However, when I do so, I am getting the obvious error: ** exception error: no match of right hand side value [1,2,300]

I don't want to use a new variable, I want to rewrite in L itself. Is it possible?


Solution

  • No, Erlang has single assignment. To use an example from Armstrong, in C this works:

    x = 5;
    x = x + 10;
    

    But in Erlang it is written:

    X = 5;
    X1 = X + 10;