XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next

delete-if


Type:   -   function (subr)
Source:   -   xllist.c

Syntax

(delete-if test list)
test - the test function to be performed
list - the list to delete from
returns - the list with matching elements deleted

Description

The 'delete-if' function destructively modifies the 'list' by removing the elements of the list that pass the 'test'. The destructive aspect of this operation means that the actual symbol value is used in the list-modifying operations, not a copy.

'list' must evaluate to a valid list. An atom for 'list' will result in an error:

error: bad argument type

Having NIL for 'list' will return a NIL as the result.

Examples

Caution: there's a bug:

(setq mylist '(1 2 3 4 5 6 7 8))         ; set up a list starting of numbers
(delete-if 'oddp mylist)                 ; returns (2 4 6)
(print mylist)                           ; prints  (1 2 4 6) <-BUG!

(setq mylist '(1 2 3 4 5 6 7 8))         ; set up the same list again
(setq mylist (delete-if 'oddp mylist))   ; returns (2 4 6)
(print mylist)                           ; prints  (2 4 6) <-OK

(setq mylist '(1 2 3 4 5 6 7 8))         ; ... again ...
(delete-if 'evenp mylist)                ; returns (1 3 5 7)
(print mylist)                           ; prints  (1 3 5 7) <-OK

(setq mylist '(1 2 3 4 5 6 7 8))         ; ... and again ...
(setq mylist (delete-if 'evenp mylist))  ; returns (1 3 5 7)
(print mylist)                           ; prints  (1 3 5 7) <-OK

Bug: 'delete-if' will return the proper value, but it does not always properly modify the symbol containing the value. This seems to be true if the first element of the 'list' passes the test [and should be deleted]. It's always better to use 'delete-if' together with setq or setf as shown in example 2 and 4.

Note: This bug can be reproduced with Nyquist 2.36 [in June 2007], so please take care.

Common Lisp: XLISP does not support the ':from-end', ':start', ':end', ':count' and ':key' keywords which Common LISP does.

See the delete-if function in the XLISP 2.0 manual.

  Back to Top


XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next