[racket] Problem with case
On 9/17/12 6:47 PM, Kieron Hardy wrote:
> Hi all,
>
> Can anyone explain why the first function below selects 'x1-case as
> expected but the second fails to select 'x2-case? Am I expecting
> something to happen that shouldn't?
>
> Thanks,
>
> Kieron.
>
> ****
>
> #lang racket
>
> (case 'a
> ['a 'x1-case]
> ['b 'x1-case]
> [(15 2 3) 'y1-case]
> [(10 11 12) 'z1-case])
>
> (case 'a
> [('a 'b) 'x2-case]
> [(15 2 3) 'y2-case]
> [(10 11 12) 'z2-case])
Be careful with quote in a case clause -- those clauses are already
implicitly quoted. The first is really:
(case 'a
[(quote a) 'x1-case]
[(quote b) 'x1-case]
[(15 2 3) 'y1-case]
[(10 11 12) 'z1-case])
So you'll notice the following surprising behavior:
(case 'quote
['a 'x1-case]
['b 'x1-case]
[(15 2 3) 'y1-case]
[(10 11 12) 'z1-case])
You want:
(case 'quote
[(a) 'x1-case]
[(b) 'x1-case]
[(15 2 3) 'y1-case]
[(10 11 12) 'z1-case])
and:
(case 'a
[(a b) 'x1-case]
[(15 2 3) 'y1-case]
[(10 11 12) 'z1-case])
David