Fixed 'flatten' to correctly handle lists with non-nil cdr (ie '(1 . 2)).
authorVladimir Sedach <vsedach@gmail.com>
Mon, 8 Jun 2009 04:35:05 +0000 (22:35 -0600)
committerVladimir Sedach <vsedach@gmail.com>
Mon, 8 Jun 2009 04:35:05 +0000 (22:35 -0600)
Thanks to Scott Bell for the bug report.
http://common-lisp.net/pipermail/parenscript-devel/2009-June/000528.html

src/utils.lisp

index de77d42..9047abb 100644 (file)
@@ -99,7 +99,7 @@ SOMEGLOBAL."
           ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
                 ,@body)))))
 
-(defun flatten (x)
-  (if (atom x)
-      (list x)
-      (mapcan #'flatten x)))
+(defun flatten (x &optional acc)
+  (cond ((null x) acc)
+        ((atom x) (cons x acc))
+        (t (flatten (car x) (flatten (cdr x) acc)))))