Discussion:
[Chicken-users] case - should that even work?
Martin Schneeweis
2018-07-15 18:36:41 UTC
Permalink
Hi,

please can someone explain to me why the following even works?

(define sym 'b)

(case sym
('a "an 'a'")
('b "a 'b'")
('c "a 'c'")
(else "something else")) ; -> a 'b'

The correct case-form for the above example should be:

(case sym
((a) "an 'a'")
((b) "a 'b'")
((c) "a 'c'")
(else "something else"))

I know the r5rs document just says "Each <clause> _should_ have the
form ((<datum1> ...) <expression1> <expression2> ...)

Surprisingly my "quote-variant" (which is obviously not in the _should_
form) also works in Racket and Chez (and even mit-scheme - although
they changed the documentation for "case" somewhat
(https://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Conditionals.html)
- "Each clause has this form...").

lg
Martin
Thomas Chust
2018-07-15 18:50:39 UTC
Permalink
Post by Martin Schneeweis
[...]
please can someone explain to me why the following even works?
(define sym 'b)
(case sym
('a "an 'a'")
('b "a 'b'")
('c "a 'c'")
(else "something else")) ; -> a 'b'
[...]
I know the r5rs document just says "Each <clause> _should_ have the
form ((<datum1> ...) <expression1> <expression2> ...)
[...]
Hello,

contrary to what you assume, these clauses *are* actually in the form specified by the standard :-D

The apostrophe is a *reader* macro, so before the compiler or interpreter ever gets to see that code, 'a is transformed to (quote a), 'b to (quote b) and 'c to (quote c). These three forms are obviously lists with two elements and represent case descriptors matching the symbols quote or a, quote or b and quote or c respectively.

Try setting sym to 'quote and see what happens!

Ciao,
Thomas
--
The greatest victory is that which requires no battle.
-- Sun Tzu, "The Art of War"
Martin Schneeweis
2018-07-15 21:24:30 UTC
Permalink
Hi Thomas,
Post by Thomas Chust
Post by Martin Schneeweis
[...]
please can someone explain to me why the following even works?
(define sym 'b)
(case sym
('a "an 'a'")
[...]
The apostrophe is a *reader* macro, so before the compiler or
interpreter ever gets to see that code, 'a is transformed to (quote
a), 'b to (quote b) and 'c to (quote c). These three forms are
obviously lists
[...]
Thank you very much for the clarification. I guess it takes a
while to read and write scheme code fluently...

lg
Martin

Loading...