190 lines
12 KiB
HTML
190 lines
12 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 RANGE-OF-COUNT-KEYWORD 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/iss282_w.htm">
|
|
<LINK REL=UP HREF="../Issues/iss283.htm">
|
|
<LINK REL=NEXT HREF="../Issues/iss284_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/iss282_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss283.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss284_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
|
|
|
|
<H2>Issue RANGE-OF-COUNT-KEYWORD Writeup</H2>
|
|
|
|
<PRE><B>Issue:</B> <A HREF="iss283.htm">RANGE-OF-COUNT-KEYWORD</A><P>
|
|
<B>References:</B> :COUNT (p247), REMOVE[-xxx] (p253), DELETE[-xxx] (p254),<P>
|
|
[N]SUBSTITUTE[-xxx] (pp255-256)<P>
|
|
<B>Category:</B> CLARIFICATION<P>
|
|
<B>Edit history:</B> 21-Aug-88, Version 1 by Dave Touretzky<P>
|
|
22-Aug-88, Version 2 by Pitman<P>
|
|
09-Oct-88, Version 3 by Pitman<P>
|
|
<B>Status:</B> For Internal Discussion<P>
|
|
<P>
|
|
<B>Problem Description:<P>
|
|
</B><P>
|
|
CLtL is overly vague about legal values for the :COUNT keyword<P>
|
|
parameters to builtin functions such as the sequence functions. It<P>
|
|
says that the keyword ``limits the number of elements [affected]''.<P>
|
|
Implementations have varied in their interpretation of this phrase,<P>
|
|
however.<P>
|
|
<P>
|
|
CLtL p247 specifies that if the :COUNT parameter to functions such<P>
|
|
as <A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> and <A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete"><B>DELETE</B></A> ``is <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> or is not supplied, all matching items<P>
|
|
are affected.'' Because of the placement of this requirement<P>
|
|
outside of the description of the functions affected, some<P>
|
|
implementations have overlooked this requirement and had to be <P>
|
|
changed later.<P>
|
|
<P>
|
|
CLtL doesn't say explicitly that the value of :COUNT must be an<P>
|
|
integer, nor does it say what to do for negative values.<P>
|
|
<P>
|
|
The fact that reasonable implementations disagree on some of the<P>
|
|
details make this an obvious candidate for cleanup.<P>
|
|
<P>
|
|
<B>Proposal (RANGE-OF-COUNT-KEYWORD:NIL-OR-INTEGER):<P>
|
|
</B><P>
|
|
Clarify that for the functions<P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> <A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove-if"><B>REMOVE-IF</B></A> <A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove-if-not"><B>REMOVE-IF-NOT</B></A><P>
|
|
<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete"><B>DELETE</B></A> <A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete-if"><B>DELETE-IF</B></A> <A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete-if-not"><B>DELETE-IF-NOT</B></A><P>
|
|
<A REL=DEFINITION HREF="../Body/f_sbs_s.htm#substitute"><B>SUBSTITUTE</B></A> <A REL=DEFINITION HREF="../Body/f_sbs_s.htm#substitute-if"><B>SUBSTITUTE-IF</B></A> <A REL=DEFINITION HREF="../Body/f_sbs_s.htm#substitute-if-not"><B>SUBSTITUTE-IF-NOT</B></A><P>
|
|
<A REL=DEFINITION HREF="../Body/f_sbs_s.htm#nsubstitute"><B>NSUBSTITUTE</B></A> <A REL=DEFINITION HREF="../Body/f_sbs_s.htm#nsubstitute-if"><B>NSUBSTITUTE-IF</B></A> <A REL=DEFINITION HREF="../Body/f_sbs_s.htm#nsubstitute-if-not"><B>NSUBSTITUTE-IF-NOT</B></A><P>
|
|
<P>
|
|
the following restrictions on the :COUNT keyword parameter exist:<P>
|
|
<P>
|
|
* The value of this parameter must be <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> or an integer.<P>
|
|
<P>
|
|
* Using a negative integer value is functionally equivalent to<P>
|
|
using a value of zero.<P>
|
|
<P>
|
|
<B>Test Case:<P>
|
|
</B><P>
|
|
#1: (<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> 'A '(A B A B) :COUNT 0) => (A B A B)<P>
|
|
#2: (<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> 'A '(A B A B) :COUNT -3) => (A B A B)<P>
|
|
#3: (<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> 'A '(A B A B) :COUNT <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>) => (B B)<P>
|
|
#4: (<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> 'A '(A B A B) :COUNT 1.0) is an error.<P>
|
|
#5: (<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> 'A '(A B A B) :COUNT 'FOO) is an error.<P>
|
|
<P>
|
|
<B>Rationale:<P>
|
|
</B><P>
|
|
Disallowing non-integer numbers is probably the original intent and<P>
|
|
is consistent with other functions such as <A REL=DEFINITION HREF="../Body/f_nth.htm#nth"><B>NTH</B></A> (p265) which accept<P>
|
|
count-like arguments that are explicitly required to integers. This<P>
|
|
restriction would presumably permit better optimizations in low-safety<P>
|
|
mode on stock hardware.<P>
|
|
<P>
|
|
Allowing <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> to be equivalent to no value allows a simplified flow<P>
|
|
of control in some situations. For example, one can write<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> MYDEL (ITEM <A REL=DEFINITION HREF="../Body/03_da.htm#AMoptional"><B>&OPTIONAL</B></A> <A REL=DEFINITION HREF="../Body/f_countc.htm#count"><B>COUNT</B></A>)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete"><B>DELETE</B></A> ITEM *MYLIST* :COUNT <A REL=DEFINITION HREF="../Body/f_countc.htm#count"><B>COUNT</B></A>))<P>
|
|
where otherwise it might be necessary to write something like<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> MYDEL (ITEM <A REL=DEFINITION HREF="../Body/03_da.htm#AMoptional"><B>&OPTIONAL</B></A> (<A REL=DEFINITION HREF="../Body/f_countc.htm#count"><B>COUNT</B></A> <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A> COUNT-P))<P>
|
|
(<A REL=DEFINITION HREF="../Body/s_if.htm#if"><B>IF</B></A> COUNT-P<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete"><B>DELETE</B></A> ITEM *MYLIST* :COUNT <A REL=DEFINITION HREF="../Body/f_countc.htm#count"><B>COUNT</B></A>)<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#delete"><B>DELETE</B></A> ITEM *MYLIST*)))<P>
|
|
<P>
|
|
Allowing negative numbers frees users from having to do an explicit<P>
|
|
check for negative numbers when the value of :COUNT is computed by<P>
|
|
some complicated expression. For example:<P>
|
|
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> LEAVE-AT-MOST (N ITEM <A REL=DEFINITION HREF="../Body/t_seq.htm#sequence"><B>SEQUENCE</B></A> <A REL=DEFINITION HREF="../Body/03_da.htm#AMkey"><B>&KEY</B></A> (FROM-END T))<P>
|
|
(<A REL=DEFINITION HREF="../Body/f_rm_rm.htm#remove"><B>REMOVE</B></A> ITEM <A REL=DEFINITION HREF="../Body/t_seq.htm#sequence"><B>SEQUENCE</B></A> <P>
|
|
:COUNT (<A REL=DEFINITION HREF="../Body/f_wr_pr.htm#print"><B>PRINT</B></A> (- (<A REL=DEFINITION HREF="../Body/f_countc.htm#count"><B>COUNT</B></A> ITEM <A REL=DEFINITION HREF="../Body/t_seq.htm#sequence"><B>SEQUENCE</B></A>) N))<P>
|
|
:FROM-END FROM-END))<P>
|
|
(LEAVE-AT-MOST 2 #\A "BANANAS") ==> "BANANS"<P>
|
|
(LEAVE-AT-MOST 2 #\S "BANANAS") ==> "BANANAS"<P>
|
|
<P>
|
|
<B>Current Practice:<P>
|
|
</B><P>
|
|
[Note: Pitman didn't try these examples in KCL or CMU Common Lisp. He's<P>
|
|
working from data in Touretzky's last draft of this proposal, so someone<P>
|
|
from those camps might want to test the assertions made here.]<P>
|
|
<P>
|
|
#1: All correct implementations presumably return (A B A B).<P>
|
|
This value is consisent with this proposal.<P>
|
|
<P>
|
|
#2: Symbolics Cloe returns (A B A B).<P>
|
|
KCL returns (A B A B) for lists.<P>
|
|
This value is forced by this proposal.<P>
|
|
<P>
|
|
Symbolics Genera and CMU Common Lisp return (B B).<P>
|
|
KCL does something bizarre for vectors (``pads with n blanks or<P>
|
|
NILs, where -n is the value of the :count keyword parameter'',<P>
|
|
says Touretzky.)<P>
|
|
These implementations would have to change.<P>
|
|
<P>
|
|
#3: All correct implementations presumably return (B B).<P>
|
|
This value is consisent with this proposal.<P>
|
|
<P>
|
|
Some implementations have been known to signal a wrong type<P>
|
|
argument error in the past, but have presumably been fixed.<P>
|
|
<P>
|
|
#4: Symbolics Genera and Symbolics Cloe return (B A B).<P>
|
|
CMU Common Lisp returns (B B).<P>
|
|
These behaviors are consistent with this proposal.<P>
|
|
<P>
|
|
#5: Symbolics Cloe and Symbolics Genera signal an error.<P>
|
|
CMU Common Lisp returns (A B A B).<P>
|
|
These behaviors are consistent with this proposal.<P>
|
|
<P>
|
|
<P>
|
|
<B>Cost to Implementors:<P>
|
|
</B><P>
|
|
Some implementations would have to change. These functions are typically<P>
|
|
heavily optimized by compilers. Not only source code, but also compiler<P>
|
|
optimizers and perhaps even microcode or hardware might have to be<P>
|
|
modified to fully accomodate this change, so it might be quite expensive.<P>
|
|
<P>
|
|
<B>Cost to Users:<P>
|
|
</B><P>
|
|
None for Common Lisp users. This change is an upward compatible<P>
|
|
clarification of <A REL=DEFINITION HREF="../Body/07_ffb.htm#standard"><B>standard</B></A> practice.<P>
|
|
<P>
|
|
<B>Cost of Non-Adoption:<P>
|
|
</B><P>
|
|
The behavior of these functions when given degenerate keyword values would<P>
|
|
be unintuitive. In many such cases, considerable additional user code must<P>
|
|
be written to watch for and avoid creating such situations.<P>
|
|
<P>
|
|
<B>Benefits:<P>
|
|
</B><P>
|
|
More compact, more intuitive, and more portable code.<P>
|
|
<P>
|
|
<B>Aesthetics:<P>
|
|
</B><P>
|
|
This change improves language aesthetics.<P>
|
|
<P>
|
|
<B>Discussion:<P>
|
|
</B><P>
|
|
In the past there has been some argument about what <A REL=DEFINITION HREF="../Body/f_subseq.htm#subseq"><B>SUBSEQ</B></A> should do when<P>
|
|
given positions greater than the length of the sequence. Currently it <P>
|
|
"is an error" to specify positions less than zero or greater than the<P>
|
|
length of the sequence. Touretzky doesn't think the same should apply to<P>
|
|
the :COUNT keyword. The inputs to <A REL=DEFINITION HREF="../Body/f_subseq.htm#subseq"><B>SUBSEQ</B></A> are ordinal numbers: they specify<P>
|
|
positions, like array subscripts. The value of :COUNT is not an ordinal,<P>
|
|
it is an upper bound on the size of the set of affected items (which is<P>
|
|
a cardinal number).<P>
|
|
<P>
|
|
Pitman supports this proposal. [Hopefully Touretzky supports it, too?]<P>
|
|
<P>
|
|
van Roggen says he personally supports the stated proposal but that a<P>
|
|
survey he did of users at DEC showed up a number of people who thought<P>
|
|
that negative count arguments should be an error.<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>
|