正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 12.3.13 In particular, we get back the value 4. Not the value 10 to (square 4)I gE which x is bound in the global environment! Remember we start in the first frame of El looking for a binding for this * #[prim] square variable. Since there is such a binding. it shadows the other binding in the global environment body (*x x:4 ( 3 *)E1 lg=>4 (square 4)I gE Slide 12.3.14 s pre We get the value of th x;10 econd x in the same way. Thus we are left with the application *: #[prim] a primitive procedure to numbers. This just returns t the value 16, which is the value of the last expression in the body of the procedure being applied. Thus, this is the value returned for the x:4 entire expression square I GE ==>#[proc] Although this was a long example, step back to notice how the >16 mechanistic rules of the environment model simply tell us how |1=>#[prim] lg=>4 to trace the evaluation of an expression with respect to an I environment, reducing it to simpler expressions Slide 12.3.15 Example: inc-square Now, let's be slightly more daring! Having seen the application of a simple procedure like square, let,'s look at something GE-anc-square that involves a little more work. In particular, let,'s assume that we have defined square and inc-square, as shown in this environment structure P: 2 b: (* I x) b:(+1《 square y)) (define square (lambda (x)(*x x)))I (define ing-square (lambda (y)(+ 1 (square y)))I Example contd: (inc-square 4)I ge Slide 12.3.16 So lets evaluate (inc-square 4) with respect to the inc-square global environment, and here is the environment structure corresponding to that environment6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 12.3.13 In particular, we get back the value 4. Not the value 10 to which x is bound in the global environment! Remember we start in the first frame of E1 looking for a binding for this variable. Since there is such a binding, it shadows the other binding in the global environment. Slide 12.3.14 And now we complete this process. We get the value of the second x in the same way. Thus we are left with the application of a primitive procedure to numbers. This just returns the value 16, which is the value of the last expression in the body of the procedure being applied. Thus, this is the value returned for the entire expression. Although this was a long example, step back to notice how the mechanistic rules of the environment model simply tell us how to trace the evaluation of an expression with respect to an environment, reducing it to simpler expressions. Slide 12.3.15 Now, let's be slightly more daring! Having seen the application of a simple procedure like square, let's look at something that involves a little more work. In particular, let's assume that we have defined square and inc-square, as shown in this environment structure. Slide 12.3.16 So lets evaluate (inc-square 4) with respect to the global environment, and here is the environment structure corresponding to that environment
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有