Skip to main content
Source Link

Common Lisp:

This doesn't print them, but does produce a list of lists of all the possible structures. My method is a bit different from the others'. It restructures the solutions to brackets(n - 1) such that they become brackets(n). My solution isn't tail recursive, but it could be made so with a little work.

Code

(defun brackets (n) (if (= 1 n) '((())) (loop for el in (brackets (1- n)) when (cdr el) collect (cons (list (car el)) (cdr el)) collect (list el) collect (cons '() el))))