P07: ツリーを単純リストに展開する。

consだったら再帰で木を下る。
結果はaccに溜めていって、最後にreverse。

;;;
;; P07 (**) Flatten a nested list structure.
;; Transform a list, possibly holding lists as elemnts into a 'flat' list by
;; Replacing each list with its elements (recursively).
;; Example:
;; * (my-flatten '(a (b (c d) e)))
;; (A B C D E)
(defun my-flatten (list)
  (labels
      ((my-flatten-aux (l acc)
		       (cond
			((null l) acc)
			((consp (car l))
			 (my-flatten-aux (cdr l) (my-flatten-aux (car l) acc)))
			(t
			 (my-flatten-aux (cdr l) (cons (car l) acc))))))
    (reverse (my-flatten-aux list '()))))


(my-flatten '(a b c d e))
;; => (A B C D E)
(my-flatten '(a (b (c d) e)))
;; => (A B C D E)
(my-flatten '(nil (nil (nil nil) nil)))
;; => (NIL NIL NIL NIL NIL)
(my-flatten '())
;; => NIL
(my-flatten '(a))
;; => (A)
(my-flatten '(((((((((nil))))))))))
;; => (NIL)