<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="248897" data-post-id="248897">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sbuttgereit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/120/30162_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sbuttgereit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Maybe something more along the lines of this will help:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">a = [:bears, :elephants]
b = a
a = Enum.reject(a, fn animal -&gt; animal == :bears end)
IO.inspect(a)
IO.inspect(b)
</code></pre>
<p>So we make <code>a</code> point to some value, and then make <code>b</code> point to the same thing as <code>a</code>.  Then we call  <code>Enum.reject</code> the result of which <code>a</code> gets rebound to.  The reject call creates a new list based on what <code>a</code> was originally bound to: it cannot modify what <code>a</code> was originally bound to since that original list is immutable.  <code>b</code> is still bound to the original list that <code>a</code> was originally bound to.  Since that original list is immutable, we can make assumptions about <code>b</code> without concern with what happens to <code>a</code> later on.  <code>a</code> itself may be rebound, but that original list of <code>[:bears, :elephants]</code> is not changed as a result.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="248897" data-batch-url="/posts/batch_likers">
                        11
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-248897" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="248897"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="248900" data-post-id="248900">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Love these examples <a class="mention" href="/u/hauleth" rel="nofollow">@hauleth</a> and <a class="mention" href="/u/sbuttgereit" rel="nofollow">@sbuttgereit</a>.  I have a good grasp on this but never know how to explain it to people in a way they’ll accept.  I think these will help—thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="248900" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-248900" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="248900"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="248913" data-post-id="248913">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ityonemo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/120/11341_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ityonemo
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Different languages have different meanings for things.</p>
<p>I’m most comfortable with julia’s definitions, where they make a distinction between <code>variable</code> and <code>value</code>.  The <code>variable</code> is the lexical representation in code, and <code>value</code> is the value that the machine sees (the stuff in memory, if you will).</p>
<p>In this lexicon (which I think corresponds more to what we typically mean in math and CS), in Elixir, the <code>variable</code> is mutable but the <code>value</code> is immutable.  You can ‘point’ your variable at a different value (“rebinding”), but this does not change the underlying <code>value</code>s.  If a different <code>variable</code> is holding onto the underlying <code>value</code>, then it is unaffected by the rebinding event.</p>
<p>Note that in the Elixir docs and the elixir community, we have a different meaning of <code>variable</code>, hopefully pointing this out will help you instead of confusing you.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="248913" data-batch-url="/posts/batch_likers">
                        11
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-248913" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="248913"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="249089" data-post-id="249089">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="smathy" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/smathy/120/20941_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  smathy
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>In a nutshell: you can repoint <code>x</code> to a new value, but you can’t change the value <code>x</code> is currently pointing at.</p>
<p>Integers are not generally mutable in any language, so this is not a great example to demonstrate Elixir’s immutability with.  Something that’s very mutable in other languages are collections, but in Elixir you cannot mutate a collection.  Ie. in Elixir there’s no way to push a new value onto an existing list/tuple/map, instead you are always creating a new collection.  Eg.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(22)&gt; x = y = { 1, 2 }
{1, 2}
iex(23)&gt; x = Tuple.insert_at(x, 0, :foo)
{:foo, 1, 2}
iex(24)&gt; x
{:foo, 1, 2}
iex(25)&gt; y
{1, 2}
</code></pre>
<p>Notice how we can reassign the new tuple we created with the new element inserted to <code>x</code>, but the tuple <code>x</code> had originally been pointing at (also assigned to <code>y</code>) remained unchanged.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="249089" data-batch-url="/posts/batch_likers">
                        6
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-249089" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="249089"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="249248" data-post-id="249248">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="highmountaintea" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/highmountaintea/120/25545_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  highmountaintea
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Sometimes it’s easier to work it backwards.</p>
<p>So first accept that Elixir is immutable, which means variables cannot change, and values and data structures cannot change. Then work back from there.</p>
<p>So in your example, <code>x = 1</code> and <code>x = 2</code> seems contrary to immutability. But like others said, it is because these are actually two different variables.</p>
<p>The consequence is that you cannot do typical counting/summing using mutable methods, e.g.:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">// This typical JavaScript way of summing would not work in Elixir due to immutability

