Lisp - Removing Element of a List



In LISP, we've multiple ways to remove elements from a List. In lisp, list is implemented as a Single Linked List. There are few methods which modifies the original list while removing the element whereas few returns a new list with reduced length. Let's cover these methods with examples.

remove method

remove is most commonly method to remove an element from a list. remove method is non-destructive and original list is not modified.

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)

Output

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

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

remove method with :count

remove method can specify the number of elements to be removed. By default, it removes all the oocurences.

main.lisp

; create a new list
(print(setf my-list '(1 2 3 1 2 3 1 2 3))) ; (1 2 3 1 2 3 1 2 3)
(terpri)
; remove all occurences of 2 from the list; (1 3 1 3 1 3)
(print(remove 2 my-list))
(terpri)
; remove 1 occurence of 2 from the list; (1 2 3 1 3 1 3)
(print(remove 2 my-list :count 1))

Output

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

(1 2 3 1 2 3 1 2 3) 
(1 3 1 3 1 3) 
(1 3 1 2 3 1 2 3) 

remove method with :test

remove method can have a comparison function using :test parameter.

main.lisp

; create a new list
(print(setf my-list '(11 2 3 4 15 6 17))) ; (11 2 3 4 15 6 17)
(terpri)
; remove numbers greater than 5
(print(remove 5 my-list :test #'>))

Output

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

(11 2 3 4 15 6 17) 
(11 15 6 17) 

remove method with :test-not

remove method can have a comparison function using :test-not parameter as shown below.

main.lisp

; create a new list
(print(setf my-list '(11 2 3 4 15 6 17))) ; (11 2 3 4 15 6 17)
(terpri)
; remove numbers not greater than 5
(print(remove 5 my-list :test-not #'>))

Output

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

(11 2 3 4 15 6 17)
(2 3 4) 

remove-if method

remove-if method takes a predicate to remove element(s) without modifying the original list.

main.lisp

; create a new list
(print(setf my-list '(11 2 3 4 15 6 17))) ; (11 2 3 4 15 6 17)
(terpri)
; remove even numbers
(print(remove-if #'evenp my-list))

Output

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

(11 2 3 4 15 6 17) 
(11 3 15 17) 

remove-if-not method

remove-if-not method is opposite of remove-if method.

main.lisp

; create a new list
(print(setf my-list '(11 2 3 4 15 6 17))) ; (11 2 3 4 15 6 17)
(terpri)
; remove non-even numbers
(print(remove-if-not #'evenp my-list))

Output

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

(11 2 3 4 15 6 17) 
(2 4 6)

delete method

delete is a destructive method. It deletes the elment(s) from the list while modifying the original list.

main.lisp

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

Output

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

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