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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: