<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
I've been away from scheme for awhile, so I decided to go back to PLAI and start reading from the beginning<div><br></div><div> I implemented the predicate AE? (for arithmetic expressions) like this</div><div><br></div><div><div>  ;;pattern based evaluator (language: AE)</div><div>  </div><div>  (define (AE? exp)</div><div>    (match exp</div><div>      ((? number? exp) #t)</div><div>      ((list '+ (? AE?) (? AE?)) #t)</div><div>      ((list '- (? AE?)) #t)</div><div>      ((list '- (? AE?) (? AE?)) #t)</div><div>      ((list '* (? AE?) (? AE?)) #t)</div><div>      ((list '/ (? AE?) (? AE?)) #t)</div><div>      (else #f)))</div><div><br></div><div>So far, so good, but if I follow the model in the book (minus the use of type-case, which doesn't seem to be part of PLT Scheme) I end up with something like this</div><br><div> <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: 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: 0; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; 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; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; 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; "><div><div>(define (evaluate exp)</div><div>  (with-handlers</div><div>      ((exn:fail:user?</div><div>        (lambda (exn) (printf "~a~n" exn))))</div><div>  (match exp</div><div>    ((? number? exp) exp)</div><div>    ((list '+ (? AE? t1) (? AE? t2)) (+ (evaluate t1) (evaluate t2)))</div><div>    ((list '- (? AE? t1) (? AE? t2)) (- (evaluate t1) (evaluate t2)))</div><div>    ((list '* (? AE? t1) (? AE? t2)) (* (evaluate t1) (evaluate t2)))</div><div>    ((list '/ (? AE? t1) (? AE? t2))</div><div>     (let</div><div>         ([ n (evaluate t1)]</div><div>          [d (evaluate t2)])</div><div>       (if (equal? d 0)</div><div>           (raise-user-error "division by zero")</div><div>           (/ n d))))</div><div>    (else (raise-user-error "Unable to evaluate expression" exp)))))</div><div>  </div><div><br></div><div>Now, on the face of it, this seems awfully wasteful because I'm repeating all the pattern matching that I had to go through to identify an expression as an AE in the first place. My next step was going to be parsing the expression into a bunch of tagged elements (so I'd only need to read the tag).  For example (+ 2 3) would be parsed as (('builtin '+) ('num 2) ('num 3)).</div><div><br></div><div>Now, I kind of suspect that all this redundant pattern matching wouldn't really slow things down much (because the computations would be cached), but this hardly seems a very nice way to write the code.</div><div><br></div><div>So, I guess I'm asking whether this analysis is reasonable. Using match seems to be an elegant way of writing an evaluator. It is certainly more compact than a chain of conditionals, but it seems to introduce a lot of extraneous computation.</div><div><br></div><div>(Oh, if you're wondering about the use of with-handlers, I've wrapped this all in a REPL loop, and I don't want to be kicked out because of a typo or division by zero or some such thing.)</div><br class="khtml-block-placeholder"></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">"The most incomprehensible thing about the world is that it is at all comprehensible."</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> --Albert Einstein (1879-1955)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><div><a href="http://www.gwoodhouse.com">http://www.gwoodhouse.com</a></div><div><a href="http://GregWoodhouse.ImageKind.com">http://GregWoodhouse.ImageKind.com</a></div><div><br></div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br class="khtml-block-placeholder"></div><br class="Apple-interchange-newline"></span><br class="Apple-interchange-newline"></span></span> </div><br></div></body></html>