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

142 lines
7.8 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: Issue NTH-VALUE Writeup</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="../Issues/iss249_w.htm">
<LINK REL=UP HREF="../Issues/iss250.htm">
<LINK REL=NEXT HREF="../Issues/iss251_w.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="../Issues/iss249_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss250.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss251_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue NTH-VALUE Writeup</H2>
<PRE><B>Status:</B> Passed, as amended, Jan 89 X3J13<P>
<B>Issue:</B> <A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A><P>
<B>References:</B> Multiple values, pp. 133-139<P>
<B>Category:</B> ADDITION<P>
<B>Edit history:</B> 16-Aug-88, Version 1 by Pierson<P>
01-Oct-88, Version 2 by Pitman (minor edits)<P>
5-Oct-88, Version 3 by Masinter<P>
(add Current Practice as per Gray)<P>
8-Dec-88, Version 4 by Masinter<P>
(add comments to discussion)<P>
17-Mar-89, Version 5, Masinter (as amended)<P>
<P>
<B>Problem description:<P>
</B><P>
The set of operations on multiple values in Common Lisp is incomplete:<P>
<P>
The only ways to retrieve just one of several return values (other than<P>
the first) are:<P>
<P>
- Bind several variables and then ignore all but one.<P>
eg, (<A REL=DEFINITION HREF="../Body/m_multip.htm#multiple-value-bind"><B>MULTIPLE-VALUE-BIND</B></A> (X Y Z) &lt;exp&gt; (<A REL=DEFINITION HREF="../Body/s_declar.htm#declare"><B>DECLARE</B></A> (<A REL=DEFINITION HREF="../Body/d_ignore.htm#ignore"><B>IGNORE</B></A> X Y)) Z)<P>
This is somewhat cumbersome to write and not perspicuous.<P>
<P>
- Get a list of all return values and select from that.<P>
eg, (<A REL=DEFINITION HREF="../Body/f_firstc.htm#third"><B>THIRD</B></A> (<A REL=DEFINITION HREF="../Body/m_mult_1.htm#multiple-value-list"><B>MULTIPLE-VALUE-LIST</B></A> &lt;exp&gt;))<P>
This is somewhat cumbersome, not perspicuous, and creates<P>
needless garbage.<P>
<P>
<B>Proposal (NTH-VALUE:ADD):<P>
</B><P>
Add a new macro <A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A>, described as<P>
<P>
<A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A> n form [Macro]<P>
<P>
N must be a non-negative integer.<P>
Evaluates the FORM and returns the Nth value returned by the form as<P>
a single value. N is 0-based, i.e. the first returned value is <P>
value 0 (for consistency with <A REL=DEFINITION HREF="../Body/f_nth.htm#nth"><B>NTH</B></A> and <A REL=DEFINITION HREF="../Body/f_nthcdr.htm#nthcdr"><B>NTHCDR</B></A>). Both N and FORM are<P>
evaluated, in left-to-right order.<P>
<P>
<A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A> returns <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> if N is greater than or equal to the number<P>
of values returned by FORM.<P>
<P>
<B>Examples:<P>
</B><P>
With this proposal <A REL=DEFINITION HREF="../Body/a_mod.htm#mod"><B>MOD</B></A> could be defined as:<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> <A REL=DEFINITION HREF="../Body/a_mod.htm#mod"><B>MOD</B></A> (<A REL=DEFINITION HREF="../Body/t_number.htm#number"><B>NUMBER</B></A> DIVISOR)<P>
(<A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A> 1 (<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>FLOOR</B></A> <A REL=DEFINITION HREF="../Body/t_number.htm#number"><B>NUMBER</B></A> DIVISOR)))<P>
<P>
The same code would currently be:<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> <A REL=DEFINITION HREF="../Body/a_mod.htm#mod"><B>MOD</B></A> (<A REL=DEFINITION HREF="../Body/t_number.htm#number"><B>NUMBER</B></A> DIVISOR)<P>
(<A REL=DEFINITION HREF="../Body/m_multip.htm#multiple-value-bind"><B>MULTIPLE-VALUE-BIND</B></A> (DIVIDEND REMAINDER)<P>
(<A REL=DEFINITION HREF="../Body/f_floorc.htm#floor"><B>FLOOR</B></A> <A REL=DEFINITION HREF="../Body/t_number.htm#number"><B>NUMBER</B></A> DIVISOR)<P>
(<A REL=DEFINITION HREF="../Body/s_declar.htm#declare"><B>DECLARE</B></A> (<A REL=DEFINITION HREF="../Body/d_ignore.htm#ignore"><B>IGNORE</B></A> DIVIDEND))<P>
REMAINDER))<P>
<P>
<B>Rationale:<P>
</B><P>
This corrects the stated problem.<P>
<P>
<B>Current practice:<P>
</B><P>
The TI Explorer and LMI Lambda have this feature.<P>
<P>
<B>Cost to Implementors:<P>
</B><P>
Writing the macro version is fairly straightforward.<P>
<P>
Some will choose to implement compiler hooks so that code written with<P>
<A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A> will be as efficient as possible. This may involve some<P>
additional work, but presumably nothing major.<P>
<P>
<B>Cost to Users:<P>
</B><P>
This is an upward-compatible change.<P>
<P>
<B>Cost of non-Adoption:<P>
</B><P>
The occassional code where this comes up may be one or more of <P>
clumsier to write, more difficult to read, or less efficient.<P>
(The feature is rarely used in implementations that have it.)<P>
<P>
<B>Benefits:<P>
</B><P>
The cost of non-adoption is avoided.<P>
<P>
<B>Aesthetics:<P>
</B><P>
While it does add another function to the language it removes<P>
some need for the hairier multiple-value forms.<P>
<P>
<B>Discussion:<P>
</B><P>
Pitman proposed this in the very late pre-CLtL days. It was<P>
rejected then because it was too late in the cycle.<P>
<P>
There was not strong sentiment for including this feature<P>
in Common Lisp, but no active opposition.<P>
<P>
Comments at the October 1988 X3J13 meeting:<P>
<P>
&quot;Trivial, gratuitous.&quot;<P>
<P>
&quot;Not trivial -- allows index computation. Hard to do this<P>
in a portable, efficient way.&quot;<P>
<P>
&quot;Says he has an <A REL=DEFINITION HREF="../Body/m_nth_va.htm#nth-value"><B>NTH-VALUE</B></A> macro for a portable system that he<P>
uses (which exploits the computed index feature) and that it's<P>
a gross kludge in one implementation to make it efficient.&quot;<P>
</PRE>
<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>