Skip to content

Instantly share code, notes, and snippets.

@vedang
Created July 10, 2011 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vedang/1074686 to your computer and use it in GitHub Desktop.
Save vedang/1074686 to your computer and use it in GitHub Desktop.
printing Pascal's Triangle.
(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