﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-糯米-随笔分类-Lisp</title><link>http://www.cppblog.com/varg-vikernes/category/17616.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 22 Aug 2011 04:45:43 GMT</lastBuildDate><pubDate>Mon, 22 Aug 2011 04:45:43 GMT</pubDate><ttl>60</ttl><item><title>lisp let,let*</title><link>http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154059.html</link><dc:creator>糯米</dc:creator><author>糯米</author><pubDate>Mon, 22 Aug 2011 03:50:00 GMT</pubDate><guid>http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154059.html</guid><wfw:comment>http://www.cppblog.com/varg-vikernes/comments/154059.html</wfw:comment><comments>http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154059.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/varg-vikernes/comments/commentRss/154059.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/varg-vikernes/services/trackbacks/154059.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; "><pre>let and let* create new variable bindings and execute a series of forms that use these bindings. <br />let performs the bindings in parallel and let* does them sequentially.<br /><br />The form<br /><br /> (let ((var1 init-form-1)<br />       (var2 init-form-2)<br />       ...<br />       (varm init-form-m))<br />   declaration1<br />   declaration2<br />   ...<br />   declarationp<br />   form1<br />   form2<br />   ...<br />   formn)<br /><br />first evaluates the expressions init-form-1, init-form-2, and so on, in that order, saving the resulting values. <br />Then all of the variables varj are bound to the corresponding values; <br />each binding is lexical unless there is a special declaration to the contrary. <br />The expressions formk are then evaluated in order; the values of all but the last are discarded <br />   (that is, the body of a let is an implicit progn).<br />let* is similar to let, but the bindings of variables are performed sequentially rather than in parallel. <br />The expression for the init-form of a var can refer to vars previously bound in the let*.<br /><br />The form<br /><br /> (let* ((var1 init-form-1)<br />        (var2 init-form-2)<br />        ...<br />        (varm init-form-m))<br />   declaration1<br />   declaration2<br />   ...<br />   declarationp<br />   form1<br />   form2<br />   ...<br />   formn)<br />first evaluates the expression init-form-1, then binds the variable var1 to that value;<br /> then it evaluates init-form-2 and binds var2, and so on. <br />The expressions formj are then evaluated in order; <br />the values of all but the last are discarded (that is, the body of let* is an implicit progn).<br /><br />For both let and let*, if there is not an init-form associated with a var, var is initialized to nil.<br /><br />The special form let has the property that the scope of the name binding does not include any initial value form. <br />For let*, a variable's scope also includes the remaining initial value forms for subsequent variable bindings.<br /><br /><br /><strong>Examples:<br /></strong><br /> (setq a 'top) =&gt;  TOP<br /> (defun dummy-function () a) =&gt;  DUMMY-FUNCTION<br /> (let ((a 'inside) (b a))<br />    (format nil "~S ~S ~S" a b (dummy-function))) =&gt;  "INSIDE TOP TOP" <br /> (let* ((a 'inside) (b a))<br />    (format nil "~S ~S ~S" a b (dummy-function))) =&gt;  "INSIDE INSIDE TOP" <br /> (let ((a 'inside) (b a))<br />    (declare (special a))<br />    (format nil "~S ~S ~S" a b (dummy-function))) =&gt;  "INSIDE TOP INSIDE"</pre></span><img src ="http://www.cppblog.com/varg-vikernes/aggbug/154059.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/varg-vikernes/" target="_blank">糯米</a> 2011-08-22 11:50 <a href="http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154059.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lisp loop,dotimes,dolist,do</title><link>http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154055.html</link><dc:creator>糯米</dc:creator><author>糯米</author><pubDate>Mon, 22 Aug 2011 03:05:00 GMT</pubDate><guid>http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154055.html</guid><wfw:comment>http://www.cppblog.com/varg-vikernes/comments/154055.html</wfw:comment><comments>http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154055.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/varg-vikernes/comments/commentRss/154055.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/varg-vikernes/services/trackbacks/154055.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; background-color: #ffffff; "><h2>Simple LOOP loops forever...</h2></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; background-color: #ffffff; "><pre>? (loop
    (print "Look, I'm looping!"))
