[racket] Why does (case ...) quote its matching value expressions?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Dec 22 23:19:57 EST 2014

Keep in mind that case also isn't really cheap. There's an infinite number of symbols so there is no cheap jump table. -- Matthias



On Dec 22, 2014, at 10:10 PM, J Arcane wrote:

> Thanks for the insight. I hadn't considered the performance implications involved I suppose (the perpetual problem of the young Lisper ...)
> 
> And evcase does indeed work more like I expected, for example: 
> 
> (for ([n (in-range 1 101)])
>   (evcase 0
>           ((+ (modulo n 5)
>               (modulo n 3)) (displayln "FizzBuzz"))
>           ((modulo n 5) (displayln "Buzz"))
>           ((modulo n 3) (displayln "Fizz"))
>           (else (displayln n))))
> 
> Works exactly as I'd expect. I'll have a look at the code and see if any further insights might be found there for Heresy's (select case ...)
> 
> Thanks!
> 
> On Mon, Dec 22, 2014 at 5:20 PM, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
> 
> Look for evcase. -- Matthias
> 
> 
> On Dec 21, 2014, at 11:58 PM, J Arcane wrote:
> 
> > Up with a horrible ear-ache this morning I decided to include a FizzBuzz example in Heresy, the Racket #lang I've been working on, and ran into an unexpected behavior in the (case ...) statement.
> >
> > In many languages with case, you can make the testing value a constant, and then make the matching clauses actual calculations which then match against that constant. So when doing FizzBuzz in C-like languages you can do something like "switch 0" and then "case x % 3" for the matching clauses.
> >
> > It turns out this doesn't work in Racket, because Racket quotes the values in the matching clauses so they do not evaluate. Specifically, it narrows down to doing this in (case/sequential-test ...): #`(equal? v 'k)
> >
> > I can implement an alternate version that works as I expect (and will probably include it in Heresy) just by removing that quote in my version, but I was curious as to the reasoning behind this behavior and if perhaps there's some explanation for it that I may've missed.
> >
> > Any insights appreciated,
> > John Berry
> >
> > ____________________
> >  Racket Users list:
> >  http://lists.racket-lang.org/users
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141222/8be50da7/attachment-0001.html>

Posted on the users mailing list.