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

From: Greg Hendershott (greghendershott at gmail.com)
Date: Sat May 4 22:09:29 EDT 2013

There will be people who see the long BNF for match in the Reference,
and flee to the Guide to try to learn by example instead. Even when
they later do read the Reference carefully, they'll be left with first
impressions of idioms from the Guide.

(I might be one of those people. Cough.)

The Guide <http://docs.racket-lang.org/guide/match.html> uses `else`
in a couple examples.

Also, I somehow got the idea that `(var x)` was the only way to do
what `else` can do by accident. So I don't _think_ I have any code
that's buggy this way, or, which would break if the change were made,
either way. I think.

Until I saw your email I didn't appreciate why it would make sense to
use `_`. Going forward I will.


On Fri, May 3, 2013 at 9:39 AM, Robby Findler
<robby at eecs.northwestern.edu> wrote:
> [ for those that just want to see what I'm getting at, scroll to the end ]
>
> While the docs are clear (enough) on this point, I think it can be quite
> confusing. See if you spot the bug in this program:
>
> #lang racket
> (define (find-literals e)
>   (define literals '())
>   (let loop ([e e])
>     (match e
>       [`(λ (,x) ,e)
>        (loop e)]
>       [`(,e1 ,e2)
>        (loop e1)
>        (loop e2)]
>       [(? symbol?) (void)]
>       [else
>        (cond
>          [(member e literals)
>           (void)]
>          [else
>           (set! literals (cons e literals))])]))
>   literals)
>
> (module+ test
>   (require rackunit)
>   (check-equal? (find-literals '(λ (x) x)) '())
>   (check-equal? (find-literals '((λ (x) x) 1)) '(1))
>   (check-equal? (find-literals '((λ (x) x) #f)) '(#f)))
>
>
> Hint: the last test case fails. Second hint: if you switch it to use sets,
> it starts working. Third hint: this isn't about lists. :)
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> The bug is that 'else' is treated as a variable in match, so it gets bound
> to #f, which shadows the else in the cond which .... is confusing.
>
> So, how about making match treat else specially?
>
>
> I don't ask this completely from outer space; there was a bug in Redex's
> random generation that was caused exactly by this shadowing of else.
>
> Robby
>
>
>
>
> _________________________
>   Racket Developers list:
>   http://lists.racket-lang.org/dev
>


Posted on the dev mailing list.