Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(select case ...) does not evaluate test clauses #10

Open
jarcane opened this issue Dec 22, 2014 · 1 comment
Open

(select case ...) does not evaluate test clauses #10

jarcane opened this issue Dec 22, 2014 · 1 comment

Comments

@jarcane
Copy link
Owner

jarcane commented Dec 22, 2014

This is a limitation, apparently, of the Racket version. Racket's (case ...) quotes its matching clauses, so they are not actually evaluated, just compared to the value expression with equal?

So this works:

(select case (mod 5 2)
  ((1) 'foo)
  ((0) 'bar))

; returns 'foo

But this doesn't:

(select case 1
  ((mod 5 2) 'foo)
  ((mod 2 2) 'bar))

; expect 'foo, but falls through instead.void

I think the case can be made for writing our own (select case ...) to allow for this usage. It's a common enough behavior in other language's case statements, so if it's possible to do with Racket's macros I think it's worth implementing.

@jarcane
Copy link
Owner Author

jarcane commented Dec 23, 2014

Racket apparently does provide an alternate case in the old mzscheme libraries that works more like our previous second example. With evcase you can do this for example:

(require mzlib/etc)

(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))))

I've mocked up a simple case macro that supports the best of both features, but the discussion in the mailing list seems to indicate there may be performance costs to allowing both a list of matching values and matching expressions in the same block. I don't consider performance as high on the list of priorities as ease of use and syntax power though, so this may not necessarily be worth worrying about yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant