112 lines
9.6 KiB
HTML
112 lines
9.6 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Function MAPC, MAPCAR, MAPCAN, MAPL...</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="f_mem_m.htm">
|
|
<LINK REL=UP HREF="c_conses.htm">
|
|
<LINK REL=NEXT HREF="f_acons.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_mem_m.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_conses.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_acons.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="mapc"><A NAME="mapcar"><A NAME="mapcan"><A NAME="mapl"><A NAME="maplist"><A NAME="mapcon"><I>Function</I> <B>MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON</B></A></A></A></A></A></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>mapc</B> <I>function <TT>&rest</TT> lists+</I> => <I>list-1</I><P>
|
|
|
|
<B>mapcar</B> <I>function <TT>&rest</TT> lists+</I> => <I>result-list</I><P>
|
|
|
|
<B>mapcan</B> <I>function <TT>&rest</TT> lists+</I> => <I>concatenated-results</I><P>
|
|
|
|
<B>mapl</B> <I>function <TT>&rest</TT> lists+</I> => <I>list-1</I><P>
|
|
|
|
<B>maplist</B> <I>function <TT>&rest</TT> lists+</I> => <I>result-list</I><P>
|
|
|
|
<B>mapcon</B> <I>function <TT>&rest</TT> lists+</I> => <I>concatenated-results</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>function</I>---a <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designator</I></A> for a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> that must take as many <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A> as there are <I>lists</I>. <P>
|
|
<I>list</I>---a <A REL=DEFINITION HREF="26_glo_p.htm#proper_list"><I>proper list</I></A>. <P>
|
|
<I>list-1</I>---the first <I>list</I> (which must be a <A REL=DEFINITION HREF="26_glo_p.htm#proper_list"><I>proper list</I></A>). <P>
|
|
<I>result-list</I>---a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A>. <P>
|
|
<I>concatenated-results</I>---a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
The mapping operation involves applying <I>function</I> to successive sets of arguments in which one argument is obtained from each <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A>. Except for <A REL=DEFINITION HREF="#mapc"><B>mapc</B></A> and <A REL=DEFINITION HREF="#mapl"><B>mapl</B></A>, the result contains the results returned by <I>function</I>. In the cases of <A REL=DEFINITION HREF="#mapc"><B>mapc</B></A> and <A REL=DEFINITION HREF="#mapl"><B>mapl</B></A>, the resulting <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> is <I>list</I>. <P>
|
|
<I>function</I> is called first on all the elements with index <TT>0</TT>, then on all those with index <TT>1</TT>, and so on. <I>result-type</I> specifies the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> of the resulting <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A>. If <I>function</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A>, it is <A REL=DEFINITION HREF="f_coerce.htm#coerce"><B>coerce</B></A>d to a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> as if by <A REL=DEFINITION HREF="f_symb_1.htm#symbol-function"><B>symbol-function</B></A>. <P>
|
|
<A REL=DEFINITION HREF="#mapcar"><B>mapcar</B></A> operates on successive <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>elements</I></A> of the <I>lists</I>. <I>function</I> is applied to the first <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A> of each <I>list</I>, then to the second <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A> of each <I>list</I>, and so on. The iteration terminates when the shortest <I>list</I> runs out, and excess elements in other lists are ignored. The value returned by <A REL=DEFINITION HREF="#mapcar"><B>mapcar</B></A> is a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> of the results of successive calls to <I>function</I>. <P>
|
|
<A REL=DEFINITION HREF="#mapc"><B>mapc</B></A> is like <A REL=DEFINITION HREF="#mapcar"><B>mapcar</B></A> except that the results of applying <I>function</I> are not accumulated. The <I>list</I> argument is returned. <P>
|
|
<A REL=DEFINITION HREF="#maplist"><B>maplist</B></A> is like <A REL=DEFINITION HREF="#mapcar"><B>mapcar</B></A> except that <I>function</I> is applied to successive sublists of the <I>lists</I>. <I>function</I> is first applied to the <I>lists</I> themselves, and then to the <A REL=DEFINITION HREF="26_glo_c.htm#cdr"><I>cdr</I></A> of each <I>list</I>, and then to the <A REL=DEFINITION HREF="26_glo_c.htm#cdr"><I>cdr</I></A> of the <A REL=DEFINITION HREF="26_glo_c.htm#cdr"><I>cdr</I></A> of each <I>list</I>, and so on. <P>
|
|
<A REL=DEFINITION HREF="#mapl"><B>mapl</B></A> is like <A REL=DEFINITION HREF="#maplist"><B>maplist</B></A> except that the results of applying <I>function</I> are not accumulated; <I>list-1</I> is returned. <P>
|
|
<A REL=DEFINITION HREF="#mapcan"><B>mapcan</B></A> and <A REL=DEFINITION HREF="#mapcon"><B>mapcon</B></A> are like <A REL=DEFINITION HREF="#mapcar"><B>mapcar</B></A> and <A REL=DEFINITION HREF="#maplist"><B>maplist</B></A> respectively, except that the results of applying <I>function</I> are combined into a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> by the use of <A REL=DEFINITION HREF="f_nconc.htm#nconc"><B>nconc</B></A> rather than <A REL=DEFINITION HREF="f_list_.htm#list"><B>list</B></A>. That is, <P>
|
|
<PRE>
|
|
(mapcon f x1 ... xn)
|
|
== (apply #'nconc (maplist f x1 ... xn))
|
|
</PRE>
|
|
</TT> and similarly for the relationship between <A REL=DEFINITION HREF="#mapcan"><B>mapcan</B></A> and <A REL=DEFINITION HREF="#mapcar"><B>mapcar</B></A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(mapcar #'car '((1 a) (2 b) (3 c))) => (1 2 3)
|
|
(mapcar #'abs '(3 -4 2 -5 -6)) => (3 4 2 5 6)
|
|
(mapcar #'cons '(a b c) '(1 2 3)) => ((A . 1) (B . 2) (C . 3))
|
|
|
|
(maplist #'append '(1 2 3 4) '(1 2) '(1 2 3))
|
|
=> ((1 2 3 4 1 2 1 2 3) (2 3 4 2 2 3))
|
|
(maplist #'(lambda (x) (cons 'foo x)) '(a b c d))
|
|
=> ((FOO A B C D) (FOO B C D) (FOO C D) (FOO D))
|
|
(maplist #'(lambda (x) (if (member (car x) (cdr x)) 0 1)) '(a b a c d b c))
|
|
=> (0 0 1 0 1 1 1)
|
|
;An entry is 1 if the corresponding element of the input
|
|
; list was the last instance of that element in the input list.
|
|
|
|
(setq dummy nil) => NIL
|
|
(mapc #'(lambda (&rest x) (setq dummy (append dummy x)))
|
|
'(1 2 3 4)
|
|
'(a b c d e)
|
|
'(x y z)) => (1 2 3 4)
|
|
dummy => (1 A X 2 B Y 3 C Z)
|
|
|
|
(setq dummy nil) => NIL
|
|
(mapl #'(lambda (x) (push x dummy)) '(1 2 3 4)) => (1 2 3 4)
|
|
dummy => ((4) (3 4) (2 3 4) (1 2 3 4))
|
|
|
|
(mapcan #'(lambda (x y) (if (null x) nil (list x y)))
|
|
'(nil nil nil d e)
|
|
'(1 2 3 4 5 6)) => (D 4 E 5)
|
|
(mapcan #'(lambda (x) (and (numberp x) (list x)))
|
|
'(a 1 b c 3 4 d 5))
|
|
=> (1 3 4 5)
|
|
</PRE>
|
|
</TT> In this case the function serves as a filter; this is a standard Lisp idiom using <A REL=DEFINITION HREF="#mapcan"><B>mapcan</B></A>. <P>
|
|
<PRE>
|
|
(mapcon #'list '(1 2 3 4)) => ((1 2 3 4) (2 3 4) (3 4) (4))
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
Should be prepared to signal an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> if any <I>list</I> is not a <A REL=DEFINITION HREF="26_glo_p.htm#proper_list"><I>proper list</I></A>. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_dolist.htm#dolist"><B>dolist</B></A>, <A REL=DEFINITION HREF="f_map.htm#map"><B>map</B></A>, <A REL=CHILD HREF="03_f.htm">Section 3.6 (Traversal Rules and Side Effects)</A> <P>
|
|
<P><B>Notes:</B> None.
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss240.htm">MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE</A><LI> <A REL=CHILD HREF="../Issues/iss179.htm">FUNCTION-TYPE:X3J13-MARCH-88</A><LI> <A REL=CHILD HREF="../Issues/iss138.htm">DOTTED-LIST-ARGUMENTS:CLARIFY</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|