Factorial: Difference between revisions
Content added Content deleted
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag) |
WillBAnders (talk | contribs) |
||
Line 8,598: | Line 8,598: | ||
===Iterative=== |
===Iterative=== |
||
Standard iterative solution using a <code>for</code> loop: |
Standard iterative solution using a <code>for</code> loop: |
||
* <code>range(2, |
* <code>range(2, num, :incl)</code> creates an inclusive range (<code>2 <= i <= num</code>) for iteration |
||
<syntaxhighlight lang="scala"> |
<syntaxhighlight lang="scala"> |
||
Line 8,613: | Line 8,613: | ||
===Recursive=== |
===Recursive=== |
||
Standard recursive solution using a pattern matching approach: |
Standard recursive solution using a pattern matching approach: |
||
* <code>require num >= 0;</code> enforces argument preconditions |
|||
* <code>match</code> without arguments is a ''conditional match'', which works like <code>if/else</code> chains. |
* <code>match</code> without arguments is a ''conditional match'', which works like <code>if/else</code> chains. |
||
* Rhovas doesn't perform tail-call optimization yet, hence why this solution isn't tail recursive. |
* Rhovas doesn't perform tail-call optimization yet, hence why this solution isn't tail recursive. |