function sumAll(list) {
  let sum = 0;
  for (let item of list) sum += item;
  return sum;
}
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="249248" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-249248" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="249248"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="249282" data-post-id="249282">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="qasim.regbits" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qasim.regbits/120/25390_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  qasim.regbits
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi,<br>
The concept of Immutability in Elixir is value of any variable(in memory) will never modified.<br>
For example,<br>
x = 1<br>
x = 2<br>
When we print x, it’ll print 2. It doesn’t means x modified its value but in memory new value(2) allocate to variable x.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="249282" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-249282" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="249282"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="249409" data-post-id="249409">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sdball" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sdball/120/24090_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sdball
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Think of a variable as a label applied to a value. The label can be pulled off and placed on some other value; but the value itself cannot change. That is a fundamental property of the Erlang VM.</p>
<p>You can think of the <code>=</code> operator as the label operator. It’s not copying the value into a box for the variable name: the variable name is pointing to the actual value.</p>
<p>Check out this code</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">x = 1
y = 1
</code></pre>
<p>In that code x and y aren’t holding two copies of <code>1</code>: they are each pointing to the <em>same</em> immutable value of <code>1</code>. When you “relabel” <code>x = 2</code> you are pulling the “x” label off of the value 1 and applying it to the value 2.</p>
<p>Maybe using strings will make it more clear.</p>
<pre data-code-wrap="iex"><code class="lang-iex">&gt; x = "abc"
"abc"

&gt; x &lt;&gt; "def"
"abcdef"

&gt; x
"abc"
</code></pre>
<p>When we concatenated “def” onto the variable “x” we did not change its value and we did not reapply the label “x” to a new value. Hence x still points to the same immutable value “abc”</p>
<p>If we reapply the label “x” to a new value</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">x = x &lt;&gt; "def"
</code></pre>
<p>Then the original value of “abc” is still unchanged and immutable. But we’ve pulled the label x off its original value and applied it to the new result.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="249409" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-249409" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="249409"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="249432" data-post-id="249432">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="eksperimental" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/120/1401_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  eksperimental
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Understanding variable scope is also crucial.</p>
<p>This used to be part of the Elixir guides:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://elixir-lang.readthedocs.io/en/latest/technical/scoping.html">
  <header class="source">

      <a href="https://elixir-lang.readthedocs.io/en/latest/technical/scoping.html" target="_blank" rel="noopener nofollow ugc">elixir-lang.readthedocs.io</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://elixir-lang.readthedocs.io/en/latest/technical/scoping.html" target="_blank" rel="noopener nofollow ugc">Scoping Rules in Elixir (and Erlang) — Elixir  documentation</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I think what has changed is the scope in <code>if/2</code>, the rest stayed the same.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="249432" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-249432" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="249432"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="347743" data-post-id="347743">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="nature" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nature/120/27337_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  nature
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Part of Immutability in Elixir is that functions are pure, they don’t modify arguments or variable in place. I’m going to do a quick contrast with Javascript.</p>
<p><code>const numbers = [4, 2, 3, 1, 23, 8, 9] numbers.sort((a, b) =&gt; a - b) console.log(numbers) // This operation has modified the numbers variable and sorted the array in ascending order. </code></p>
<p>But this is how Elixir will handle this type of problem.</p>
<p><code>numbers = [4, 2, 3, 1, 23, 8, 9] sorted_numbers = Enum.sort(numbers, &amp;(&amp;1 &lt;= &amp;2)) </code></p>
<p>After running the sort function on the variable, the value assigned to numbers variable is still the same. Elixir without modifying the numbers list just returns the sorted list which can be assigned to a new variable .</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="347743" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-347743" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="347743"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
    <div class="postbit" id="367452" data-post-id="367452">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="New2Elixir" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  New2Elixir
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi All, new to Elixir and still rather confused about Elixir/Erlang’s immutability despite the numerous attempts to explain it.</p>
<p>As a non-pro, the claim that variables are immutable appears on its face to be a stretch or just false.<br>
A simple look at any dictionary will generally all give the same definition of immutability.</p>
<p>If I can do<br>
X=1<br>
X=2<br>
Print X = 2 printed, then its not actually immutable.<br>
Now it seems as though the common reply is 'But…", and an explanation of rebinding/resetting.<br>
So assuming that is true, what is the rationale for calling this behaviour out prominently as ‘immutable’ when it actual reality from programmers perspective it is mutuable?<br>
Is this some sort of inside baseball FP bona fide that needed to be included for some reason?  Not trying to be snarky, but saying a variable can not change when it actually can as per what seems to be normal operation, regardless of whats happening below the surface, just seems incongruent.</p>
<p>I thought I read somewhere where doing as above does change X, whereas trying to chage X in the middle of an If statement did not.<br>
Is there a difference between a simple statement x=1 then another with x=2 vs simple statement x=1 but an attempt to modify x in an expression like if something x=2, or perhaps better explained, a non-simple x= statement?<br>
I think I just made myself more confused.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="367452" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-would-you-explain-elixir-immutability/47323/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-367452" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="367452"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/47323/load_more?page=3">Load more posts (20 remaining)</a>
</div></template></turbo-stream>