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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Correct me if I’m wrong (It’s been a few years since my last comp sci course) but aren’t while loops in most languages just synaptic sugar for recursive functions. By which I mean that they compile down to the same code and aren’t functionally much different.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92781" 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/write-while-loop-equivalent-in-elixir/15880/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-92781" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92781"
                     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="92783" data-post-id="92783">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Zesky665" data-post="11" data-topic="15880" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zesky665/48/9525_2.png" class="avatar"> Zesky665:</div>
<blockquote>
<p>You’re right, I always forget that you can just leave a function empty and it won’t cause an error.</p>
</blockquote>
</aside>
<p>Specifically it returns the atom <code>nil</code>.</p>
<aside class="quote no-group" data-username="Zesky665" data-post="12" data-topic="15880" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zesky665/48/9525_2.png" class="avatar"> Zesky665:</div>
<blockquote>
<p>Correct me if I’m wrong (It’s been a few years since my last comp sci course) but aren’t while loops in most languages just synaptic sugar for recursive functions. By which I mean that they compile down to the same code and aren’t functionally much different.</p>
</blockquote>
</aside>
<p>Depends on the back-end, if the back-end is, say LLVM, then it compiles to something that looks recursive but it bases it on blocks instead of function calls, similar concept though.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92783" 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/write-while-loop-equivalent-in-elixir/15880/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-92783" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92783"
                     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="92784" data-post-id="92784">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Zesky665" data-post="12" data-topic="15880">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zesky665/48/9525_2.png" class="avatar"> Zesky665:</div>
<blockquote>
<p>while loops in most languages just synaptic sugar for recursive functions.</p>
</blockquote>
</aside>
<p>Going down to <a href="https://stackoverflow.com/questions/23595183/while-in-x86-64-assembly" rel="noopener nofollow ugc">assembly language</a> a loop often has a test, conditional jump, body and unconditional jump - which really is iterative.</p>
<p>Also keep in mind that iteration doesn’t require a stack frame and only tail call (<a href="http://erlang.org/pipermail/erlang-questions/2016-October/090663.html" rel="nofollow">last call</a> optimized) recursion can reuse a stack frame while <em>body recursion</em> consumes a stack frame on each call.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92784" 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/write-while-loop-equivalent-in-elixir/15880/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-92784" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92784"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="92785" data-post-id="92785">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="peerreynders" data-post="14" data-topic="15880">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>Going down to <a href="https://stackoverflow.com/questions/23595183/while-in-x86-64-assembly" rel="noopener nofollow ugc">assembly language</a> a loop often has a test, conditional jump, body and unconditional jump - which really is iterative.</p>
</blockquote>
</aside>
<p>LLVM has that as well except you can only reference bindings defined before, and bindings are immutable, so you have to allocate memory somewhere (stack is common) to have mutable memory.  <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92785" 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/write-while-loop-equivalent-in-elixir/15880/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-92785" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92785"
                     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="92786" data-post-id="92786">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Zesky665" data-post="12" data-topic="15880" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zesky665/48/9525_2.png" class="avatar"> Zesky665:</div>
<blockquote>
<p>Correct me if I’m wrong (It’s been a few years since my last comp sci course) but aren’t while loops in most languages just synaptic sugar for recursive functions. By which I mean that they compile down to the same code and aren’t functionally much different.</p>
</blockquote>
</aside>
<p>Regardless of what it compiles down to, iteration is a special case, recursion is more general.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92786" 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/write-while-loop-equivalent-in-elixir/15880/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-92786" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92786"
                     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="92805" data-post-id="92805">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Zesky665" data-post="12" data-topic="15880" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zesky665/48/9525_2.png" class="avatar"> Zesky665:</div>
<blockquote>
<p>Correct me if I’m wrong (It’s been a few years since my last comp sci course) but aren’t while loops in most languages just synaptic sugar for recursive functions. By which I mean that they compile down to the same code and aren’t functionally much different.</p>
</blockquote>
</aside>
<p>The major difference (from the developer’s point of view) between a while loop and a recursive function is that in every other language a recursive function allocates a new stack frame and therefore has a limit to how many times it can be recursively executed. In Elixir this is not true because a stack frame is not allocated due to tail recursion. That is why a while loop is not necessary in Elixir.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92805" 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/write-while-loop-equivalent-in-elixir/15880/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-92805" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92805"
                     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="92840" data-post-id="92840">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="mgwidmann" data-post="17" data-topic="15880">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mgwidmann/48/7249_2.png" class="avatar"> mgwidmann:</div>
<blockquote>
<p>…in every other language…</p>
</blockquote>
</aside>
<p>In <em>some</em> other languages–you might be surprised how few. For starters, basically all functional languages have tail call optimization (without explicit looping constructs built in they have to), and that includes the JVM-based ones like Scala &amp; Closure. But also C, C++, C# (actually all .NET languages because the JIT does it), Objective-C, Javascript, Kotlin…</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92840" 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/write-while-loop-equivalent-in-elixir/15880/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-92840" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92840"
                     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="92847" data-post-id="92847">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group quote-modified" data-username="sribe" data-post="18" data-topic="15880">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/439d5e/48.png" class="avatar"> sribe:</div>
<blockquote>
<p>JVM-based ones like Scala &amp; Clojure</p>
</blockquote>
</aside>
<p>The JVM itself <em>doesn’t</em> TCO - so it’s up to the language to fake it.</p>
<p>Clojure in particular has <a href="https://clojuredocs.org/clojure.core/recur" rel="noopener nofollow ugc"><code>recur</code></a> to support <em>iteration</em> - it essentially replaces the data in a “recursion frame” before reentering the body of a loop; it’s an iterative loop based on the concept of tail recursive calls (probably one of the best examples of how iteration and recursion are two sides of the same coin).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92847" 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/write-while-loop-equivalent-in-elixir/15880/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-92847" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92847"
                     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="92848" data-post-id="92848">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yep, that’s why Java wasn’t on my list <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92848" 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/write-while-loop-equivalent-in-elixir/15880/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-92848" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92848"
                     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="92850" data-post-id="92850">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ultimately it depends on how determined you are:</p>
<p><a href="https://pragprog.com/book/vsjava8/functional-programming-in-java" rel="noopener nofollow ugc">Functional Programming in Java (Pragprog)</a></p>
<p>p.123 - Chapter 7: Optimizing Recursions - Turning to Tail Recursion</p>
<p><a href="https://media.pragprog.com/titles/vsjava8/code/recur/fpij/Factorial.java" class="onebox" target="_blank" rel="noopener nofollow ugc">https://media.pragprog.com/titles/vsjava8/code/recur/fpij/Factorial.java</a></p>
<p><a href="https://media.pragprog.com/titles/vsjava8/code/recur/fpij/TailCall.java" class="onebox" target="_blank" rel="noopener nofollow ugc">https://media.pragprog.com/titles/vsjava8/code/recur/fpij/TailCall.java</a></p>
<p><a href="https://media.pragprog.com/titles/vsjava8/code/recur/fpij/TailCalls.java" class="onebox" target="_blank" rel="noopener nofollow ugc">https://media.pragprog.com/titles/vsjava8/code/recur/fpij/TailCalls.java</a></p>
<p><a href="https://www.google.com/search?tbm=bks&amp;hl=en&amp;q=import+fpij.tailcalls.call" class="inline-onebox" rel="noopener nofollow ugc">Google Search</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="92850" 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/write-while-loop-equivalent-in-elixir/15880/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-92850" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="92850"
                     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>
</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/15880/load_more?page=3">Load more posts (16 remaining)</a>
</div></template></turbo-stream>