Iteration.html 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <html lang="en">
  2. <head>
  3. <title>Iteration - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Virtual-Code-Semantics.html#Virtual-Code-Semantics" title="Virtual Code Semantics">
  9. <link rel="prev" href="Predicates.html#Predicates" title="Predicates">
  10. <link rel="next" href="List-Combinators.html#List-Combinators" title="List Combinators">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Iteration"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="List-Combinators.html#List-Combinators">List Combinators</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Predicates.html#Predicates">Predicates</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Virtual-Code-Semantics.html#Virtual-Code-Semantics">Virtual Code Semantics</a>
  32. <hr>
  33. </div>
  34. <h4 class="subsection">2.7.12 Iteration</h4>
  35. <p><a name="index-recursion-314"></a><a name="index-g_t_0040code_007biterate_007d-315"></a>One of many alternatives to recursion provided by the virtual machine is
  36. iteration, which allows an operation to be repeated until a condition is
  37. met. If the source language is imperative, this feature provides an easy
  38. means of translating loop statements to virtual code. In languages that allow
  39. functions to be treated as data, iteration can be regarded as a function
  40. that takes a predicate and a function as arguments, and returns a
  41. function that applies the given function repeatedly to its argument
  42. until the predicate is refuted.
  43. <p>Iterative applications are expressed in virtual code by the pattern shown below.
  44. <dl>
  45. <dt><em>T21</em><dd>[[<code>iterate</code>]] <code>(</code><var>p</var><code>,</code><var>f</var><code>)</code> = <code>((nil,nil),(nil,(</code><var>p</var><code>,</code><var>f</var><code>)))</code>
  46. </dl>
  47. <p class="noindent">In the <code>silly</code> language, the <code>iterate</code> mnemonic plays the role
  48. of the function that takes the virtual code for a predicate
  49. <var>p</var> and a function <var>f</var> as arguments, and returns
  50. the virtual code for an iterating function.
  51. <p>The code for an iterating function is recognized as such by the virtual
  52. machine emulator only if the subtrees <var>f</var> and <var>p</var> are other
  53. than <code>nil</code>. The resulting function tests the argument
  54. <var>x</var> with <var>p</var> and returns <var>x</var> if the
  55. predicate is false.
  56. <dl>
  57. <dt><em>P22</em><dd>([[<code>iterate</code>]] <code>(</code><var>p</var><code>,</code><var>f</var><code>)</code>) <var>x</var> = <var>x</var> if <var>p</var> <var>x</var> = <code>nil</code>
  58. </dl>
  59. <p class="noindent">If the predicate turns out to be true, <var>f</var> is applied to
  60. <var>x</var>, and then another iteration is performed.
  61. <dl>
  62. <dt><em>P23</em><dd>([[<code>iterate</code>]] <code>(</code><var>p</var><code>,</code><var>f</var><code>)</code>) <var>x</var> =
  63. ([[<code>iterate</code>]] <code>(</code><var>p</var><code>,</code><var>f</var><code>)</code>) <var>f</var> <var>x</var> if <var>p</var> <var>x</var> is a non-<code>nil</code> tree
  64. </dl>
  65. </body></html>