SICP习题 1.12 (帕斯卡三角的递归与迭代)

(define (pascal row col)
(cond ((or (< col 0) (< row 0))
(display"behave yourself!"))
((> col row)
(display"behave yourself!"))
((or (= col 0) (= col row))
1)
(else (+ (pascal (- row 1) (- col 1))
(pascal (- row 1) col)))))

(pascal 4 -3)

 

 

(define (factorial n)
(fact-iter 1 1 n))

(define (fact-iter a i n)
(if (> i n)
a
(fact-iter (* a i)
(+ i 1)
n)))
;(factorial 3)
(define (pascal row col)
(/ (factorial row)
(* (factorial col)
(factorial (- row col)))))

(pascal 1024 256)

 

posted @ 2017-11-23 01:15  R4mble  阅读(235)  评论(0编辑  收藏  举报