title: The while language
body: <p>For my theory of computing languages class, we got a homework assignment to implement a piece of code in a language that only has while statements for flow control (no if statements).  This is mainly to prove that you can write a Turing-complete language with only a while loop.</p>
  
  <p>For those of you who can understand language grammars, here are the language rules:</p>
  
  <pre><code>S -&gt; S;S | while C do S od | id := E
  
  E -&gt; E + T | T | E - T
  
  T -&gt; T * F | F | F / T
  
  F -&gt; id | cons | (E)
  
  C -&gt; E = E | E &gt; E | E &lt; E | E &gt;= E | E &lt;= E | E != E | C and C | C or C | not(C)
  </code></pre>
  
  <p>This is copied from my class notes, so don't blame me if something is missing or incorrect!</p>
  
  <p>The piece of code to implement is this:</p>
  
  <pre><code>if d = 0 do
      x := 1
  else
      x := a / d
  </code></pre>
  
  <p>At any rate, if you want to go ahead and write that using the language rules above, go ahead.  Otherwise, go ahead and write it in whatever language you're most comfortable in.  But there are a few caveats!</p>
  
  <ul>
  <li>No if statements or any other kind of flow control other than while loops.</li>
  <li>No cheating:  the grammar above doesn't include any break statements, return statements, or exceptions.  Don't use them.</li>
  </ul>
  
  <p>I've got my piece of code written for this (which I'll post just to prove this isn't a show me teh codez post).  I'm kinda curious what anyone else can come up with though.</p>