[comp.lang.scheme] 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