Created
June 28, 2013 02:20
-
-
Save maxcountryman/5881993 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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