<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Sep 5, 2012, at 4:15 PM, Rüdiger Asche wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: 'Lucida Grande'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="font-family: Arial; font-size: small; "><span class="Apple-converted-space"> </span>the pattern matching and substitution process is expectedly rather sophisticated (and thus more resource consuming)?</span></span></blockquote></div><br><div>Nope, it all happens at compile time. The object code is probably close to this </div><div><br></div><div> (if (null? ls) </div><div> (void)</div><div> (let ([fst (car ls)]))</div><div> (if (null? (cdr ls))</div><div> (void) </div><div> (let ([snd (cadr ls)])</div><div> (if (null? (cddr ls))</div><div> (void) </div><div> (let ([trd (caddr ls)])</div><div> (if (null? (cdddr ls)) ;; <--- here </div><div> (list trd snd fst) </div><div> (void))))))))</div><div><br></div><div>Now you would think that </div><div><br></div><div> (list (caddr ls) (cadr ls) (car ls))</div><div><br></div><div>is much faster because it avoids all the null? checks. BUT, you need to keep in mind that Racket's car must perform the null? checks anyway because it cannot just assume that ls consists of three items. </div><div><br></div><div>Conjecture: the jit compiler actually knows that the two checks happen and eliminates one of them. I do not know for sure that this is true but I know that some compilers have done this in the past. </div><div><br></div><div>Meaning? For this simple pattern matching problem, the code produced via (the) match (macro) and the plain function definition _performs_ at about the same level, possibly with one extra null? check -- labeled "<-- here" because this one is not needed for the naive version, which does not check whether there is a fourth item on the list. The match version enforces this constraint however. </div><div><br></div><div>;; --- </div><div><br></div><div>At this point you may realize that the two functions aren't trivially equivalent. It is not true that </div><div><br></div><div> for all x in Racket values, </div><div> (equal? (switch-one-and-three1 x) (switch-one-and-three4 x))</div><div><br></div><div>What you would have to prove is </div><div> </div><div> for all x in Racket values, </div><div> (implies (and (list? x) (= (length x) 3)) </div><div> (equal? (switch-one-and-three1 x) (switch-one-and-three4 x))</div><div><br></div><div>Exercise: Try to prove the above in Dracula. -- Matthias</div><div><br></div><div><br></div><div><span class="Apple-style-span" style="font-family: Arial; font-size: small; "><br></span></div><div><font class="Apple-style-span" face="Arial" size="2"><br></font></div><div><font class="Apple-style-span" face="Arial" size="2"><br></font></div><div><br></div><span class="Apple-style-span" style="font-family: Arial; font-size: small; "> </span></body></html>