正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 15.2.19 Notice where we are at this stage. We have unwrapped this 1. Arith metic calculator compound expression into a nested sequence of operations of p1us*24(p1us*56}) primitive things to primitive values. At this stage we can gather What are the argument and return values of eval each up the things we have left to do We have some deferred time it is called in the evaluation of ine 17? operations, for example in the topmost eval-sum we can now add 5 and 6 to get ll, and so on, reducing all the deferred 1-m(1u*561)1 primitive operations down to a single value 124124(a1u#561 (eva1-n'(plu当241u青S6))) A'(p1u+24(1u+56) 1. Things to observe Slide 15.2.20 cond determines the expression type hus, we have built a simple evaluator that handles sums of no more than two arguments. here are some key points to notice schemes reader has already done the work from this exercise, since our goal is to understand the process of it converts a sequence of characters like 24" to an evaluation intemal binary representation of the number 24 First, eval does type tag checking It dispatches based on eval-sum recursively calls eval on both argument expressions type, much like we saw earlier in the course Second, numbers are just numbers, so there is nothing really to Third, complex expressions nicely get recursively evaluated in I pieces. Eval unwraps a complex expr evaluation of the simpler parts, plus a deferred operation to ather the values back together. Numbers just get handled as numbers. and eventually we reduce this whole thing down to a set of primitive operations on primitive values 6.001 Notes: Section 15.3 Slide 15.3.1 2. Names Okay, now let's build on this basic system. Suppose we want to Extend the calculator to store intermediate results give names to things. For example, suppose we want to have the behavior shown here. in which we can store intermediate (define** (plus* 4 5)) store result as x results as named values, and then just use those names (plus* ac* 2) anywhere that we would want to use the actual expression and its resulting value. This is the kind of behavior we saw earlier in the term in Scheme. how would we add that behavior to the evaluator we are building for simple arithmetic expressions?6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 15.2.19 Notice where we are at this stage. We have unwrapped this compound expression into a nested sequence of operations of primitive things to primitive values. At this stage we can gather up the things we have left to do. We have some deferred operations, for example in the topmost eval-sum we can now add 5 and 6 to get 11, and so on, reducing all the deferred primitive operations down to a single value. Slide 15.2.20 Thus, we have built a simple evaluator that handles sums of no more than two arguments. Here are some key points to notice from this exercise, since our goal is to understand the process of evaluation. First, eval does type tag checking. It dispatches based on type, much like we saw earlier in the course. Second, numbers are just numbers, so there is nothing really to do. Third, complex expressions nicely get recursively evaluated in pieces. Eval unwraps a complex expression into an evaluation of the simpler parts, plus a deferred operation to gather the values back together. Numbers just get handled as numbers. And eventually we reduce this whole thing down to a set of primitive operations on primitive values. 6.001 Notes: Section 15.3 Slide 15.3.1 Okay, now let's build on this basic system. Suppose we want to give names to things. For example, suppose we want to have the behavior shown here, in which we can store intermediate results as named values, and then just use those names anywhere that we would want to use the actual expression and its resulting value. This is the kind of behavior we saw earlier in the term in Scheme, how would we add that behavior to the evaluator we are building for simple arithmetic expressions?
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有