Factorial in Lisp
Example for versions
Clojure 1.0.0,
Clojure 1.1.0
This example uses recursive factorial definition. range
with one argument generates a list of numbers from 0 (inclusive) to this number (exclusive). str
concatenates strings. dec
is decrement, equivalent to (- x 1)
. doseg
is Clojure-style loop.
(defn factorial [x]
(if (< x 2)
1
(* x (factorial (dec x)))))
(doseq [i (range 17)]
(println (str (str i "! = ") (factorial i))))
Comments
]]>blog comments powered by Disqus
]]>