133 lines
7.8 KiB
HTML
133 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 LAST-N 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/iss208_w.htm">
|
|
<LINK REL=UP HREF="../Issues/iss209.htm">
|
|
<LINK REL=NEXT HREF="../Issues/iss210_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/iss208_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss209.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss210_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
|
|
|
|
<H2>Issue LAST-N Writeup</H2>
|
|
|
|
<PRE><B>Issue:</B> <A HREF="iss209.htm">LAST-N</A><P>
|
|
<B>References:</B> <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> (p267)<P>
|
|
<B>Category:</B> ENHANCEMENT<P>
|
|
<B>Edit history:</B> 04-Dec-87, Version 1 by Pitman<P>
|
|
12-Mar-88, Version 2 by Pitman<P>
|
|
<P>
|
|
<B>Problem Description:<P>
|
|
</B><P>
|
|
People always ask why <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> returns a cons and not an element.<P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> takes an argument N but <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> does not.<P>
|
|
<P>
|
|
<B>Proposal (LAST-N:ALLOW-OPTIONAL-ARGUMENT):<P>
|
|
</B><P>
|
|
Allow <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> to take an optional argument N, saying how many cells to return.<P>
|
|
The default for N would be 1.<P>
|
|
<P>
|
|
It is an error if N is negative or L is circular.<P>
|
|
<P>
|
|
If N is zero, then the atom that terminates the list L is returned.<P>
|
|
<P>
|
|
If N is greater than or equal to the number of cons cells in the list<P>
|
|
L, then the result is L.<P>
|
|
<P>
|
|
<B>Test Cases:<P>
|
|
</B><P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A B C) 0) => ()<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> '(A B C) 0) => (A B C)<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A B C) 1) => (C)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> '(A B C) 1) => (A B)<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A B C) 2) => (B C)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> '(A B C) 2) => (A)<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A B C) 3) => (A B C)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> '(A B C) 3) => ()<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A B C) 4) => (A B C)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> '(A B C) 4) => ()<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A B C)) => (C)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> '(A B C)) => (A B)<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A . B) 0) => B<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A . B) 1) => (A . B)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> '(A . B) 2) => (A . B)<P>
|
|
<P>
|
|
<B>Rationale:<P>
|
|
</B><P>
|
|
<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> and <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> would select complementary parts of a list in general.<P>
|
|
That is (<A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A> L (<A REL=DEFINITION HREF="../Body/f_append.htm#append"><B>APPEND</B></A> (<A REL=DEFINITION HREF="../Body/f_butlas.htm#butlast"><B>BUTLAST</B></A> L N) (<A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> L N))) would be T for N <A REL=DEFINITION HREF="../Body/f_eq_sle.htm#GTEQ"><B>>=</B></A> 0<P>
|
|
and L being a proper list.<P>
|
|
<P>
|
|
This would make it more obvious why <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> should return a list and not<P>
|
|
an element. ie, it would return the "last N elements" where N=1 by default.<P>
|
|
<P>
|
|
<B>Current Practice:<P>
|
|
</B><P>
|
|
Probably nobody does this.<P>
|
|
<P>
|
|
<B>Adoption Cost:<P>
|
|
</B><P>
|
|
Very slight. The following code would suffice:<P>
|
|
<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> <A REL=DEFINITION HREF="../Body/f_last.htm#last"><B>LAST</B></A> (<A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>LIST</B></A> <A REL=DEFINITION HREF="../Body/03_da.htm#AMoptional"><B>&OPTIONAL</B></A> (N 1))<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_check_.htm#check-type"><B>CHECK-TYPE</B></A> N (<A REL=DEFINITION HREF="../Body/t_intege.htm#integer"><B>INTEGER</B></A> 0))<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_do_do.htm#do"><B>DO</B></A> ((L <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>LIST</B></A> (<A REL=DEFINITION HREF="../Body/f_car_c.htm#cdr"><B>CDR</B></A> L))<P>
|
|
(R <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>LIST</B></A>)<P>
|
|
(I 0 (+ I 1)))<P>
|
|
((<A REL=DEFINITION HREF="../Body/a_atom.htm#atom"><B>ATOM</B></A> L) R)<P>
|
|
(<A REL=DEFINITION HREF="../Body/s_if.htm#if"><B>IF</B></A> (<A REL=DEFINITION HREF="../Body/f_eq_sle.htm#GTEQ"><B>>=</B></A> I N) (<A REL=DEFINITION HREF="../Body/m_pop.htm#pop"><B>POP</B></A> R))))<P>
|
|
<P>
|
|
Some implementations might want to <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A> compiler optimizations for<P>
|
|
the N=1 case.<P>
|
|
<P>
|
|
<B>Benefits:<P>
|
|
</B><P>
|
|
This utility is probably needed often enough to warrant its inclusion.<P>
|
|
It's present (as a separate function called NLEFT) in Symbolics Common<P>
|
|
Lisp and Interlisp.<P>
|
|
<P>
|
|
<B>Conversion Cost:<P>
|
|
</B><P>
|
|
None. This is an upward compatible extension.<P>
|
|
<P>
|
|
<B>Aesthetics:<P>
|
|
</B><P>
|
|
This would make things a little prettier.<P>
|
|
<P>
|
|
<B>Discussion:<P>
|
|
</B><P>
|
|
This suggestion came up recently on a Symbolics design discussion list.<P>
|
|
Symbolics is contemplating offering it as an extension, but it seemed like<P>
|
|
the rest of the CL community might want to adopt it, too. Pitman thinks<P>
|
|
it's a nice idea.<P>
|
|
<P>
|
|
Masinter opposes this extension as gratuitous.<P>
|
|
<P>
|
|
Moon and Daniels think this is very low priority but have expressed a<P>
|
|
lack of major objection to this proposal.<P>
|
|
<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>
|