# Immutability simplified

**URL:** <https://forum.elixirforum.com/t/immutability-simplified/9351>\
**Category:** Questions\
**Tags:** immutability\
**Created:** [October 14, 2017, 5:38pm UTC](https://forum.elixirforum.com/t/immutability-simplified/9351 "2017-10-14T17:38:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vals](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/v/8e8cbc/32.png) [@vals](https://forum.elixirforum.com/u/vals)\
**Post date:** [October 14, 2017, 5:38pm UTC](https://forum.elixirforum.com/t/immutability-simplified/9351/1 "2017-10-14T17:38:36Z")

</div>

```
iex(5)> list1 = [1,2,3]
[1, 2, 3]
iex(6)> list2 = [4 | list1]
[4, 1, 2, 3]

```

I am trying to imagine how immutability works in Elixir (and in other functional languages).  
When line 6 is executed there will be no new list data structure created but both `list1` and `list2` will refer to the same underline data structure. Is it correct assumtion???

 ![](https://forum.elixirforum.com/uploads/default/original/2X/6/63b874c15e4aa68ad143777992e04a64f5cce21a.jpg)

---

<div class="post-metadata">

**Author:** ![gregvaughn](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregvaughn/32/954_2.png) [@gregvaughn](https://forum.elixirforum.com/u/gregvaughn)\
**Post date:** [October 14, 2017, 5:41pm UTC](https://forum.elixirforum.com/t/immutability-simplified/9351/2 "2017-10-14T17:41:14Z")

</div>

Yes. In your example you’ll have a single linked list in memory, but the different variables will point to different head elements of it. Since it is a singly linked list there is no way from the `list1` variable to ever go “backwards” and see element “4”.

In a general hand-wavey sense, this is how a lot of data structures work in immutable languages. You basically have one large/combined structure and different variables have different visibility to parts of it.

---

<div class="post-metadata">

**Author:** ![vals](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/v/8e8cbc/32.png) [@vals](https://forum.elixirforum.com/u/vals)\
**Post date:** [October 16, 2017, 4:53pm UTC](https://forum.elixirforum.com/t/immutability-simplified/9351/3 "2017-10-16T16:53:45Z")

</div>

However, ignoring the under-the-hood implementation, the end user of the language should be under the impression that a brand new copy of the list is created. The particular implementation shown is just a trick for the sake of efficiency.

---

<div class="post-metadata">

**Author:** ![Joseph](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/joseph/32/5609_2.png) [@Joseph](https://forum.elixirforum.com/u/Joseph)\
**Post date:** [October 18, 2017, 4:46pm UTC](https://forum.elixirforum.com/t/immutability-simplified/9351/4 "2017-10-18T16:46:44Z")

</div>

I’m not so sure that it’s about _copying_ as much as it is that immutability gives us the effect that it’s _like_ copying. As you say, it’s like that underneath for efficiency, but the interesting thing is that if we have immutable data then we don’t _have_ to copy underneath: we know that pointing to different parts of the data (from different variables) won’t give us a different value at different times.
