[plt-scheme] Why isn't the car of a list of symbols a symbol?

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Thu Jul 9 23:07:19 EDT 2009

On Jul 9, 2009, at 7:53 PM, Marco Morazan wrote:

>>> ((quote yes) (quote no))
>>>
>>> is not a value. It is an application expression.
>>
>> It most certainly is a value.  It is a list of two elements.
>
> Huh? Really? How do you figure it evaluates to a list with two
> elements?

Note the change in wording, from "It is a list of two elements" to  
"it evaluates to a list with two elements."  Very different things.

((quote yes) (quote no))

is a list with two elements, each of which is a list with two elements.

(list (quote yes) (quote no))

is a list with three elements, which (in a certain language, with a  
certain environment) evaluates to the aforementioned list with two  
elements, because "eval" interprets it as an application expression.

> I suggest a little testing:
>
> Welcome to DrScheme, version 4.2 [3m].
> Language: Essentials of Programming Languages (3rd ed.); memory limit:
> 128 megabytes.
>> (list? (quote ((quote yes) (quote no)) ))
> #t
>> (list? '((quote yes) (quote no)) )
> #t
>> (list ((quote yes) (quote no)))
> . . procedure application: expected procedure, given: yes;  
> arguments were: no
>>
>
> That last one means that you are trying to apply 'yes to 'no. Clearly,
> it is an application expression.


No, I'm not trying to apply 'yes to 'no; "eval" is trying to apply  
'yes to 'no, and the only reason "eval" is involved at all is because  
you typed these expressions into a read-EVAL-print loop.

To find out what the expression you typed IS, as opposed to what it  
EVALUATES TO, imagine a DrScheme language in which the "eval" step is  
skipped: it's just an RPL.  (I'm sure one of the PLT gurus on the  
list could whip this up in a few minutes....)  You type in an  
expression and you get back the same expression -- possibly in a  
different display format, but with no function applications in  
between.  Of course, you could set this up with various different  
display formats, e.g. the nested-cons display format of BSL; the  
"list" display format of BSLLA; a quoted-list display format; or an  
unquoted display format (which I think is the first output format I  
saw in Lisp, thirty years ago).  In any case, the result "is" the  
same thing as the expression you typed in.

In BSL output format,
 > (quote ((quote yes) (quote no)))
(cons 'quote (cons (cons 'quote (cons 'yes empty)) (cons (cons 'quote  
(cons 'no empty)) empty)) empty))
 > '((quote yes) (quote no))
(cons 'quote (cons (cons 'quote (cons 'yes empty)) (cons (cons 'quote  
(cons 'no empty)) empty)) empty))
 > ((quote yes) (quote no))
(cons (cons 'quote (cons 'yes empty)) (cons (cons 'quote (cons 'no  
empty)) empty)

In BSLLA output format,
 > (quote ((quote yes) (quote no)))
(list 'quote (list (list 'quote 'yes) (list 'quote 'no)))
 > '((quote yes) (quote no))
(list 'quote (list (list 'quote 'yes) (list 'quote 'no)))
 > ((quote yes) (quote no))
(list (list 'quote 'yes) (list 'quote 'no))

In quoted-list display format,
 > (quote ((quote yes) (quote no)))
'('yes 'no)
 > '((quote yes) (quote no))
'('yes 'no)
 > ((quote yes) (quote no))
'('yes 'no)

In unquoted display format,
 > (quote ((quote yes) (quote no)))
(quote ((quote yes) (quote no)))
 > '((quote yes) (quote no))
(quote ((quote yes) (quote no)))
 >((quote yes) (quote no))
((quote yes) (quote no))


Every time you see a left parenthesis, there's the beginning of a  
list in internal representation.  Every right parenthesis becomes the  
end of a list in internal representation.

Of course, I'm sure this is a gross oversimplification of the actual  
implementation, which has syntax objects that are kinda-like-but-not- 
quite lists.  But to a first approximation, it works.

Stephen Bloch
sbloch at adelphi.edu





Posted on the users mailing list.