[racket] Problem with case

From: Jon Zeppieri (zeppieri at gmail.com)
Date: Tue Sep 18 21:29:04 EDT 2012

On Tue, Sep 18, 2012 at 5:29 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
> >
> > I'd like to see a discussion of the tradeoffs between cond & case & match
> > and how each is best used.

1. Use 'cond' where you would use 'if...else if...else' in a C-like
language. It's just a multi-way if:

(cond [(string? value)
           (write-string value out)]
          [(bytes? value)
           (write-bytes value out)]
          [(input-port? value)
           (copy-port value out)]
           (error "bad value")])

2. Use 'case' (roughly) where you would use 'switch' in C, namely,
when you're comparing the value of an expression against a set of

(case escape-char
  [(#\n) #\newline]
  [(#\r) #\return]
  [(#\t) #\tab]
  [(#\a) #\u0007]
  [(#\v) #\u000b]
  [(#\f) #\u000c]
  [(#\e) #\u001b]
  [else escape-char])

3. Use 'match' (roughly) where you would use 'case' in ML. I agree
with Danny: it's particularly nice for struct matching:

(: add (All (A) ((Braun A) A -> (Braun A))))
(define (add t a)
  (match t
     (Braun-Tree a t t)]
    [(Braun-Tree x l r)
     (Braun-Tree a (add r x) l)]))

> I think 'case' is supposed to have good performance compared to the
> other two, due to work by Jon Zeppieri
> (http://lists.racket-lang.org/dev/archive//2012-July/010036.html).

The new case implementation isn't in 5.3 or the stable branch on
github. (I assume the nightly builds on racket-lang.org are from the
stable branch?) It's in the master branch.

Posted on the users mailing list.