From: Vladimir Sedach Date: Mon, 8 Jun 2009 04:35:05 +0000 (-0600) Subject: Fixed 'flatten' to correctly handle lists with non-nil cdr (ie '(1 . 2)). X-Git-Url: https://git.hcoop.net/clinton/parenscript.git/commitdiff_plain/604b5bbea9dd9eff7a21a59f2236f0d9374a13db Fixed 'flatten' to correctly handle lists with non-nil cdr (ie '(1 . 2)). Thanks to Scott Bell for the bug report. http://common-lisp.net/pipermail/parenscript-devel/2009-June/000528.html --- diff --git a/src/utils.lisp b/src/utils.lisp index de77d42..9047abb 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -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)))))