7.5.3.8 Deleting

Scheme Procedure: delete x lst [=]
Scheme Procedure: delete! x lst [=]

Return a list containing the elements of lst but with those equal to x deleted. The returned elements will be in the same order as they were in lst.

Equality is determined by the = predicate, or equal? if not given. An equality call is made just once for each element, but the order in which the calls are made on the elements is unspecified.

The equality calls are always (= x elem), ie. the given x is first. This means for instance elements greater than 5 can be deleted with (delete 5 lst <).

delete does not modify lst, but the return might share a common tail with lst. delete! may modify the structure of lst to construct its return.

These functions extend the core delete and delete! (see List Modification) in accepting an equality predicate. See also lset-difference (see Set Operations on Lists) for deleting multiple elements from a list.

Scheme Procedure: delete-duplicates lst [=]
Scheme Procedure: delete-duplicates! lst [=]

Return a list containing the elements of lst but without duplicates.

When elements are equal, only the first in lst is retained. Equal elements can be anywhere in lst, they don’t have to be adjacent. The returned list will have the retained elements in the same order as they were in lst.

Equality is determined by the = predicate, or equal? if not given. Calls (= x y) are made with element x being before y in lst. A call is made at most once for each combination, but the sequence of the calls across the elements is unspecified.

delete-duplicates does not modify lst, but the return might share a common tail with lst. delete-duplicates! may modify the structure of lst to construct its return.

In the worst case, this is an O(N^2) algorithm because it must check each element against all those preceding it. For long lists it is more efficient to sort and then compare only adjacent elements.