Skip to content

Instantly share code, notes, and snippets.

@agentultra
Created April 30, 2012 20:09
Show Gist options
  • Save agentultra/2562306 to your computer and use it in GitHub Desktop.
Save agentultra/2562306 to your computer and use it in GitHub Desktop.
A function to calculate n rows of Pascal's Triangle
(defun triangle (num)
"Generate num rows of Pascal's Triangle"
(labels ((next-row (row last-num)
(cond ((null row) '(1))
(t (cons (+ last-num (car row))
(next-row (cdr row) (car row))))))
(triangle-b (num prev-row)
(cond ((= num 0) '())
(t (cons prev-row
(triangle-b (1- num)
(next-row prev-row 0)))))))
(triangle-b num '(1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment