Lisp - Removing Element from a Sequence



Lisp provides multiple functions to remove elements from a sequence(lists, vectors etc.) In this chapter, we're going to discuss commonly used functions with examples.

remove function

remove function removes an element and returns a new sequence with specific item removed. Original sequence is unchanged.

Syntax

(remove item sequence &key from-end test test-not start end count key)

Arguments

  • item− Element to remove.

  • sequence− A sequence from which element is to be removed.

  • from-end− If passed t (true), item will be removed from the end.

  • test− Comparison Function to compare elements. By default, eql is used to search an element.

  • test-not− Function to use as the opposite of above comparison test function.

  • start− start index of the sequence to remove element.

  • end− end index of the sequence to remove element.

  • count− Maximum number of elements to remove.

  • key− Function to apply to each elements before removing. Useful for comparing attributes of elements.

Returns

This function returns a new sequence, with a specific item(s) removed.

Example - Using remove function

Following example, shows usage of remove function to remove an element from a list.

main.lisp

; remove and print list after removing element
(print (remove 'c '(a b c d)))

Output

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

(A B D)

delete function

delete function removes an element and modifies the original sequence with specific item removed. Original sequence is modified.

Syntax

(delete item sequence &key from-end test test-not start end count key)

Arguments

Same as that of remove function.

Returns

This function modifies the original sequence, with a specific item(s) removed.

Example - Using delete function

Following example, shows usage of delete function to remove an element from a list.

main.lisp

; set a new list
(setq my-list '(a b c d))
; remove element
(delete 'c my-list) 
; print element
(print my-list)

Output

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

(A B D)

remove-if function

remove-if function removes an element satifying a given predicate and returns a new sequence. Original sequence is unchanged.

Syntax

(remove-if predicate sequence &key from-end test start end count key)

Arguments

  • predicate− A function to return true for elements to be removed.

  • sequence− A sequence from which element is to be removed.

  • from-end− If passed t (true), item will be removed from the end.

  • test− Comparison Function to compare elements. By default, eql is used to search an element.

  • start− start index of the sequence to remove element.

  • end− end index of the sequence to remove element.

  • count− Maximum number of elements to remove.

  • key− Function to apply to each elements before removing. Useful for comparing attributes of elements.

Returns

This function returns a new sequence, with a specific item(s) removed.

Example - Using remove-if function

Following example, shows usage of remove function to remove an element from a list.

main.lisp

; remove odd numbers and print list after removing elements
(print (remove-if #'oddp '(1 2 3 4 5 6 7)))

Output

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

(2 4 6)

delete-if function

delete function removes an element satisfying the given predicate and modifies the original sequence with items removed. Original sequence is modified.

Syntax

(delete-if predicate sequence &key from-end test start end count key)

Arguments

Same as that of remove-if function.

Returns

This function modifies the original sequence, with a specific item(s) removed.

Example - Using delete-if function

Following example, shows usage of delete function to remove an element from a list.

main.lisp

; set a new list
(setq my-list '( 1 2 3 4 5 6 7))
; remove even elements
(delete-if #'evenp my-list) 
; print element
(print my-list)

Output

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

(1 3 5 7)
Advertisements