1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/f_map.htm
2024-04-01 10:24:07 +02:00

77 lines
8.1 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 MAP</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_subseq.htm">
<LINK REL=UP HREF="c_sequen.htm">
<LINK REL=NEXT HREF="f_map_in.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_subseq.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_sequen.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_map_in.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="map"><I>Function</I> <B>MAP</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>map</B> <I>result-type function <TT>&amp;rest</TT> sequences+</I> =&gt; <I>result</I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>result-type</I> -- a <A REL=DEFINITION HREF="t_seq.htm#sequence"><B>sequence</B></A> <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>, or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
<I>function</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#function_designator"><I>function designator</I></A>. <I>function</I> must take as many arguments as there are <I>sequences</I>. <P>
<I>sequence</I>---a <A REL=DEFINITION HREF="26_glo_p.htm#proper_sequence"><I>proper sequence</I></A>. <P>
<I>result</I>---if <I>result-type</I> is a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A> other than <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, then a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> it denotes; otherwise (if the <I>result-type</I> is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>), <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
<P><B>Description:</B><P>
<P>
Applies <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>. The <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. The <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>. <P>
<A REL=DEFINITION HREF="#map"><B>map</B></A> returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> if <I>result-type</I> is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. Otherwise, <A REL=DEFINITION HREF="#map"><B>map</B></A> returns a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> such that element <TT>j</TT> is the result of applying <I>function</I> to element <TT>j</TT> of each of the <I>sequences</I>. The result <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> is as long as the shortest of the <I>sequences</I>. The consequences are undefined if the result of applying <I>function</I> to the successive elements of the <I>sequences</I> cannot be contained in a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> given by <I>result-type</I>. <P>
If the <I>result-type</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#subtype"><I>subtype</I></A> of <A REL=DEFINITION HREF="t_list.htm#list"><B>list</B></A>, the result will be a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A>. <P>
If the <I>result-type</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#subtype"><I>subtype</I></A> of <A REL=DEFINITION HREF="t_vector.htm#vector"><B>vector</B></A>, then if the implementation can determine the element type specified for the <I>result-type</I>, the element type of the resulting array is the result of <I>upgrading</I> that element type; or, if the implementation can determine that the element type is unspecified (or <TT>*</TT>), the element type of the resulting array is <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A>; otherwise, an error is signaled. <P>
<P><B>Examples:</B><P>
<P>
<PRE>
(map 'string #'(lambda (x y)
(char &quot;01234567890ABCDEF&quot; (mod (+ x y) 16)))
'(1 2 3 4)
'(10 9 8 7)) =&gt; &quot;AAAA&quot;
(setq seq '(&quot;lower&quot; &quot;UPPER&quot; &quot;&quot; &quot;123&quot;)) =&gt; (&quot;lower&quot; &quot;UPPER&quot; &quot;&quot; &quot;123&quot;)
(map nil #'nstring-upcase seq) =&gt; NIL
seq =&gt; (&quot;LOWER&quot; &quot;UPPER&quot; &quot;&quot; &quot;123&quot;)
(map 'list #'- '(1 2 3 4)) =&gt; (-1 -2 -3 -4)
(map 'string
#'(lambda (x) (if (oddp x) #\1 #\0))
'(1 2 3 4)) =&gt; &quot;1010&quot;
</PRE>
</TT> <P>
<PRE>
(map '(vector * 4) #'cons &quot;abc&quot; &quot;de&quot;) should signal an error
</PRE>
</TT> <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B><P>
<P>
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> must be signaled if the <I>result-type</I> is not a <A REL=DEFINITION HREF="26_glo_r.htm#recognizable_subtype"><I>recognizable subtype</I></A> of <A REL=DEFINITION HREF="t_list.htm#list"><B>list</B></A>, not a <A REL=DEFINITION HREF="26_glo_r.htm#recognizable_subtype"><I>recognizable subtype</I></A> of <A REL=DEFINITION HREF="t_vector.htm#vector"><B>vector</B></A>, and not <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <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>sequence</I> is not a <A REL=DEFINITION HREF="26_glo_p.htm#proper_sequence"><I>proper sequence</I></A>. <P>
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> should be signaled if <I>result-type</I> specifies the number of elements and the minimum length of the <I>sequences</I> is different from that number. <P>
<P><B>See Also:</B><P>
<P>
<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/iss302.htm">SEQUENCE-TYPE-LENGTH:MUST-MATCH</A><LI> <A REL=CHILD HREF="../Issues/iss179.htm">FUNCTION-TYPE:X3J13-MARCH-88</A><LI> <A REL=CHILD HREF="../Issues/iss073.htm">CONCATENATE-SEQUENCE:SIGNAL-ERROR</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>