Skip to content

Instantly share code, notes, and snippets.

@Arnot
Created September 21, 2017 11:21
Show Gist options
  • Save Arnot/f3bd9470f95d226b6cafaf8ab5527c3c to your computer and use it in GitHub Desktop.
Save Arnot/f3bd9470f95d226b6cafaf8ab5527c3c to your computer and use it in GitHub Desktop.
(defun catalan-next-row (v)
(if (or (null v)
(= (length v) 0))
[1]
(let ((r (make-vector (1+ (length v)) 1)))
(dotimes (x (length v))
(unless (= x 0)
(setf (aref r x)
(+ (aref v x)
(aref r (1- x))))))
(setf (aref r (length v))
(aref r (1- (length v))))
r)))
(defun catalan-triangle (nrows)
(let ((row [1]) result)
(dotimes (i nrows)
(setf row (catalan-next-row row))
(push row result))
(reverse result)))
(insert (prin1-to-string (catalan-triangle 10)))
;; ([1 1]
;; [1 2 2]
;; [1 3 5 5]
;; [1 4 9 14 14]
;; [1 5 14 28 42 42]
;; [1 6 20 48 90 132 132]
;; [1 7 27 75 165 297 429 429]
;; [1 8 35 110 275 572 1001 1430 1430]
;; [1 9 44 154 429 1001 2002 3432 4862 4862]
;; [1 10 54 208 637 1638 3640 7072 11934 16796 16796])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment