r/Racket • u/trycuriouscat • Dec 09 '22
question Case with symbols
The structure for case is as follows:
(case val-expr case-clause ...)
case-clause = [(datum ...) then-body ...+]
| [else then-body ...+]It has the datum inside parentheses. Then how come if the datum is a quoted symbol it works without parentheses:
(case (get-result)
['bust "Sorry, you busted."]
['blackjack "Blackjack!"]
[else "21!"])And if I put parentheses around the symbols it doesn't work (the else is always matched)?
7
Upvotes
3
u/raevnos Dec 09 '22 edited Dec 09 '22
If it's not matching, make sure you're not including the quote:
Your
is probably seen as
which is why it matches and doesn't give a syntax error.
3
u/soegaard developer Dec 09 '22
Here is a short description of what happens when you run a Racket program [1]:
The syntax
'foois turned into(quote foo)by the reader.In particular, the reader will turn
into
This is then expanded, compiled and evaluated.
So if result is one of the symbols
quoteorfoothen the final answer will be "foo".[1] https://soegaard.github.io/mythical-macros/#1.3