X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcode%2Flist.lisp;h=8bc2a885d098e73e9e30eb6220cb91bebcddc25f;hb=2db3b6b4cb740d5b6512459c223859f747807b09;hp=cacffe8ac08009401d882b27f986d422180d3717;hpb=b711554e4ce0dce883ba9e09a445c969aec0d305;p=sbcl.git diff --git a/src/code/list.lisp b/src/code/list.lisp index cacffe8..8bc2a88 100644 --- a/src/code/list.lisp +++ b/src/code/list.lisp @@ -257,45 +257,36 @@ ((zerop count) result) (declare (type index count)))) -;;; The outer loop finds the first non-null list and the result is -;;; started. The remaining lists in the arguments are tacked to the -;;; end of the result using splice which cdr's down the end of the new -;;; list. (defun append (&rest lists) #!+sb-doc "Construct a new list by concatenating the list arguments" - (flet ((fail (object) - (error 'type-error - :datum object - :expected-type 'list))) - (do ((top lists (cdr top))) ; CDR to first non-null list. - ((atom top) '()) - (cond ((null (car top))) ; NIL -> Keep looping - ((not (consp (car top))) ; Non CONS - (if (cdr top) - (fail (car top)) - (return (car top)))) - (t ; Start appending - (return - (if (atom (cdr top)) - (car top) ; Special case. - (let* ((result (cons (caar top) '())) - (splice result)) - (do ((x (cdar top) (cdr x))) ; Copy first list - ((atom x)) - (setq splice - (cdr (rplacd splice (cons (car x) ()) ))) ) - (do ((y (cdr top) (cdr y))) ; Copy rest of lists. - ((atom (cdr y)) - (setq splice (rplacd splice (car y))) - result) - (if (listp (car y)) - (do ((x (car y) (cdr x))) ; Inner copy loop. - ((atom x)) - (setq - splice - (cdr (rplacd splice (cons (car x) ()))))) - (fail (car y)))))))))))) + (labels ((fail (object) + (error 'type-error + :datum object + :expected-type 'list)) + (append-into (last-cons current rest) + "Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST)." + (declare (cons last-cons rest)) + (cond ((consp current) + (append-into (setf (cdr last-cons) (list (car current))) + (cdr current) + rest)) + ((not (null current)) (fail current)) + ((null (cdr rest)) (setf (cdr last-cons) (car rest))) + (t (append-into last-cons (car rest) (cdr rest))))) + (append1 (lists) + (let ((current (car lists)) + (rest (cdr lists))) + (cond ((null rest) current) + ((consp current) + (let ((result (truly-the cons (list (car current))))) + (append-into result + (cdr current) + rest) + result)) + ((null current) (append1 rest)) + (t (fail current)))))) + (append1 lists))) ;;;; list copying functions