Created
July 10, 2011 16:46
-
-
Save vedang/1074686 to your computer and use it in GitHub Desktop.
printing Pascal's Triangle.
This file contains 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 pascal-val | |
"function to calculate value of element at a particular row and column | |
for the pascal triangle." | |
[r c] | |
(cond | |
(= c 0) 1 | |
(= c r) 1 | |
:else (+ (pascal-val (- r 1) (- c 1)) | |
(pascal-val (- r 1) c)) )) | |
(defn pascal-row | |
"calculate a row of the pascal triangle" | |
[r] | |
(map (fn [x] | |
(pascal-val r x)) | |
(range (+ r 1)))) | |
(defn pascal-triangle | |
[i h] | |
(println (pascal-row i)) | |
(cond | |
(= h 0) nil | |
:else (pascal-triangle (+ i 1) (- h 1)))) | |
(defn print-pascal-triangle | |
"print pascal triangle of height h" | |
[h] | |
(pascal-triangle 0 h)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment