Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Created June 28, 2013 02:20
Show Gist options
  • Select an option

  • Save maxcountryman/5881993 to your computer and use it in GitHub Desktop.

Select an option

Save maxcountryman/5881993 to your computer and use it in GitHub Desktop.
(defn add-row [cnt acc]
(let [prev (last acc)]
(loop [n 0 row []]
(if (= n cnt)
row
(let [a (nth prev (- n 1) 0)
b (nth prev n 0)]
(recur (inc n) (conj row (if (zero? (+ a b)) 1 (+ a b)))))))))
(defn pascals-triangle [n]
(loop [cnt 1 acc []]
(if (> cnt n)
acc
(recur (inc cnt) (conj acc (add-row cnt acc))))))
;; => (pascals-triangle 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment