Pascal's triangle: Difference between revisions

Content added Content deleted
(add Clojure)
(Clojure)
Line 67: Line 67:
=={{header|Clojure}}==
=={{header|Clojure}}==


For n < 1, prints nothing, returns nil. Copied from the Common Lisp implementation above, but with local functions and explicity tail-call-optimized recursion (recur).
For n < 1, prints nothing, always returns nil. Copied from the Common Lisp implementation above, but with local functions and explicity tail-call-optimized recursion (recur).
(defn pascal [n]
(defn pascal [n]
(let [newrow (fn newrow [lst ret]
(let [newrow (fn newrow [lst ret]
(if lst
(if lst
(recur (rest lst) (conj ret (+ (first lst) (or (second lst) 0))))
(recur (rest lst)
(conj ret (+ (first lst) (or (second lst) 0))))
ret))
ret))
genrow (fn genrow [n lst]
genrow (fn genrow [n lst]
Line 77: Line 78:
(do (println lst)
(do (println lst)
(recur (dec n) (conj (newrow lst []) 1)))))]
(recur (dec n) (conj (newrow lst []) 1)))))]
(genrow n [1])))
(genrow n [1])))
(pascal 4)
(pascal 4)