File: mapcar.htm

package info (click to toggle)
nyquist 3.20%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 58,008 kB
  • sloc: ansic: 74,743; lisp: 17,929; java: 10,723; cpp: 6,690; sh: 171; xml: 58; makefile: 40; python: 15
file content (151 lines) | stat: -rw-r--r-- 5,054 bytes parent folder | download | duplicates (7)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<html><head><title>XLISP mapcar</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>mapcar</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>(mapcar <i>function list1</i> [<i>list2</i> ... ])</dt>
<dd><i>function</i> - a function definition like a
<a href="lambda.htm">lambda</a> or a function name<br>
<i>listN</i> - a list or list expression<br>
returns - a list that is constructed from the results of the <i>function</i> applications</dd>
</dl>

<h2>Description</h2>

<p>The mapcan function 'mapcar' applies the 'function' to the succesive
<a href="car.htm">car</a>s of each of the lists 'listN'. Each of
the lists supplies one of the arguments to 'function'. The 'mapcar' function
returns a list that is constructed from the results of the 'function'
applications. If the lists are of different lengths, the shortest list will
determine the number of applications of 'function'.</p>

<h2>Examples</h2>

<pre class="example">
&gt; (mapcar #'+ '(1 2 3) '(1 2 3))
(2 4 6)

&gt; (mapcar #'princ '(1 2 3))       
123       <font color="#008844">; screen output</font>
(1 2 3)   <font color="#008844">; return value</font>

&gt; (mapcar #'+ '(1 2 3) '(1 2 3 4 5 6)  <font color="#008844">; different length lists</font>
(2 4 6)
</pre>

<p><b>Note:</b> The use of the 'function' will work properly when it is a
<nobr>sharp-quoted</nobr> symbol, <nobr>a quoted</nobr> symbol [must be the
name of a function], <nobr>an unquoted</nobr> symbol whose value is a
function, or a closure object like a <a href="lambda.htm">lambda</a>
form.</p>

<p><div class="box">

<p><b>Bug:</b> The proper syntax for 'function' when 'function' is a
<a href="lambda.htm">lambda</a> expression is, for example:</p>

<pre class="example">
(mapcar #'(lambda (arg1 arg2) (+ arg1 arg2)) '(1 2))
</pre>

<p>and not:</p>

<pre class="example">
(mapcar '(lambda (arg1 arg2) (+ arg1 arg2)) '(1 2))
</pre>

<p>That is, the #' [<a href="function.htm">function</a>] read macro must be
present. This error should be caught by the XLISP interpreter, but it is
not, with the result that very obscure garbage collection bugs occur.
<nobr>[I still</nobr> haven't tested Nyquist for possible garbage collection
bugs caused by this.]</p>

</div></p>

<h2>Notes</h2>

<p>In XLISP, a '<nobr>special form</nobr>' of type FSUBR is not a function.
This means that 'mapcar' only works with functions of type SUBR
<nobr>[built-in</nobr> function] or CLOSURE [function defined by
<a href="defun.htm">defun</a>, <a href="flet.htm">flet</a>,
<a href="labels.htm">labels</a>, or <a href="lambda.htm">lambda</a>], but
with special forms of <nobr>type FSUBR</nobr> a 'bad function' error is
signalled. Here is an example how to work around this behaviour:</p>

<pre class="example">
(defmacro <font color="#0000CC">mapcar*</font> (function &amp;rest args)
  (if (eq (type-of (symbol-function (second function))) 'fsubr)
      (let ((rest (gensym)))
        `(mapcar #'(lambda (&rest ,rest) 
                     (eval (cons ,function ,rest)))
                 ,@args))
      `(mapcar ,function ,@args)))
</pre>

<p>Examples:</p>

<pre class="example">
(type-of #'eql)  =&gt; SUBR   <font color="#008844">; built-in function</font>
(type-of #'and)  =&gt; FSUBR  <font color="#008844">; built-in special form</font>

&gt; (macroexpand-1 '(mapcar* #'eql '(1 2 3) '(t nil 3)))
(MAPCAR (FUNCTION EQL)
        (QUOTE (1 2 3))
        (QUOTE (T NIL 3)))

&gt; (macroexpand-1 '(mapcar* #'and '(1 2 3) '(t nil 3)))
(MAPCAR (FUNCTION (LAMBDA (&amp;REST G7)
                    (EVAL (CONS (FUNCTION AND) G7))))
        (QUOTE (1 2 3))
        (QUOTE (T NIL 3)))

(mapcar  #'eql '(1 2 3) '(t nil 3)))  =&gt; (NIL NIL T)
(mapcar* #'eql '(1 2 3) '(t nil 3)))  =&gt; (NIL NIL T)

(mapcar  #'and '(1 2 3) '(t nil 3)))  =&gt; <font color="#AA0000">error: bad function</font>
(mapcar* #'and '(1 2 3) '(t nil 3)))  =&gt; (T NIL 3)
</pre>


<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>