Re: (case 'quote ('lambda 1) (otherwise 2))
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Mon, 29 Jan 2007 23:57:18 +0100
"David Wragg" <david@xxxxxxxxx> writes:
I have found 2 Common Lisp implementations that think that the above
code evaluates to 1. This is quite surprising to me.
SBCL 1.0.2 (on Fedora Core 6 x86-64, from the extras repository)
* (case 'quote ('lambda 1) (otherwise 2))
[...]
1
CLisp 2.41 (as above):
[1]> (case 'quote ('lambda 1) (otherwise 2))
1
Both implementations give less surprising results for (eq 'quote
'lambda) and (case 'lambda ('quote 1) (otherwise 2)).
Is this the correct behavior (and if so, where should I look in the
Hyperspec to understand it)? Or do these independent implementations
share an obscure bug?
Yes, you will find that all conforming implementations will return 1 here.
You have to remember that ' is a reader macro which expands to a list:
(read-from-string "'x") --> (QUOTE X)
[and #' is a dispatching reader macro that expands similarly to a
list, but with FUNCTION instead of QUOTE:
(read-from-string "#'x") --> (FUNCTION X)
]
Now, when you try these read-from-string forms, you must be careful
not to be misled by your implementation printer, which usually process
specially lists such as (QUOTE X) and (FUNCTION X) and print them as
'X and #'X instead of printing them as normal lists. To be sure, you
can use your own list printing function:
(defun print-list (list &optional (stream *standard-output*))
(princ "(")
(when list
(loop
:for head = list :then (cdr head)
:while (cdr head)
:do (prin1 (car head)) (princ " ")
:finally (prin1 (car head))))
(princ ")")
list)
C/USER[46]> (print-list '(quote x))
(QUOTE X)
'X
C/USER[47]> (print-list '(function x))
(FUNCTION X)
#'X
So your form: (case 'quote ('lambda 1) (otherwise 2))
is read as: (case (quote quote) ((quote lambda) 1) (otherwise 2))
Now, the point is that CASE doesn't evaluates the keys listed in
normal-clause. The keys you gave in your first clause is (QUOTE
LAMBDA), which is a list of two keys, QUOTE and LAMBDA. It happens
that the evaluation of (QUOTE QUOTE) returns QUOTE, which is exactly
the first key in the the list of keys of the first clause.
My advice with CASE is to always write the keys in a list, even if you
have only one key:
(case (quote quote)
((lambda) 1)
(otherwise 2))
--> 2
--
__Pascal Bourguignon__ http://www.informatimago.com/
Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
.
- Follow-Ups:
- Re: (case 'quote ('lambda 1) (otherwise 2))
- From: tayssir . john
- Re: (case 'quote ('lambda 1) (otherwise 2))
- References:
- (case 'quote ('lambda 1) (otherwise 2))
- From: David Wragg
- (case 'quote ('lambda 1) (otherwise 2))
- Prev by Date: Re: All Your GUIs Are Belong to Us (Die, McCLIM! Die!!)
- Next by Date: Re: Integer to Character conversion
- Previous by thread: Re: (case 'quote ('lambda 1) (otherwise 2))
- Next by thread: Re: (case 'quote ('lambda 1) (otherwise 2))
- Index(es):