Skip to content

Instantly share code, notes, and snippets.

@buntine
Created February 10, 2010 06:53
Show Gist options
  • Save buntine/300085 to your computer and use it in GitHub Desktop.
Save buntine/300085 to your computer and use it in GitHub Desktop.
;;; 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