Next: , Previous: , Up: lists   [Index]


23.11 Deletion

Function: delete obj ell
Function: delete obj ell item=
Function: delete! obj ell
Function: delete! obj ell item=

Use the comparison procedure item= (which defaults to equal?) to find all the elements of ell that are equal to obj, and delete them from ell. The dynamic order in which the various applications of item= are made is not specified.

The list is not disordered: Elements that appear in the result list occur in the same order as they occur in ell. The result may share a common tail with ell.

Note that fully general element deletion can be performed with the remove and remove! procedures.

The comparison procedure is used as (item= obj Ei), that is: obj is always the first argument and a list element is always the second argument. The comparison procedure will be used to compare each element of list exactly once; the order in which it is applied to the various Ei is not specified. Thus, one can reliably remove all the numbers greater than 5 from a list with (delete 5 ell <).

delete! is allowed to alter the cons cells in ell to construct the result.

Function: delete-duplicates ell
Function: delete-duplicates ell item=
Function: delete-duplicates! ell
Function: delete-duplicates! ell item=

Remove duplicate elements from ell. If there are multiple equal elements in ell, the result list only contains the first or leftmost of these elements in the result. The order of these surviving elements is the same as in the original list: delete-duplicates does not disorder the list (hence it is useful for “cleaning up” association lists).

The item= parameter is used to compare the elements of the list; it defaults to equal?. If x comes before y in ell, then the comparison is performed as (item= x y). The comparison procedure will be used to compare each pair of elements in ell no more than once; the order in which it is applied to the various pairs is not specified.

The list argument may share a common tail with the returned list.

delete-duplicates! is allowed to alter the cons cells in its argument list to construct the result.

(delete-duplicates '(a b a c a b c z)) ⇒ (a b c z)

;; Clean up an alist:
(delete-duplicates '((a . 3) (b . 7) (a . 9) (c . 1))
                   (lambda (x y)
                     (eq? (car x) (car y))))
⇒ ((a . 3) (b . 7) (c . 1))

Next: , Previous: , Up: lists   [Index]