3 ms·
The modern version of this function, from On Lisp: (defun flatten (x) (labels ((rec (x acc) (cond ((null x) acc) ((atom x) (cons x acc))
by daniel-cussen 16y ago
The modern version of this function, from On Lisp:
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec (car x) (rec (cdr x) acc))))))
(rec x nil)))
- fogus 16y agoMy favorite (although not the most efficient) Clojure version of this is: (defn flat [s] (if (sequential? s) (apply concat (map flat s)) (list s))) (flat '((((1 2 3)) (4 5)) (((((((6 7 [8 9])))))) a b c) {:a 1 :b 2})) ;=> (1 2 3 4 5 6 7 8 9 a b c {:a 1, :b 2})
- lispm 16y agoCommon Lisp has MAPCAN: (defun flatten (list) (mapcan (lambda (item) (if (listp item) (flatten item) (list item))) list))