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

delete-if-not


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

Syntax

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

Description

The 'delete-if-not' function destructively modifies the 'list' by removing the elements of the list that fail 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 of numbers
(delete-if-not 'evenp mylist)                ; returns (2 4 6 8)
(print mylist)                               ; prints  (1 2 4 6 8) <-BUG!

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

(setq mylist '(1 2 3 4 5 6 7 8))             ; ... again ...
(delete-if-not 'oddp 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-not 'oddp mylist))   ; returns (1 3 5 7)
(print mylist)                               ; prints  (1 3 5 7) <-OK

Bug: 'delete-if-not' 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' fails the test [and should be deleted]. It's always better to use 'delete-if-not' together with setq or setf as shown in example 2 and 4.

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

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

  Back to Top


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