summaryrefslogtreecommitdiff
path: root/docsrc/xlisp/xlisp-doc/reference/delete-if-not.htm
blob: 7aed3a8c2d93ffe2c260b10b4bc13a5362ee3eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<html><head><title>XLISP delete-if-not</title>

<link rel="stylesheet" type="text/css" href="reference.css">

</head>

<body>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>

<hr>

<h1>delete-if-not</h1>

<hr>

<p><table cellpadding="0" cellspacing="0" style="margin-left:10px"><tbody>
<tr valign="top">
  <td><nobr>Type:</nobr></td>
  <td><nobr>&nbsp;&nbsp;-&nbsp;&nbsp;</nobr></td>
  <td width="100%"><nobr>function (subr)</nobr></td>
</tr>
<tr valign="top">
  <td><nobr>Source:</nobr></td>
  <td><nobr>&nbsp;&nbsp;-&nbsp;&nbsp;</nobr></td>
  <td width="100%"><nobr>xllist.c</nobr></td>
</tr>
</tbody></table></p>

<h2>Syntax</h2>

<dl>
<dt>(delete-if-not <i>test list</i>)</dt>
<dd><i>test</i> - the test function to be performed<br>
<i>list</i> - the list to delete from<br>
returns - the list with non-matching elements deleted</dd>
</dl>

<h2>Description</h2>

<p>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.</p>

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

<pre class="example">
<font color="#AA0000">error: bad argument type</font>
</pre>

<p>Having <a href="nil.htm">NIL</a> for 'list' will return a
<a href="nil.htm">NIL</a> as the result.</p>

<h2>Examples</h2>

<p><b>Caution:</b> there's a bug:</p>

<pre class="example">
(setq mylist '(1 2 3 4 5 6 7 8))             <font color="#008844">; set up a list of numbers</font>
(delete-if-not 'evenp mylist)                <font color="#008844">; returns (2 4 6 8)</font>
(print mylist)                               <font color="#008844">; prints  (1 2 4 6 8)</font> <font color="#AA0000">&lt;-BUG!</font>

(setq mylist '(1 2 3 4 5 6 7 8))             <font color="#008844">; set up the same list again</font>
(setq mylist (delete-if-not 'evenp mylist))  <font color="#008844">; returns (3 5 7)</font>
(print mylist)                               <font color="#008844">; prints  (3 5 7)</font> <font color="#009900">&lt;-OK</font>

(setq mylist '(1 2 3 4 5 6 7 8))             <font color="#008844">; ... again ...</font>
(delete-if-not 'oddp mylist)                 <font color="#008844">; returns (1 3 5 7)</font>
(print mylist)                               <font color="#008844">; prints  (1 3 5 7)</font> <font color="#009900">&lt;-OK</font>

(setq mylist '(1 2 3 4 5 6 7 8))             <font color="#008844">; ... and again ...</font>
(setq mylist (delete-if-not 'oddp mylist))   <font color="#008844">; returns (1 3 5 7)</font>
(print mylist)                               <font color="#008844">; prints  (1 3 5 7)</font> <font color="#009900">&lt;-OK</font>
</pre>

<p><b>Bug:</b> '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
<a href="setq.htm">setq</a> or
<a href="setf.htm">setf</a> as shown in <nobr>example 2</nobr>
<nobr>and 4.</nobr></p>

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

<p>See the
<a href="../manual/xlisp-man-017.htm#delete-if-not">delete-if-not</a>
function in the <nobr>XLISP 2.0</nobr> manual.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</nobr></a></p>

<hr>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="reference-index.htm">Reference</a>

</body></html>