about summary refs log blame commit diff stats
path: root/subx/050_write.subx
blob: 0d6b8152b0b45f2d40bf3039addd38862b94f76e (plain) (tree)
1
2
3
4
5
6
7
8
9
- --> </script> </head> <body> <pre id='vimCodeElement'> <span class="Comment"># example program: compute the factorial of 5</span> <span class="muRecipe">recipe</span> main [ <span class="Constant">local-scope</span> x:number<span class="Special"> &lt;- </span>factorial <span class="Constant">5:literal</span> $print <span class="Constant">[result: ]</span>, x:number, <span class="Constant">[ </span> <span class="Constant">]</span> ] <span class="muRecipe">recipe</span> factorial [ <span class="Constant">local-scope</span> n:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span> <span class="Delimiter">{</span> <span class="Comment"># if n=0 return 1</span> zero?:boolean<span class="Special"> &lt;- </span>equal n:number, <span class="Constant">0:literal</span> <span class="muControl">break-unless</span> zero?:boolean <span class="muControl">reply</span> <span class="Constant">1:literal</span> <span class="Delimiter">}</span> <span class="Comment"># return n * factorial(n-1)</span> x:number<span class="Special"> &lt;- </span>subtract n:number, <span class="Constant">1:literal</span> subresult:number<span class="Special"> &lt;- </span>factorial x:number result:number<span class="Special"> &lt;- </span>multiply subresult:number, n:number <span class="muControl">reply</span> result:number ] <span class="Comment"># unit test</span> <span class="muScenario">scenario</span> factorial-test [ run [ 1:number<span class="Special"> &lt;- </span>factorial <span class="Constant">5:literal</span> ] memory-should-contain [ 1<span class="Special"> &lt;- </span>120 ] ] </pre> </body> </html> <!-- vim: set foldmethod=manual : -->