Check out this small snippet from the REPL:
> (define (foo p) (case p (('bar) 1) (else 2)))
> (foo 'bar)
2
I expected to the call (foo 'bar) would return 1. Why is 'bar not matched?
Check out this small snippet from the REPL:
> (define (foo p) (case p (('bar) 1) (else 2)))
> (foo 'bar)
2
I expected to the call (foo 'bar) would return 1. Why is 'bar not matched?
Well, the short version here is: don't use case, it's a very old form and has surprising conventions. Specifically, in this case, the elements in the matching clauses are automatically quoted, so you probably wanted to write:
> (define (foo p) (case p ((bar) 1) (else 2)))
> (foo 'bar)
1
What would you have me use instead of case? The closest thing would probably be cond; however I prefer case in cases when I have values to match, because the cond clauses are more verbose in this case.
Usually it is better to use match than case:
> (define (foo p)
(match p
['bar 1]
[_ 2]))
> (foo 'bar)
1
(One very niche reason when you actually might want case instead of match is when you care about the guarantee that “the case form can dispatch to a matching case-clause in O(log N) time for N datums.”)