I see that I was misled by raco demod, which suggests that this: (define (f x) (match-define (cons a b) x) (+ a b))
compiles into this: (define-values (_f) (lambda (arg0-21) '#(f #<path:/Users/robby/tmp.rkt> 2 0 14 54 #f) '(flags: preserves-marks single-result) '(captures: (val/ref #%modvars) (|_match:error:P@(lib "racket/match/runtime.rkt")| |_syntax-srclocs:P@(lib "racket/match/runtime.rkt")| #%syntax)) (let ((localv22 ?) (localv23 ?)) (begin (set!-values (localv22 localv23) (if (pair? arg0-21) (values (unsafe-car arg0-21) (unsafe-cdr arg0-21)) (|_match:error:P@(lib "racket/match/runtime.rkt")| (#%sfs-clear arg0-21) (|_syntax-srclocs:P@(lib "racket/match/runtime.rkt")| stx19) 'match-define))) (+ localv22 localv23))))) and I figured that match introduced the set! (without checking). It make sense to me that it wouldn't be matches job to eliminate those multiple values. Probably not worth doing more here. Robby On Tue, Aug 8, 2017 at 6:34 PM, Sam Tobin-Hochstadt <sa...@cs.indiana.edu> wrote: > On Thu, Jul 27, 2017 at 8:36 PM, Robby Findler > <ro...@eecs.northwestern.edu> wrote: >> I see from raco demod that match-define does not generate very >> beautiful code, which leads me to this version (which is what I think >> match-define should have expanded into). It seems to be about 4x >> faster on my computer. > > Unfortunately, I don't think `match-define` can avoid the use of > multiple values here. What you're seeing is the difference between: > > (define-values (a b) (if (pair? x) (values (car x) (cdr x)) (error 'fail))) > > and > > (unless (pair? x) (error 'fail)) > (define a (car x)) > (define b (cdr x)) > > `match-define` generates the first snippet, but the second performs > significantly faster. I would like the optimizer to transform the > first into the second, however, since `match-define` has to support > this program: > > (match-define (list (cons a b)) ...) > > which would expand to: > > (define-values (a b) (if (and (pair? x) (null? (cdr x)) (let ([car-x > (car x)]) (if (pair? car-x) (values (car car-x) (cdr car-x)) (error > 'fail)) (error 'fail2))))) > > which is much harder (although of course not impossible) to transform > into a series of definitions. > > `match-define` also has the additional complication that the > identifiers being defined should be in scope in the expression > defining them, which also makes doing this transformation in > `match-define` harder. > > Sam -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.