I've been feeling pretty good about my macro-fu recently so I gave it a shot (I havent looked at anyone's solutions yet). This seems to pass all the test cases but I dont think it passes the "linear" requirement yet.
(define-syntax (cas-cad-e stx) (syntax-case stx () [(_ e ((v) exp ...)) (with-syntax ([break (datum->syntax stx 'break)]) #'(call/cc (λ (break) (if (equal? e v) (begin exp ...) (void)))))] [(_ e ((v1) exp1 ...) ((v2) exp2 ...) ...) (with-syntax ([break (datum->syntax stx 'break)]) #'(call/cc (λ (break) (if (equal? e v1) (begin exp1 ... exp2 ... ...) (cas-cad-e e ((v2) exp2 ...) ...)))))])) On Fri, Oct 8, 2010 at 9:04 PM, Shriram Krishnamurthi <s...@cs.brown.edu> wrote: > One of my students recently sent me this needless email message: > >> Well, how would you do switch fall-through in Scheme? Could you >> write a version of the case statement that does that? > > Since the honor of Racket was at stake (yes, we can have all the same > stupid features the scripting languages have!), I wrote down the code > for this, and realized it would make a cute little macro exercise. > > Spec: define a case construct syntactically just like that of Racket. > In terms of semantics: > > - each branch automatically falls through to the next, > > - the last one returns its answer since it has no next clause, and > > - any branch can contain (break <expr>), which evaluates <expr> and > returns its value as that of the entire case. > > In honor of its behavor, we'll call this cas-cad-e. Thus, > > (define (cas1 v) > (cas-cad-e v > ((1) (display "1")) > ((2) (display "2") (break 2) > ((3) 3)))) > > (cas1 1) ==> 2 (and prints "12") > (cas1 2) ==> 2 (and prints "2") > (cas1 3) ==> 3 (and prints nothing) > (cas1 4) ==> <void> (and prints nothing) > > If anyone wants to look at my solution, here it is: > > http://github.com/shriram/cas-cad-e > > Of course, those who know how to do this don't need to peek, and those > who don't shouldn't. > > Shriram > _________________________________________________ > For list-related administrative tasks: > http://lists.racket-lang.org/listinfo/users >
_________________________________________________ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users