]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex1_12.clj
Merge branch 'master' of github.com:vu3rdd/sicp
[sicp.git] / src / sicp / ex1_12.clj
1 (ns sicp.ex1_12
2   (:use [sicp utils]
3         [clojure.contrib trace test-is]))
4
5 ;; exercise 1.12.  The following pattern of numbers is called Pascal's triangle.
6 ;;          1
7 ;;        1   1
8 ;;      1   2   1
9 ;;    1   3   3   1
10 ;;  1   4   6   4   1
11 ;; ...................
12 ;;
13 ;;                 The numbers at the edge of the triangle are all 1, and each
14 ;;                 number inside the triangle is the sum of the two numbers above
15 ;;                 it. Write a procedure that computes elements of Pascal's triangle
16 ;;                 by means of a recursive process. 
17 (defn pascal [row col]
18   (when (<= col row)
19     (if (or (= col 0) (= row col))
20       1
21       (+ (pascal (dec row) col)
22          (pascal (dec row) (dec col))))))