Created
February 10, 2010 06:53
-
-
Save buntine/300085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Calculates all elements of Pascal's Triangle up to a particular width/row. | |
(define (pascal width) | |
(reverse (pascal-helper '((1)) width))) | |
(define (pascal-helper rows width) | |
(if (= (length rows) width) | |
rows | |
(pascal-helper (cons (next-row (car rows)) | |
rows) | |
width))) | |
(define (next-row previous) | |
(let ((body (row-body '() previous))) | |
(if (null? body) | |
'(1 1) | |
(cons 1 (append body '(1)))))) | |
(define (row-body body previous) | |
(if (null? (cdr previous)) | |
body | |
(row-body (cons (+ (car previous) (cadr previous)) | |
body) | |
(cdr previous)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment