03 Dec 2013, 08:25

Ved Antani (2 posts)

Page 21 section 3.3 says “But Elixir knows that list1 will never change, so it simply constructs a new list with a head of 4 and a tail of list1” But Elixir lets list1 hold other values.

iex(9)> list1 = [1,2,3]
[1, 2, 3]
iex(10)> list1 = [3,4,5]
[3, 4, 5]
--------------------------->list1 changed
iex(11)> list1
[3, 4, 5]
iex(12)> list2 = [5 | list1]
[5, 3, 4, 5]
iex(13)> list1 = [6,7,8]
---------------------------> list1 changed
[6, 7, 8]
iex(14)> list2
[5, 3, 4, 5]
iex(16)> list1
[6, 7, 8]
12 Dec 2013, 16:44

Dave Thomas (338 posts)

There are two things here. Variables and values.

The list itself never changes. But the variable can reference other values. THe data itself is immutable.

02 Sep 2014, 20:50

Waseem Ahmad (1 post)

THe data itself is immutable.

Isn’t that true with other programming languages as well? e.g. The number five represented by symbol 5 will always have the same value in any other programming language. We can not do 5 = 4. So in a sense the value of number five is immutable in those programming languages as well.

How’s this data immutability concept unique to Elixir and other such functional programming languages?

Erlang on the other hand probably follows this concept much better because once a value is bounded to a variable (which is a misnomer in this case since it can not vary anymore), another value can not be bounder to it again.

10 Oct 2014, 09:35

Eric West (2 posts)

In other languages, with mutable data structures, list1 will only allocate memory once , then new writes to the array will be written over the top of the old in memory. In languages like Elixir, with immutable data, list1 will not write over the top of the old value. This is one thing that makes functional languages good at concurrency. Of course, in Elixir we do not access the value directly from a separate thread anyways, but instead pass a message. But fast immutable data structures have other valuable properties. For a great example of this, see the clojurescript project Om. Elixir allows us to rebind variables so we don’t get into the trap of :

a = 1

a1 = 2

a2 = 3

The difference between Elixir and a language with mutable data becomes more obvious when you do something like:

iex(1)> a = 1


iex(2)> a = a + 1


iex(3)> 2 = a


iex(4)> 3 = a

** (MatchError) no match of right hand side value: 2

Because = means “pattern match on these values and bind the value if possible to make the pattern match” instead of “assign this value to this variable” here, we can easily write 2 = a and it comes back with the value of a. This can be useful. On the other hand 3 = a is totally nonsense and we get a MatchError here. Again useful when we want to have behavior conditional on the value of a. This simple feature of binding variables to a value when possible, then pattern matching on them, is very fast due to the immutable data underneath, and allows to write some very terse, yet still easy to reason about, code.

  You must be logged in to comment