Skip to content
Snippets Groups Projects
Commit 5af76d1a authored by pw's avatar pw
Browse files

Fix butlast and nbutlast to be correct for NIL argument.

parent d1d6f622
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
;;; Carnegie Mellon University, and has been placed in the public domain. ;;; Carnegie Mellon University, and has been placed in the public domain.
;;; ;;;
(ext:file-comment (ext:file-comment
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/list.lisp,v 1.22 1998/05/09 22:16:45 pw Exp $") "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/list.lisp,v 1.23 2000/11/15 11:36:34 pw Exp $")
;;; ;;;
;;; ********************************************************************** ;;; **********************************************************************
;;; ;;;
...@@ -359,34 +359,36 @@ ...@@ -359,34 +359,36 @@
"Returns a new list the same as List without the last N conses. "Returns a new list the same as List without the last N conses.
List must not be circular." List must not be circular."
(declare (list list) (type index n)) (declare (list list) (type index n))
(let ((length (do ((list list (cdr list)) (unless (null list)
(i 0 (1+ i))) (let ((length (do ((list list (cdr list))
((atom list) (1- i))))) (i 0 (1+ i)))
(declare (type index length)) ((atom list) (1- i)))))
(unless (< length n) (declare (type index length))
(do* ((top (cdr list) (cdr top)) (unless (< length n)
(result (list (car list))) (do* ((top (cdr list) (cdr top))
(splice result) (result (list (car list)))
(count length (1- count))) (splice result)
((= count n) result) (count length (1- count)))
(declare (type index count)) ((= count n) result)
(setq splice (cdr (rplacd splice (list (car top))))))))) (declare (type index count))
(setq splice (cdr (rplacd splice (list (car top))))))))))
(defun nbutlast (list &optional (n 1)) (defun nbutlast (list &optional (n 1))
"Modifies List to remove the last N conses. List must not be circular." "Modifies List to remove the last N conses. List must not be circular."
(declare (list list) (type index n)) (declare (list list) (type index n))
(let ((length (do ((list list (cdr list)) (unless (null list)
(i 0 (1+ i))) (let ((length (do ((list list (cdr list))
((atom list) (1- i))))) (i 0 (1+ i)))
(declare (type index length)) ((atom list) (1- i)))))
(unless (< length n) (declare (type index length))
(do ((1st (cdr list) (cdr 1st)) (unless (< length n)
(2nd list 1st) (do ((1st (cdr list) (cdr 1st))
(count length (1- count))) (2nd list 1st)
((= count n) (count length (1- count)))
(rplacd 2nd ()) ((= count n)
list) (rplacd 2nd ())
(declare (type index count)))))) list)
(declare (type index count)))))))
(defun ldiff (list object) (defun ldiff (list object)
"Returns a new list, whose elements are those of List that appear before "Returns a new list, whose elements are those of List that appear before
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment