Ex. 1.12 Structure and Interpretation of Computer Programs

Generating Pascal’s Triangle. Not the most optimal implementation.

; Ex 1.12 (define bin-coeffs (lambda (n k) (cond ((= k 0 ) 1) ((= k n ) 1) (else (+ (bin-coeffs (- n 1) (- k 1)) (bin-coeffs (- n 1) k))))))

(define pascals-triangle-row (lambda (n) (define pascals-triangle-row-iter (lambda (k coeff-list) (cond ((> k n ) coeff-list) (else (pascals-triangle-row-iter (+ k 1) (cons (bin-coeffs n k) coeff-list)))))) (pascals-triangle-row-iter 0 ‘())))

(define pascals-triangle (lambda (d) (define pascals-triangle&co (lambda (i col) (cond ((= i -1) col) (else (pascals-triangle&co (- i 1) (cons (pascals-triangle-row i) col )))))) (pascals-triangle&co d ‘())))

Advertisements

Like this:

LikeLoading...

Related

This entry was posted on Saturday, January 23rd, 2010 at 1:14 am and is filed under All Random Notes. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.