;; demonstrates functional thinking in Common Lisp (defun combinations ( alist n &optional (list-so-far nil) ) (cond ((= (length list-so-far) n) (print (reverse list-so-far))) ((null alist) nil) (t (combinations (rest alist) n (cons (first alist) list-so-far)) (combinations (rest alist) n list-so-far) ) ) ) ;; demonstrates rest recursion (defun my-length ( alist ) (cond ((null alist) 0) (t (+ 1 (my-length (rest alist))) ) ) ) ;; demonstrates first rest recursion (defun count-all ( alist ) (cond ((null alist) 0) ((atom alist) 1) (t (+ (count-all (first alist)) (count-all (rest alist))) ) ) ) ;; demonstrates first rest recursion, developed with help of students (defun flatten (alist) (cond ((null alist) nil) ((atom alist) (list alist)) (t (append (flatten (first alist)) (flatten (rest alist))) ) ) )