Factorial: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Clojure}}: added trampolining solution, updated all solutions to support arbitrary precision) |
||
Line 2,968: | Line 2,968: | ||
=== Folding === |
=== Folding === |
||
<syntaxhighlight lang="lisp">(defn factorial [x] |
<syntaxhighlight lang="lisp">(defn factorial [x] |
||
(apply * (range 2 (inc x))))</syntaxhighlight> |
(apply *' (range 2 (inc x))))</syntaxhighlight> |
||
=== Recursive === |
=== Recursive === |
||
Line 2,974: | Line 2,974: | ||
(if (< x 2) |
(if (< x 2) |
||
1 |
1 |
||
(* x (factorial (dec x)))))</syntaxhighlight> |
(*' x (factorial (dec x)))))</syntaxhighlight> |
||
=== Tail recursive === |
=== Tail recursive === |
||
Line 2,982: | Line 2,982: | ||
(if (< x 2) |
(if (< x 2) |
||
acc |
acc |
||
(recur (dec x) (* acc x)))))</syntaxhighlight> |
(recur (dec x) (*' acc x)))))</syntaxhighlight> |
||
=== Trampolining === |
|||
<syntaxhighlight lang="lisp">(defn factorial |
|||
([x] (trampoline factorial x 1)) |
|||
([x acc] |
|||
(if (< x 2) |
|||
acc |
|||
#(factorial (dec x) (*' acc x)))))</syntaxhighlight> |
|||
=={{header|CLU}}== |
=={{header|CLU}}== |