[comp.lang.lisp] Correct LisP

D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE (Johnny Billquist) (03/11/90)

In article <14112@cbnewsc.ATT.COM> lgm@cbnewsc.ATT.COM (lawrence.g.mayka,ihp,) writes:
>One of the most important quality measures of a Common Lisp
>compiler is the level of safety it supports without an undue
>sacrifice in execution speed.  The better CL implementations score
>quite highly on this scale, especially on newer processor
>architectures such as SPARC.  For example, the better compilers
>indeed ensure that CAR and CDR are only applied to lists, without
>a loss of execution speed and even though a CL implementation must
>permit one to apply CAR and CDR to the symbol NIL.

You might consider me picky, but someone might feel this is
important.

The correct argument to CAR and CDR is *not* lists, but
s-expr (punctuated pairs to be specific). The fact that most
(if not all) LisPs implement lists by punctuated pairs
is a feature you should not rely upon. That is why the
functions FIRST and REST exists. Sure, they do just CAR
and CDR, but what if you stumles upon a LisP which implement
lists in another way? If you should do it *really* right,
CAR and CDR should not be able to work on lists at all,
however convenient it might be.

Anybody who cares to flame can do it to me personally.

======================================================================
Everybody know that the DECstation is a pdp8, which is a RISC,
but where did MIPS computers get into it?

	- Johnny Billquist

D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE
======================================================================

moore%cdr.utah.edu@cs.utah.edu (Tim Moore) (03/13/90)

In article <12572711825024@AIDA.CSD.UU.SE> D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE (Johnny Billquist) writes:
>In article <14112@cbnewsc.ATT.COM> lgm@cbnewsc.ATT.COM (lawrence.g.mayka,ihp,) writes:
>The correct argument to CAR and CDR is *not* lists, but
>s-expr (punctuated pairs to be specific). The fact that most
>(if not all) LisPs implement lists by punctuated pairs
>is a feature you should not rely upon. That is why the
>functions FIRST and REST exists. Sure, they do just CAR
>and CDR, but what if you stumles upon a LisP which implement
>lists in another way? If you should do it *really* right,
>CAR and CDR should not be able to work on lists at all,
>however convenient it might be.
>
>Anybody who cares to flame can do it to me personally.

I don't know what Lisps you're refering to, but Common Lisp isn't one
of them. The argument to CAR and CDR is LISTP, which means its either
a cons pair or NIL. How could lists be implemented in some other way?
If some sort of cdr-coding trick or array scheme is used internally in the
implementation, CAR and CDR better still work, or the implementation
is quite broken.

FIRST and REST are for quiche eaters :-) Or, with only have a smiley,
for non-Lisp programmers who are slumming.

>D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE

Tim Moore                     moore@cs.utah.edu {bellcore,hplabs}!utah-cs!moore
"Ah, youth. Ah, statute of limitations."
		-John Waters

utility@quiche.cs.mcgill.ca (Ronald BODKIN) (03/13/90)

In article <1990Mar12.104518.1412@hellgate.utah.edu> moore%cdr.utah.edu@cs.utah.edu (Tim Moore) writes:
>I don't know what Lisps you're refering to, but Common Lisp isn't one
>of them. The argument to CAR and CDR is LISTP, which means its either
>a cons pair or NIL. How could lists be implemented in some other way?
>If some sort of cdr-coding trick or array scheme is used internally in the
>implementation, CAR and CDR better still work, or the implementation
>is quite broken.
>
>FIRST and REST are for quiche eaters :-) Or, with only have a smiley,
>for non-Lisp programmers who are slumming.
	The problem with this is that you lose something conceptually.
I don't think anyone will ever invent a lisp where lists aren't cons'd
together (can you imagine how little extant lisp code would work).
However, the author of Anatomy of Lisp, makes a good point that if you
are trying to deal with abstract things (e.g. lists) then don't "pun"
and say "its really just a ..."  The question does arise, however,
whether or not car/cdr are invalid peeking or whether or not they are
so well established that we can consider them overlloaded.  I certainly
use them (if only due to the convenience of cXXXr) and if one wasn't
to use them, then one would have to replace cons and just about anything
which can be used on dotted pairs.  The issue gets more emphatic when
people start to say "well I know that a window structure is just a list:
(x y dx dy textcolor)" and rely on this kind of thing.
	Ron

lgm@cbnewsc.ATT.COM (lawrence.g.mayka) (03/13/90)

In article <12572711825024@AIDA.CSD.UU.SE> D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE (Johnny Billquist) writes:
>The correct argument to CAR and CDR is *not* lists, but
>s-expr (punctuated pairs to be specific). The fact that most
>(if not all) LisPs implement lists by punctuated pairs
>is a feature you should not rely upon. That is why the
>functions FIRST and REST exists. Sure, they do just CAR
>and CDR, but what if you stumles upon a LisP which implement
>lists in another way? If you should do it *really* right,
>CAR and CDR should not be able to work on lists at all,
>however convenient it might be.

Your argument is conceptually appealing, and well describes my own
programming practice:  I use FIRST and REST for the ubiquitous
list, and reserve CAR and CDR for intentional dotted pairs (such
as the constituents of an association list).  Formally, however, I
must quote "Common Lisp: the Language - Second Edition":

	p. 415:  "FIRST is the same as CAR, SECOND is the same
			as CADR..."

	p. 416:  "REST means the same as CDR but mnemonically
			complements FIRST."

Apparently the connection between dotted pairs and lists is deeply
ingrained into Common Lisp.


	Lawrence G. Mayka
	AT&T Bell Laboratories
	lgm@ihlpf.att.com

Standard disclaimer.

jeff@aiai.ed.ac.uk (Jeff Dalton) (03/14/90)

In article <12572711825024@AIDA.CSD.UU.SE> D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE (Johnny Billquist) writes:
>The correct argument to CAR and CDR is *not* lists, but
>s-expr (punctuated pairs to be specific). 

Ok so far.

>The fact that most (if not all) LisPs implement lists by
>punctuated pairs is a feature you should not rely upon.

Why not?  Indeed, in Lisp lists just are dotted pairs used
in a certain way.  You might argue that "list" ought to be
more abstract, but it isn't.

>That is why the functions FIRST and REST exists. Sure, they do just
>CAR and CDR, but what if you stumles upon a LisP which implement
>lists in another way? 

Most Lisps don't have FIRST and REST.

A CL or Scheme that implements lists some other way wouldn't be a
conforming implementation.

-- Jeff

jeff@aiai.ed.ac.uk (Jeff Dalton) (03/14/90)

In article <2452@quiche.cs.mcgill.ca> utility@quiche.cs.mcgill.ca (Ronald BODKIN) writes:
>However, the author of Anatomy of Lisp, makes a good point that if you
>are trying to deal with abstract things (e.g. lists) then don't "pun"
>and say "its really just a ..."

One certainly shouldn't use CAR and CDR for some abstractions, but
why not use them on lists?  When would you actually allow me to use
CAR and CDR?

mesard@bbn.com (Wayne Mesard) (03/15/90)

jeff@aiai.UUCP (Jeff Dalton) writes:
>why not use them on lists?  When would you actually allow me to use
>CAR and CDR?

Thursdays between 1:30 and 5:30pm.

Wayne();

utility@quiche.cs.mcgill.ca (Ronald BODKIN) (03/15/90)

In article <2035@skye.ed.ac.uk> jeff@aiai.UUCP (Jeff Dalton) writes:
>One certainly shouldn't use CAR and CDR for some abstractions, but
>why not use them on lists?  When would you actually allow me to use
>CAR and CDR?
	Its a judgement call.  I think the CxxxR usage is reasonable
(if you like, consider it an overloaded operator) and since there is
not a soul in the lisp world who doesn't know what these do, it is
pretty safe.  Indeed, does common lisp have something ('join') that is
like cons but prepends an object to the head of only a LISP?  If not,
one would have use such also to isolate lists.
		Ron

