| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<html> |
| 3 |
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ --> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<title>mapc (ANSI and GNU Common Lisp Document)</title> |
| 7 |
|
| 8 |
<meta name="description" content="mapc (ANSI and GNU Common Lisp Document)"> |
| 9 |
<meta name="keywords" content="mapc (ANSI and GNU Common Lisp Document)"> |
| 10 |
<meta name="resource-type" content="document"> |
| 11 |
<meta name="distribution" content="global"> |
| 12 |
<meta name="Generator" content="makeinfo"> |
| 13 |
<link href="index.html" rel="start" title="Top"> |
| 14 |
<link href="Conses-Dictionary.html" rel="up" title="Conses Dictionary"> |
| 15 |
<link href="acons.html" rel="next" title="acons"> |
| 16 |
<link href="member-_0028Function_0029.html" rel="prev" title="member (Function)"> |
| 17 |
<style type="text/css"> |
| 18 |
<!-- |
| 19 |
a.summary-letter {text-decoration: none} |
| 20 |
blockquote.indentedblock {margin-right: 0em} |
| 21 |
div.display {margin-left: 3.2em} |
| 22 |
div.example {margin-left: 3.2em} |
| 23 |
div.lisp {margin-left: 3.2em} |
| 24 |
kbd {font-style: oblique} |
| 25 |
pre.display {font-family: inherit} |
| 26 |
pre.format {font-family: inherit} |
| 27 |
pre.menu-comment {font-family: serif} |
| 28 |
pre.menu-preformatted {font-family: serif} |
| 29 |
span.nolinebreak {white-space: nowrap} |
| 30 |
span.roman {font-family: initial; font-weight: normal} |
| 31 |
span.sansserif {font-family: sans-serif; font-weight: normal} |
| 32 |
ul.no-bullet {list-style: none} |
| 33 |
--> |
| 34 |
</style> |
| 35 |
|
| 36 |
|
| 37 |
</head> |
| 38 |
|
| 39 |
<body lang="en"> |
| 40 |
<span id="mapc"></span><div class="header"> |
| 41 |
<p> |
| 42 |
Next: <a href="acons.html" accesskey="n" rel="next">acons</a>, Previous: <a href="member-_0028Function_0029.html" accesskey="p" rel="prev">member (Function)</a>, Up: <a href="Conses-Dictionary.html" accesskey="u" rel="up">Conses Dictionary</a> </p> |
| 43 |
</div> |
| 44 |
<hr> |
| 45 |
<span id="mapc_002c-mapcar_002c-mapcan_002c-mapl_002c-maplist_002c-mapcon-_005bFunction_005d"></span><h4 class="subsection">14.2.34 mapc, mapcar, mapcan, mapl, maplist, mapcon [Function]</h4> |
| 46 |
|
| 47 |
<p><code>mapc</code> <i>function <span class="roman">&rest</span> lists^+</i> ⇒ <i>list-1</i> |
| 48 |
</p> |
| 49 |
<p><code>mapcar</code> <i>function <span class="roman">&rest</span> lists^+</i> ⇒ <i>result-list</i> |
| 50 |
</p> |
| 51 |
<p><code>mapcan</code> <i>function <span class="roman">&rest</span> lists^+</i> ⇒ <i>concatenated-results</i> |
| 52 |
</p> |
| 53 |
<p><code>mapl</code> <i>function <span class="roman">&rest</span> lists^+</i> ⇒ <i>list-1</i> |
| 54 |
</p> |
| 55 |
<p><code>maplist</code> <i>function <span class="roman">&rest</span> lists^+</i> ⇒ <i>result-list</i> |
| 56 |
</p> |
| 57 |
<p><code>mapcon</code> <i>function <span class="roman">&rest</span> lists^+</i> ⇒ <i>concatenated-results</i> |
| 58 |
</p> |
| 59 |
<span id="Arguments-and-Values_003a_003a-303"></span><h4 class="subsubheading">Arguments and Values::</h4> |
| 60 |
|
| 61 |
<p><i>function</i>—a <i>designator</i> for a <i>function</i> |
| 62 |
that must take as many <i>arguments</i> as there are <i>lists</i>. |
| 63 |
</p> |
| 64 |
<p><i>list</i>—a <i>proper list</i>. |
| 65 |
</p> |
| 66 |
<p><i>list-1</i>—the first <i>list</i> (which must be a <i>proper list</i>). |
| 67 |
</p> |
| 68 |
<p><i>result-list</i>—a <i>list</i>. |
| 69 |
</p> |
| 70 |
<p><i>concatenated-results</i>—a <i>list</i>. |
| 71 |
</p> |
| 72 |
<span id="Description_003a_003a-404"></span><h4 class="subsubheading">Description::</h4> |
| 73 |
|
| 74 |
<p>The mapping operation involves applying <i>function</i> to |
| 75 |
successive sets of arguments in which |
| 76 |
one argument is obtained from each <i>sequence</i>. |
| 77 |
Except for <b>mapc</b> and <b>mapl</b>, |
| 78 |
the result contains the results returned by <i>function</i>. |
| 79 |
In the cases of <b>mapc</b> and <b>mapl</b>, |
| 80 |
the resulting <i>sequence</i> is <i>list</i>. |
| 81 |
</p> |
| 82 |
<p><i>function</i> is called |
| 83 |
first on all the elements with index <tt>0</tt>, then on all those |
| 84 |
with index <tt>1</tt>, and so on. |
| 85 |
<i>result-type</i> specifies the <i>type</i> of |
| 86 |
the |
| 87 |
resulting <i>sequence</i>. |
| 88 |
</p> |
| 89 |
<p>If <i>function</i> is a <i>symbol</i>, it is <b>coerce</b>d |
| 90 |
to a <i>function</i> as if by <b>symbol-function</b>. |
| 91 |
</p> |
| 92 |
<p><b>mapcar</b> operates on successive <i>elements</i> of the <i>lists</i>. |
| 93 |
<i>function</i> is applied to the first <i>element</i> of each <i>list</i>, |
| 94 |
then to the second <i>element</i> of each <i>list</i>, and so on. |
| 95 |
The iteration terminates when the shortest <i>list</i> runs out, |
| 96 |
and excess elements in other lists are ignored. |
| 97 |
The value returned by <b>mapcar</b> is a <i>list</i> |
| 98 |
of the results of successive calls to <i>function</i>. |
| 99 |
</p> |
| 100 |
<p><b>mapc</b> is like <b>mapcar</b> except that the results of |
| 101 |
applying <i>function</i> are not accumulated. |
| 102 |
The <i>list</i> argument is returned. |
| 103 |
</p> |
| 104 |
<p><b>maplist</b> is like <b>mapcar</b> except that |
| 105 |
<i>function</i> is applied to successive sublists of the <i>lists</i>. |
| 106 |
<i>function</i> |
| 107 |
is first applied to the <i>lists</i> themselves, |
| 108 |
and then to the <i>cdr</i> of each |
| 109 |
<i>list</i>, and then to the <i>cdr</i> of the <i>cdr</i> |
| 110 |
of each <i>list</i>, and so on. |
| 111 |
</p> |
| 112 |
<p><b>mapl</b> is like <b>maplist</b> except that the results of |
| 113 |
applying <i>function</i> are not accumulated; |
| 114 |
<i>list-1</i> is returned. |
| 115 |
</p> |
| 116 |
<p><b>mapcan</b> and <b>mapcon</b> are like <b>mapcar</b> and |
| 117 |
<b>maplist</b> respectively, except that the results of |
| 118 |
applying <i>function</i> are combined |
| 119 |
into a <i>list</i> by the use of <b>nconc</b> |
| 120 |
rather than <b>list</b>. |
| 121 |
That is, |
| 122 |
</p> |
| 123 |
<div class="example"> |
| 124 |
<pre class="example"> (mapcon f x1 ... xn) |
| 125 |
≡ (apply #'nconc (maplist f x1 ... xn)) |
| 126 |
</pre></div> |
| 127 |
|
| 128 |
<p>and similarly for the relationship between <b>mapcan</b> |
| 129 |
and <b>mapcar</b>. |
| 130 |
</p> |
| 131 |
<span id="Examples_003a_003a-287"></span><h4 class="subsubheading">Examples::</h4> |
| 132 |
|
| 133 |
<div class="example"> |
| 134 |
<pre class="example"> (mapcar #'car '((1 a) (2 b) (3 c))) ⇒ (1 2 3) |
| 135 |
(mapcar #'abs '(3 -4 2 -5 -6)) ⇒ (3 4 2 5 6) |
| 136 |
(mapcar #'cons '(a b c) '(1 2 3)) ⇒ ((A . 1) (B . 2) (C . 3)) |
| 137 |
|
| 138 |
(maplist #'append '(1 2 3 4) '(1 2) '(1 2 3)) |
| 139 |
⇒ ((1 2 3 4 1 2 1 2 3) (2 3 4 2 2 3)) |
| 140 |
(maplist #'(lambda (x) (cons 'foo x)) '(a b c d)) |
| 141 |
⇒ ((FOO A B C D) (FOO B C D) (FOO C D) (FOO D)) |
| 142 |
(maplist #'(lambda (x) (if (member (car x) (cdr x)) 0 1)) '(a b a c d b c)) |
| 143 |
⇒ (0 0 1 0 1 1 1) |
| 144 |
;An entry is 1 if the corresponding element of the input |
| 145 |
; list was the last instance of that element in the input list. |
| 146 |
|
| 147 |
(setq dummy nil) ⇒ NIL |
| 148 |
(mapc #'(lambda (&rest x) (setq dummy (append dummy x))) |
| 149 |
'(1 2 3 4) |
| 150 |
'(a b c d e) |
| 151 |
'(x y z)) ⇒ (1 2 3 4) |
| 152 |
dummy ⇒ (1 A X 2 B Y 3 C Z) |
| 153 |
|
| 154 |
(setq dummy nil) ⇒ NIL |
| 155 |
(mapl #'(lambda (x) (push x dummy)) '(1 2 3 4)) ⇒ (1 2 3 4) |
| 156 |
dummy ⇒ ((4) (3 4) (2 3 4) (1 2 3 4)) |
| 157 |
|
| 158 |
(mapcan #'(lambda (x y) (if (null x) nil (list x y))) |
| 159 |
'(nil nil nil d e) |
| 160 |
'(1 2 3 4 5 6)) ⇒ (D 4 E 5) |
| 161 |
(mapcan #'(lambda (x) (and (numberp x) (list x))) |
| 162 |
'(a 1 b c 3 4 d 5)) |
| 163 |
⇒ (1 3 4 5) |
| 164 |
</pre></div> |
| 165 |
|
| 166 |
<p>In this case the function serves as a filter; |
| 167 |
this is a standard <span class="roman">Lisp</span> idiom using <b>mapcan</b>. |
| 168 |
</p> |
| 169 |
<div class="example"> |
| 170 |
<pre class="example"> (mapcon #'list '(1 2 3 4)) ⇒ ((1 2 3 4) (2 3 4) (3 4) (4)) |
| 171 |
</pre></div> |
| 172 |
|
| 173 |
<span id="Exceptional-Situations_003a_003a-129"></span><h4 class="subsubheading">Exceptional Situations::</h4> |
| 174 |
|
| 175 |
<p>Should be prepared to signal an error of <i>type</i> <b>type-error</b> |
| 176 |
if any <i>list</i> is not a <i>proper list</i>. |
| 177 |
</p> |
| 178 |
<span id="See-Also_003a_003a-328"></span><h4 class="subsubheading">See Also::</h4> |
| 179 |
|
| 180 |
<p><a href="dolist.html">dolist</a> |
| 181 |
, |
| 182 |
<a href="map.html">map</a> |
| 183 |
, |
| 184 |
</p> |
| 185 |
<p><a href="Traversal-Rules-and-Side-Effects.html">Traversal Rules and Side Effects</a> |
| 186 |
</p> |
| 187 |
<hr> |
| 188 |
<div class="header"> |
| 189 |
<p> |
| 190 |
Next: <a href="acons.html" accesskey="n" rel="next">acons</a>, Previous: <a href="member-_0028Function_0029.html" accesskey="p" rel="prev">member (Function)</a>, Up: <a href="Conses-Dictionary.html" accesskey="u" rel="up">Conses Dictionary</a> </p> |
| 191 |
</div> |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
</body> |
| 196 |
</html> |