"Look, I'm looping!" 
"Look, I'm looping!" 
"Look, I'm looping!" 
"Look, I'm looping!" 
"Look, I'm looping!" 
"Look, I'm looping!" 
"Look, I'm looping!" 
"Look, I'm looping!" 
<em>... and so on, until you interrupt execution...</em> 
Aborted
? <br /><br /></pre></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; background-color: #ffffff; "><pre>? (let ((n 0))
    (loop
      (when (&gt; n 10) (return))
      (print n) (prin1 (* n n))
      (incf n)))
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
NIL
?</pre></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><h2><br />Use DOTIMES for a counted loop</h2></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><pre>? (dotimes (n 11)
    (print n) (prin1 (* n n)))
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
NIL
?</pre></span><br /><br /><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><h2>Use DOLIST to process elements of a list</h2></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; background-color: #ffffff; "><pre>? (dolist (item '(1 2 4 5 9 17 25))
    (format t "~&amp;~D is~:[n't~;~] a perfect square.~%" item (integerp (sqrt item))))
1 is a perfect square.
2 isn't a perfect square.
4 is a perfect square.
5 isn't a perfect square.
9 is a perfect square.
17 isn't a perfect square.
25 is a perfect square.
NIL<br /><br /><br /></pre></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><pre>? (dolist (item `(1 foo "Hello" 79.3 2/3 ,#'abs))
    (format t "~&amp;~S is a ~A~%" item (type-of item)))
1 is a FIXNUM
FOO is a SYMBOL
"Hello" is a (SIMPLE-BASE-STRING 5)
79.3 is a DOUBLE-FLOAT
2/3 is a RATIO
#&lt;Compiled-function ABS #x1E9CC3E&gt; is a FUNCTION
NIL
? </pre></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; background-color: #ffffff; "><pre><br /></pre></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><h2>DO is tricky, but powerful</h2></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><pre>? (do ((which 1 (1+ which))
       (list '(foo bar baz qux) (rest list)))
      ((null list) 'done)
    (format t "~&amp;Item ~D is ~S.~%" which (first list)))
Item 1 is FOO.
Item 2 is BAR.
Item 3 is BAZ.
Item 4 is QUX.
DONE
? 
</pre><div></div></span><span class="Apple-style-span" style="font-family: monospace; line-height: normal; white-space: pre; background-color: #ffffff; font-size: medium; ">(do ((<em>var1 init1 step1</em>)</span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><pre>     (<em>var2 init2 step2</em>)
     <em>...</em>)
    (<em>end-test result</em>)
  <em>statement1
  ...</em>)

<em>var1</em>       = which
<em>init1</em>      = 1
<em>step1</em>      = (1+ which)
<em>var2</em>       = list
<em>init2</em>      = '(foo bar baz qux)
<em>step2</em>      = (rest list)
<em>end-test</em>   = (null list)
<em>result</em>     = 'done
<em>statement1</em> = (format t "~&amp;Item ~D is ~S.~%" which (first list))
</pre><div></div></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; background-color: #ffffff; font-size: medium; "><div></div></span><span class="Apple-style-span" style="font-family: Simsun; line-height: normal; font-size: medium; background-color: #ffffff; "><pre><br /><br /><br /></pre></span><img src ="http://www.cppblog.com/varg-vikernes/aggbug/154055.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/varg-vikernes/" target="_blank">糯米</a> 2011-08-22 11:05 <a href="http://www.cppblog.com/varg-vikernes/archive/2011/08/22/154055.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lisp find find-if find-if-not</title><link>http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153887.html</link><dc:creator>糯米</dc:creator><author>糯米</author><pubDate>Fri, 19 Aug 2011 14:04:00 GMT</pubDate><guid>http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153887.html</guid><wfw:comment>http://www.cppblog.com/varg-vikernes/comments/153887.html</wfw:comment><comments>http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153887.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/varg-vikernes/comments/commentRss/153887.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/varg-vikernes/services/trackbacks/153887.html</trackback:ping><description><![CDATA[<div>
<p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>find</strong>&nbsp;<em>item sequence&nbsp;<tt>&amp;key</tt>&nbsp;from-end test test-not start end key</em>&nbsp;=&gt;&nbsp;<em>element</em></p>
<p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>find-if</strong>&nbsp;<em>predicate sequence&nbsp;<tt>&amp;key</tt>&nbsp;from-end start end key</em>&nbsp;=&gt;&nbsp;<em>element</em></p>
<p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>find-if-not</strong>&nbsp;<em>predicate sequence&nbsp;<tt>&amp;key</tt>&nbsp;from-end start end key</em>&nbsp;=&gt;&nbsp;<em>element<br />
<br />
</em></p>
<div>
<p style="font-style: normal; "><em><strong>Arguments and Values:</strong></em></p>
<p style="font-style: normal; "></p>
<p style="font-style: normal; "><em><em>item</em>---an&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_o.htm#object"><em>object</em></a>.<br />
</em></p>
<p style="font-style: normal; "><em><em>sequence</em>---a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_p.htm#proper_sequence"><em>proper sequence</em></a>.<br />
</em></p>
<p style="font-style: normal; "><em><em>predicate</em>---a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#designator"><em>designator</em></a>&nbsp;for a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function"><em>function</em></a>&nbsp;of one&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_a.htm#argument"><em>argument</em></a>&nbsp;that returns a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_g.htm#generalized_boolean"><em>generalized boolean</em></a>.<br />
接受一个参数的函数，返回boolean</em></p>
<p style="font-style: normal; "><em><em>from-end</em>---a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_g.htm#generalized_boolean"><em>generalized boolean</em></a>. The default is&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#false"><em>false</em></a>.<br />
boolean类型，默认为false</em></p>
<p style="font-style: normal; "><em><em>test</em>---a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#designator"><em>designator</em></a>&nbsp;for a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function"><em>function</em></a>&nbsp;of two&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_a.htm#argument"><em>arguments</em></a>&nbsp;that returns a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_g.htm#generalized_boolean"><em>generalized boolean</em></a>.<br />
接受两个参数的函数，返回boolean</em></p>
<p style="font-style: normal; "><em><em>test-not</em>---a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#designator"><em>designator</em></a>&nbsp;for a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function"><em>function</em></a>&nbsp;of two&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_a.htm#argument"><em>arguments</em></a>&nbsp;that returns a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_g.htm#generalized_boolean"><em>generalized boolean</em></a>.<br />
接受两个参数的函数，返回boolean</em></p>
<p style="font-style: normal; "><em><em>start</em>,&nbsp;<em>end</em>---<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_b.htm#bounding_index_designator"><em>bounding index designators</em></a>&nbsp;of&nbsp;<em>sequence</em>. The defaults for&nbsp;<em>start</em>&nbsp;and&nbsp;<em>end</em>&nbsp;are&nbsp;<tt>0</tt>&nbsp;and&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/a_nil.htm#nil"><strong>nil</strong></a>, respectively.<br />
</em></p>
<p style="font-style: normal; "><em><em>key</em>---a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#designator"><em>designator</em></a>&nbsp;for a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function"><em>function</em></a>&nbsp;of one argument, or&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/a_nil.htm#nil"><strong>nil</strong></a>.</em></p>
<p style="font-style: normal; "><em><em>element</em>---an&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>element</em></a>&nbsp;of the&nbsp;<em>sequence</em>, or&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/a_nil.htm#nil"><strong>nil</strong></a>.<br />
<br />
</em></p>
<div>
<p><em><a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_find_.htm#find"><strong>find</strong></a>,&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_find_.htm#find-if"><strong>find-if</strong></a>, and&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_find_.htm#find-if-not"><strong>find-if-not</strong></a>&nbsp;each search for an&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>element</em></a>&nbsp;of the&nbsp;<em>sequence</em>&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_b.htm#bounded"><em>bounded</em></a>&nbsp;by&nbsp;<em>start</em>&nbsp;and&nbsp;<em>end</em>&nbsp;that&nbsp;<em>satisfies the predicate</em>&nbsp;<em>predicate</em>&nbsp;or that&nbsp;<em>satisfies the test</em>&nbsp;<em>test</em>&nbsp;or&nbsp;<em>test-not</em>, as appropriate.</em></p>
<p><em>If&nbsp;<em>from-end</em>&nbsp;is&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_t.htm#true"><em>true</em></a>, then the result is the rightmost&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>element</em></a>&nbsp;that&nbsp;<em>satisfies the test</em>.</em></p>
<p><em>If the&nbsp;<em>sequence</em>&nbsp;contains an&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>element</em></a>&nbsp;that&nbsp;<em>satisfies the test</em>, then the leftmost or rightmost&nbsp;<em>sequence</em>&nbsp;element, depending on&nbsp;<em>from-end</em>, is returned; otherwise&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/a_nil.htm#nil"><strong>nil</strong></a>&nbsp;is returned.</em></p>
</div>
<em><br />
<div>
<p><strong>Examples:</strong></p>
<pre>Examples:
(find #\d "here are some letters that can be looked at" :test #'char&gt;)
=&gt; #\Space
(find-if #'oddp '(1 2 3 4 5) :end 3 :from-end t) =&gt; 3
(find-if-not #'complexp '#(3.5 2 #C(1.0 0.0) #C(0.0 1.0)) :start 2) =&gt; NIL
</pre>
</div>
</em></div>
</div><img src ="http://www.cppblog.com/varg-vikernes/aggbug/153887.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/varg-vikernes/" target="_blank">糯米</a> 2011-08-19 22:04 <a href="http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153887.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lisp MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON</title><link>http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153861.html</link><dc:creator>糯米</dc:creator><author>糯米</author><pubDate>Fri, 19 Aug 2011 13:44:00 GMT</pubDate><guid>http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153861.html</guid><wfw:comment>http://www.cppblog.com/varg-vikernes/comments/153861.html</wfw:comment><comments>http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153861.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/varg-vikernes/comments/commentRss/153861.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/varg-vikernes/services/trackbacks/153861.html</trackback:ping><description><![CDATA[<div><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>mapc</strong>&nbsp;<em>function&nbsp;<tt>&amp;rest</tt>&nbsp;lists+</em>&nbsp;=&gt;&nbsp;<em>list-1</em></p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>mapcar</strong>&nbsp;<em>function&nbsp;<tt>&amp;rest</tt>&nbsp;lists+</em>&nbsp;=&gt;&nbsp;<em>result-list</em></p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>mapcan</strong>&nbsp;<em>function&nbsp;<tt>&amp;rest</tt>&nbsp;lists+</em>&nbsp;=&gt;&nbsp;<em>concatenated-results</em></p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>mapl</strong>&nbsp;<em>function&nbsp;<tt>&amp;rest</tt>&nbsp;lists+</em>&nbsp;=&gt;&nbsp;<em>list-1</em></p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>maplist</strong>&nbsp;<em>function&nbsp;<tt>&amp;rest</tt>&nbsp;lists+</em>&nbsp;=&gt;&nbsp;<em>result-list</em></p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "><strong>mapcon</strong>&nbsp;<em>function&nbsp;<tt>&amp;rest</tt>&nbsp;lists+</em>&nbsp;=&gt;&nbsp;<em>concatenated-results<br /><br /></em></p><div><p style="font-style: normal; "><em><a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcar"><strong>mapcar</strong></a>&nbsp;operates on successive&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>elements</em></a>&nbsp;of the&nbsp;<em>lists</em>.&nbsp;<em>function</em>&nbsp;is applied to the first&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>element</em></a>&nbsp;of each&nbsp;<em>list</em>, then to the second&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#element"><em>element</em></a>&nbsp;of each&nbsp;<em>list</em>, and so on. The iteration terminates when the shortest&nbsp;<em>list</em>&nbsp;runs out, and excess elements in other lists are ignored. The value returned by&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcar"><strong>mapcar</strong></a>&nbsp;is a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_l.htm#list"><em>list</em></a>&nbsp;of the results of successive calls to&nbsp;<em>function</em>.</em></p></div><div><em>mapcar 首先将函数apply到每个列表的第一个元素，再将函数apply到每个列表的第二个元素。。<br />一直到最短的列表的最后一个元素。剩下的元素将被忽略。<br />它的结果是返回值不为nil的集合。</em></div><em><br /><div><p style="font-family: 文泉驿正黑; font-style: normal; line-height: normal; font-size: medium; "><a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapc"><strong>mapc</strong></a>&nbsp;is like&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcar"><strong>mapcar</strong></a>&nbsp;except that the results of applying&nbsp;<em>function</em>&nbsp;are not accumulated. The&nbsp;<em>list</em>&nbsp;argument is returned.</p></div><div>mapc 和 mapcar 类似。不过返回的是第一个列表。</div><br /><div><p style="font-family: 文泉驿正黑; font-style: normal; line-height: normal; font-size: medium; "><a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#maplist"><strong>maplist</strong></a>&nbsp;is like&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcar"><strong>mapcar</strong></a>&nbsp;except that&nbsp;<em>function</em>&nbsp;is applied to successive sublists of the&nbsp;<em>lists</em>.&nbsp;<em>function</em>&nbsp;is first applied to the&nbsp;<em>lists</em>&nbsp;themselves, and then to the&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_c.htm#cdr"><em>cdr</em></a>&nbsp;of each&nbsp;<em>list</em>, and then to the&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_c.htm#cdr"><em>cdr</em></a>&nbsp;of the&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_c.htm#cdr"><em>cdr</em></a>&nbsp;of each&nbsp;<em>list</em>, and so on.</p></div><div>maplist 和 mapcar 类似，不过首先将函数apply到每个列表，然后将函数apply到每个列表的cdr，然后将函数apply到每个列表的cddr。。<br />直到最短的一个列表为空为止。</div><div><p style="font-family: 文泉驿正黑; font-style: normal; line-height: normal; font-size: medium; "><a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapl"><strong>mapl</strong></a>&nbsp;is like&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#maplist"><strong>maplist</strong></a>&nbsp;except that the results of applying&nbsp;<em>function</em>&nbsp;are not accumulated;&nbsp;<em>list-1</em>&nbsp;is returned.<br /></p></div></em><em><div style="display: inline !important; "><div style="display: inline !important; "><span style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "><em><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; display: inline !important; ">mapl和maplist类似，但是返回的是第一个列表。</div></em></span></div></div></em><em><div><br /><div><p style="font-family: 文泉驿正黑; font-style: normal; line-height: normal; font-size: medium; "><a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcan"><strong>mapcan</strong></a>&nbsp;and&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcon"><strong>mapcon</strong></a>&nbsp;are like&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#mapcar"><strong>mapcar</strong></a>&nbsp;and&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm#maplist"><strong>maplist</strong></a>&nbsp;respectively, except that the results of applying&nbsp;<em>function</em>&nbsp;are combined into a&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_l.htm#list"><em>list</em></a>&nbsp;by the use of&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_nconc.htm#nconc"><strong>nconc</strong></a>&nbsp;rather than&nbsp;<a rel="DEFINITION" href="http://www.lispworks.com/documentation/HyperSpec/Body/f_list_.htm#list"><strong>list</strong></a>. That is,</p><p style="font-family: 文泉驿正黑; font-style: normal; line-height: normal; font-size: medium; "></p></div>mapcan 和 mapcon 类似于 mapcar 和 maplist。它们使用 nconc 连接结果而不是 list。<br /></div><div><span style="font-style: normal; "><strong>Examples</strong></span></div><div><span style="font-style: normal; "><pre>(mapcar #'car '((1 a) (2 b) (3 c))) =&gt;  (1 2 3)   <br />(mapcar #'abs '(3 -4 2 -5 -6)) =&gt;  (3 4 2 5 6)  <br />(mapcar #'cons '(a b c) '(1 2 3)) =&gt;  ((A . 1) (B . 2) (C . 3)) <br /><br /></pre><div><div><pre>(maplist #'append '(1 2 3 4) '(1 2) '(1 2 3))  =&gt;  ((1 2 3 4 1 2 1 2 3) (2 3 4 2 2 3)) <br />(maplist #'(lambda (x) (cons 'foo x)) '(a b c d)) =&gt;  ((FOO A B C D) (FOO B C D) (FOO C D) (FOO D))  <br />(maplist #'(lambda (x) (if (member (car x) (cdr x)) 0 1)) '(a b a c d b c)) =&gt;  (0 0 1 0 1 1 1)</pre></div></div></span></div><div><span style="font-family: 文泉驿正黑; font-style: normal; line-height: normal; font-size: medium; "><pre>(setq dummy nil) =&gt;  NIL   <br />(mapc #'(lambda (&amp;rest x) (setq dummy (append dummy x)))<br />         '(1 2 3 4)<br />         '(a b c d e)<br />         '(x y z)) =&gt;  (1 2 3 4)<br />dummy =&gt;  (1 A X 2 B Y 3 C Z)<br /><br /><div><span style="font-family: 文泉驿正黑; white-space: normal; "><pre>(setq dummy nil) =&gt;  NIL   <br />(mapl #'(lambda (x) (push x dummy)) '(1 2 3 4)) =&gt;  (1 2 3 4)   <br />dummy =&gt;  ((4) (3 4) (2 3 4) (1 2 3 4))   <br /><br />(mapcan #'(lambda (x y) (if (null x) nil (list x y)))   <br />        '(nil nil nil d e)  <br />        '(1 2 3 4 5 6)) =&gt;  (D 4 E 5)   <br />(mapcan #'(lambda (x) (and (numberp x) (list x)))  <br />        '(a 1 b c 3 4 d 5)) =&gt;  (1 3 4 5) <br /><br /></pre></span></div><div><span style="font-family: 文泉驿正黑; white-space: normal; "><pre>(mapcon #'list '(1 2 3 4)) =&gt;  ((1 2 3 4) (2 3 4) (3 4) (4))  </pre></span><p style="font-family: 文泉驿正黑; white-space: normal; "></p></div></pre></span></div><br /><br /></em><p>&nbsp;</p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "></p><p style="font-family: 文泉驿正黑; line-height: normal; font-size: medium; "></p></div><img src ="http://www.cppblog.com/varg-vikernes/aggbug/153861.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/varg-vikernes/" target="_blank">糯米</a> 2011-08-19 21:44 <a href="http://www.cppblog.com/varg-vikernes/archive/2011/08/19/153861.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>