Skip to content

Instantly share code, notes, and snippets.

@thisiswei
Created May 10, 2013 17:51
Show Gist options
  • Save thisiswei/5556139 to your computer and use it in GitHub Desktop.
Save thisiswei/5556139 to your computer and use it in GitHub Desktop.
pascals-triangle
(define (expand-row p)
(cons (car p) (expand-row-rest p)))
(define (expand-row-rest p)
(if (null? (cdr p)) (list 1)
(cons (+ (car p) (cadr p))
(expand-row-rest (cdr p)))))
(define (pascals-row n)
(if (= n 0) (list 1)
(expand-row (pascals-row (- n 1)))))
(define (pascals-triangle n)
(define (pas-helper n)
(if (= n 0) (list (pascals-row 0))
(cons (pascals-row n) (pas-helper (- n 1)))))
(reverse (pas-helper n)))
@thisiswei
Copy link
Author

a. (expand- row (list 1)) evaluates to (1 1); (expand-row (list 1 1)) evaluates to (1 2 1); and (expand-row (list 1 4 6 4 1)) evaluates to (1 5 10 10 5 1).

b. (pascals-triangle-row 0) evaluates to (1) and (pascals-triangle-row 3) produces (1 3 3 1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment