From 5af76d1a409ca80314c6fda7e4570c532fe8b191 Mon Sep 17 00:00:00 2001
From: pw <pw>
Date: Wed, 15 Nov 2000 11:36:34 +0000
Subject: [PATCH] Fix butlast and nbutlast to be correct for NIL argument.

---
 code/list.lisp | 52 ++++++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/code/list.lisp b/code/list.lisp
index 1eb1cb143..68c699c4d 100644
--- a/code/list.lisp
+++ b/code/list.lisp
@@ -5,7 +5,7 @@
 ;;; Carnegie Mellon University, and has been placed in the public domain.
 ;;;
 (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 @@
   "Returns a new list the same as List without the last N conses.
    List must not be circular."
   (declare (list list) (type index n))
-  (let ((length (do ((list list (cdr list))
-		     (i 0 (1+ i)))
-		    ((atom list) (1- i)))))
-    (declare (type index length))
-    (unless (< length n)
-      (do* ((top (cdr list) (cdr top))
-	    (result (list (car list)))
-	    (splice result)
-	    (count length (1- count)))
-	   ((= count n) result)
-	(declare (type index count))
-	(setq splice (cdr (rplacd splice (list (car top)))))))))
+  (unless (null list)
+    (let ((length (do ((list list (cdr list))
+		       (i 0 (1+ i)))
+		      ((atom list) (1- i)))))
+      (declare (type index length))
+      (unless (< length n)
+	(do* ((top (cdr list) (cdr top))
+	      (result (list (car list)))
+	      (splice result)
+	      (count length (1- count)))
+	     ((= count n) result)
+	  (declare (type index count))
+	  (setq splice (cdr (rplacd splice (list (car top))))))))))
 
 (defun nbutlast (list &optional (n 1))
   "Modifies List to remove the last N conses. List must not be circular."
   (declare (list list) (type index n))
-  (let ((length (do ((list list (cdr list))
-		     (i 0 (1+ i)))
-		    ((atom list) (1- i)))))
-    (declare (type index length))
-    (unless (< length n)
-      (do ((1st (cdr list) (cdr 1st))
-	   (2nd list 1st)
-	   (count length (1- count)))
-	  ((= count n)
-	   (rplacd 2nd ())
-	   list)
-	(declare (type index count))))))
+  (unless (null list)
+    (let ((length (do ((list list (cdr list))
+		       (i 0 (1+ i)))
+		      ((atom list) (1- i)))))
+      (declare (type index length))
+      (unless (< length n)
+	(do ((1st (cdr list) (cdr 1st))
+	     (2nd list 1st)
+	     (count length (1- count)))
+	    ((= count n)
+	     (rplacd 2nd ())
+	     list)
+	  (declare (type index count)))))))
 
 (defun ldiff (list object)
   "Returns a new list, whose elements are those of List that appear before
-- 
GitLab