<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">&nbsp;</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&nbsp;</div><div><br></div><div>&nbsp;(if (null? ls)&nbsp;</div><div>&nbsp; &nbsp; &nbsp;(void)</div><div>&nbsp; &nbsp; &nbsp;(let ([fst (car ls)]))</div><div>&nbsp; &nbsp; &nbsp; &nbsp;(if (null? (cdr ls))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(void)&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(let ([snd (cadr ls)])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(if (null? (cddr ls))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(void)&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(let ([trd (caddr ls)])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (if (null? (cdddr ls)) ;; &lt;--- here&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (list trd snd fst)&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (void))))))))</div><div><br></div><div>Now you would think that&nbsp;</div><div><br></div><div>&nbsp;(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.&nbsp;</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.&nbsp;</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 "&lt;-- 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.&nbsp;</div><div><br></div><div>;; ---&nbsp;</div><div><br></div><div>At this point you may realize that the two functions aren't trivially equivalent. It is not true that&nbsp;</div><div><br></div><div>&nbsp;for all x in Racket values,&nbsp;</div><div>&nbsp; &nbsp;(equal? (switch-one-and-three1 x) (switch-one-and-three4 x))</div><div><br></div><div>What you would have to prove is&nbsp;</div><div>&nbsp;</div><div>&nbsp; for all x in Racket values,&nbsp;</div><div>&nbsp; &nbsp;(implies (and (list? x) (= (length x) 3))&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (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; ">&nbsp;</span></body></html>