Skip to content

Commit

Permalink
Use destructive operations in list-insert and list-delete for 0 index…
Browse files Browse the repository at this point in the history
… too (euslisp#361)
  • Loading branch information
Affonso-Gui committed Sep 17, 2022
1 parent 103d985 commit 9d75fc7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lisp/l/common.l
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,21 @@
if pos is bigger than the length of list, item is nconc'ed at the tail"
(cond ((null list) (list item))
((>= pos (length list)) (nconc list (list item)))
((= pos 0) (cons item list))
((= pos 0)
(rplacd list (cons (car list) (cdr list)))
(rplaca list item))
(t (let ((tail (cons item (nthcdr pos list))))
(rplacd (nthcdr (1- pos) list) tail)
list))))

(defun list-delete (lst n) "(lst n) delete nth element of lst"
(if (= n 0)
(setq lst (cdr lst))
(rplacd (nthcdr (1- n) lst) (nthcdr (1+ n) lst)) )
(cond ((null lst) nil)
((>= n (length lst)) nil)
((= n 0)
(rplaca lst (cadr lst))
(rplacd lst (cddr lst)))
(t
(rplacd (nthcdr (1- n) lst) (nthcdr (1+ n) lst))))
lst)

(defun adjoin (item list &key (test #'eq) (test-not) (key #'identity))
Expand Down

0 comments on commit 9d75fc7

Please sign in to comment.