jeff@aiai.ed.ac.uk (Jeff Dalton) (03/15/90)

In article <53508@bbn.COM> mesard@labs-n.bbn.com (Wayne Mesard) writes:
 >jeff@aiai.UUCP (Jeff Dalton) writes:
 >>why not use them on lists?  When would you actually allow me to use
 >>CAR and CDR?
 >
 >Thursdays between 1:30 and 5:30pm.

Oh, good.  So I can use them _right now_.

noren@dinl.uucp (Charles Noren) (03/16/90)

In article <2057@skye.ed.ac.uk> jeff@aiai.UUCP (Jeff Dalton) writes:
>In article <53508@bbn.COM> mesard@labs-n.bbn.com (Wayne Mesard) writes:
 > >jeff@aiai.UUCP (Jeff Dalton) writes:
 >>>why not use them on lists?  When would you actually allow me to use
 >>>CAR and CDR?
 >>
 >>Thursdays between 1:30 and 5:30pm.
 
 >Oh, good.  So I can use them _right now_.
 
Ahh, but are you in the correct time zone?
-- 
Chuck Noren
NET:     ncar!dinl!noren
US-MAIL: Martin Marietta I&CS, MS XL8058, P.O. Box 1260,
         Denver, CO 80201-1260
Phone:   (303) 971-7930

D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE (Johnny Billquist) (03/17/90)

In artricle <2035@skye.ed.ac.uk> jeff@aiai.ed.ac.uk (Jeff Dalton) writes:

>One certainly shouldn't use CAR and CDR for some abstractions, but
>why not use them on lists?

Because lists are an abstraction.

>  When would you actually allow me to use
>CAR and CDR?

When you work on conses of course.

======================================================================
Everybody know that the DECstation is a pdp8, which is a RISC,
but where did MIPS computers get into it?

	- Johnny Billquist
======================================================================

jeff@aiai.ed.ac.uk (Jeff Dalton) (03/20/90)

In article <12574182161036@AIDA.CSD.UU.SE> D89.JOHNNY-BILLQUIST@AIDA.CSD.UU.SE (Johnny Billquist) writes:
>In artricle <2035@skye.ed.ac.uk> jeff@aiai.ed.ac.uk (Jeff Dalton) writes:
>
>>One certainly shouldn't use CAR and CDR for some abstractions, but
>>why not use them on lists?
>
>Because lists are an abstraction.

I agree for "some abstractions", so you need an argument about "all
abstractions" or else one about why lists are among the ones that must
be treated abstractly in this way.  I'd already taken the simple
"lists are an abstraction" into account, and as far as I could tell
the benefits of using FIRST and REST in Lisp, and restricting CAR and
CDR to pairs, are chiefly (but not entirely) ideological rather than
practical.

Note that "don't use CAR and CDR on lists" is not the same as saying
"the data structure LIST should not be confused with its representation
as PAIRS".  I agree with the latter but not the former.  I have no
objection to someone using FIRST and REST if they want to, but I
regard it as chiefly a matter of style.

In an earlier message you wrote:

   The correct argument to CAR and CDR is *not* lists, but s-expr
   (punctuated pairs to be specific). The fact that most (if not all)
   LisPs implement lists by punctuated pairs is a feature you should
   not rely upon. That is why the functions FIRST and REST exists.
   Sure, they do just CAR and CDR, but what if you stumles upon a LisP
   which implement lists in another way?

Well, what if you stumble on a Lisp that has CAR and CDR for *lists*
and PAIR-LEFT and PAIR-RIGHT (or something) for pairs?  

In any case, most of the time programmers are not writing Lisp but
some particular kind of Lisp such as Common Lisp or Scheme.  That
something that still counts as Lisp might implement lists in some
other way is not much of a problem in practice.  However, using
CAR and CDR for other data structures built on top of lists or
pairs is another matter.  Nothing I've said should be taken as
an argument against treating those structures abstractly.  Nor
do I think it would necessarily be a bad idea to define a Lisp
in which lists and pairs *were* distinct.

-- Jeff