Cannot use case+else inside match+else

47 views
Skip to first unread message

Shu-Hung You

unread,
Feb 25, 2019, 7:33:16 PM2/25/19
to Racket Users
When using a case expression with an else clause in inside a match
expression, the expander complains about case having a bad syntax.
However, the use of case expressions outside of match are fine. Is
there anyway to get around this?

Currently, I just replace it with another match.

#lang racket/base

(require racket/match)

(match 6
[else
(case 5
[else 7])])

#|
unsaved editor:8:6: case: bad syntax (not a datum sequence) at: else
in: (case 5 (else 7))
#(78 4)
|#

John Clements

unread,
Feb 25, 2019, 7:34:46 PM2/25/19
to Shu-Hung You, Racket Users
I may be missing something obvious, here, but why are you using the name “else” to bind the result in the match clause? I usually use the name “other”, which might solve your problem.

John
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



Sam Tobin-Hochstadt

unread,
Feb 25, 2019, 7:35:24 PM2/25/19
to Shu-Hung You, Racket Users
This is because 'case' expects a particular binding for 'else', but you shadowed it with 'match', which does not treat 'else' differently than any other identifier. 

Sam

Shu-Hung You

unread,
Feb 25, 2019, 7:41:13 PM2/25/19
to John Clements, Sam Tobin-Hochstadt, Racket Users
Ah!

Thank you for pointing this out. I've always thought that `else' is
treated as a keyword in `match' just as what `cond' does. That
explains everything.

Sorawee Porncharoenwase

unread,
Feb 25, 2019, 7:51:24 PM2/25/19
to Shu-Hung You, John Clements, Sam Tobin-Hochstadt, Racket Users

I usually use _ for the otherwise case because I usually match on an identifier, so I have an id to refer to the matched value already if I need to. The only case I don’t use _ is when I compute something inside the match and also need to use its result:

(match (foo bar)
  [... ...]
  [result-of-foo-bar ...])

Robby Findler

unread,
Feb 25, 2019, 8:46:40 PM2/25/19
to Sorawee Porncharoenwase, Shu-Hung You, John Clements, Sam Tobin-Hochstadt, Racket Users
I have been bitten by this one too, it is a bit pernicious. Perhaps we
can adjust the documentation to make more clear that `else` isn't
special in `match`?

Robby

Greg Hendershott

unread,
Feb 25, 2019, 10:59:51 PM2/25/19
to Robby Findler, Sorawee Porncharoenwase, Shu-Hung You, John Clements, Sam Tobin-Hochstadt, Racket Users
Yep, I also spent a non-zero number of years not even realizing "else"
wasn't a magic literal for match. And then remembering to use _
instead, as Sorawee suggested.


I think this shows why it's usually better for syntax to use
#:keywords instead of literals? If cond and case used #:else, this
wouldn't be a problem.

(Racket inherited those from Scheme. But we don't have to follow that
example in our own, new macros.)

Jon Zeppieri

unread,
Feb 25, 2019, 11:10:04 PM2/25/19
to Greg Hendershott, Robby Findler, Sorawee Porncharoenwase, Shu-Hung You, John Clements, Sam Tobin-Hochstadt, Racket Users
Previously on this program... 

[racket-dev] else clauses: possible change to match?



Ben Greenman

unread,
Feb 26, 2019, 12:03:47 AM2/26/19
to Jon Zeppieri, Greg Hendershott, Robby Findler, Sorawee Porncharoenwase, Shu-Hung You, John Clements, Sam Tobin-Hochstadt, Racket Users
Here's a suggestion for the docs:

https://github.com/racket/racket/pull/2505

Laurent

unread,
Feb 27, 2019, 8:20:01 AM2/27/19
to Ben Greenman, Jon Zeppieri, Greg Hendershott, Robby Findler, Sorawee Porncharoenwase, Shu-Hung You, John Clements, Sam Tobin-Hochstadt, Racket Users
Wait, that means that in an interactive session, if you ever happen to redefine `else', you can't use `case' anymore?

On Tue, Feb 26, 2019 at 5:03 AM Ben Greenman <benjamin...@gmail.com> wrote:
Here's a suggestion for the docs:

https://github.com/racket/racket/pull/2505

Jens Axel Søgaard

unread,
Feb 27, 2019, 8:28:25 AM2/27/19
to Laurent, Ben Greenman, Greg Hendershott, John Clements, Jon Zeppieri, Racket Users, Robby Findler, Sam Tobin-Hochstadt, Shu-Hung You, Sorawee Porncharoenwase
I suppose you could (re)require it again. 
--
--
Jens Axel Søgaard

Laurent

unread,
Feb 27, 2019, 8:32:10 AM2/27/19
to Jens Axel Søgaard, Ben Greenman, Greg Hendershott, John Clements, Jon Zeppieri, Racket Users, Robby Findler, Sam Tobin-Hochstadt, Shu-Hung You, Sorawee Porncharoenwase
Good point. I wasn't sure that would work---it does.

Shu-Hung You

unread,
Feb 27, 2019, 10:03:22 AM2/27/19
to Laurent, Jens Axel Søgaard, Ben Greenman, Greg Hendershott, John Clements, Jon Zeppieri, Racket Users, Robby Findler, Sam Tobin-Hochstadt, Shu-Hung You, Sorawee Porncharoenwase
And because it is nothing else but a usual binding, it's possible to
prefix-in or rename-in as in

(require (prefix-in r: racket/base))
(case 5 [r:else 'ok])
Reply all
Reply to author
Forward
0 new messages