Lisp - List Modifications



In LISP, List is a fundamental data structure and LISP provides various methods to modify a list. In this chapter, let's us discuss most commonly used methods with examples.

rplaca and rplacd methods

rplaca (replace car) and rplaced (replace cdr) are low level functions which can directly modify the car and cdr of a list cell. They modifies the original list. Following examples showcases the usage of both functions.

main.lisp

; create a new list
(print(setf my-list '(a b c))) ; (A B C)
(terpri)
; replace first element and print the updated list; (X A B) 
(print(rplaca my-list 'x))
(terpri)
; replace rest of the list; (X Y Z)
(print(rplacd my-list '(y z)))

Output

When you execute the code, it returns the following result −

(A B C) 
(X B C) 
(X Y Z) 

push method, Add to Beginning

push method adds an element to the beginning of the list and modify the original list as shown in example below−

main.lisp

; create a new list
(print(setf my-list '(a b c))) ; (A B C)
(terpri)
; add a new element at beginning; (X A B C)
(print(push 'x my-list))

Output

When you execute the code, it returns the following result −

(A B C) 
(X A B C) 

pop method, Remove from Beginning

pop method removes an element from the beginning of the list and returns the same. pop method modifies the original list as shown in example below−

main.lisp

; create a new list
(print(setf my-list '(a b c))) ; (A B C)
(terpri)
; remove an element from the beginning; A
(print(pop my-list))
(terpri)
; print the updated list; (B C)
(print my-list)

Output

When you execute the code, it returns the following result −

(A B C) 
A 
(B C) 

append method, Add to End without Modification

append method adds an element to the end of the list without modifying it as it returns the updated list as shown in example below−

main.lisp

; create a new list
(print(setf my-list '(a b c))) ; (A B C)
(terpri)
; append an element to the end ; (A B C D)
(print(setf new-list (append my-list '(d))))
(terpri)
; print the original list unmodified; (A B C)
(print my-list)

Output

When you execute the code, it returns the following result −

(A B C) 
(A B C D) 
(A B C) 

nconc method, Add to End with Modification

nconc method adds an element to the end of the list while altering the original list as shown in example below−

main.lisp

; create a new list
(print(setf my-list '(a b c))) ; (A B C)
(terpri)
; append an element to the end ; (A B C D)
(print(nconc my-list '(d)))

Output

When you execute the code, it returns the following result −

(A B C) 
(A B C D) 

delete and remove methods

remove methods removes an element from the list without modifying the original structure whiledelete removes and modifies the original list as shown in example below−

main.lisp

; create a new list
(print(setf my-list '(a b c))) ; (A B C)
(terpri)
; remove an element from the list; (A C)
(print(remove 'b my-list))
(terpri)
; print the original list unmodified; (A B C)
(print my-list)
(terpri)
; delete an element from the list; (A C)
(print(delete 'b my-list))
(terpri)
; print the original list modified; (A B C)
(print my-list)

Output

When you execute the code, it returns the following result −

(A B C) 
(A C) 
(A B C) 
(A C) 
(A C) 
Advertisements