正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 3.1.5 Here is a more detailed example. First, let's create two Substitution model details procedures. These expressions will each generate a procedure, (define square (lambda (*)(* xx))) ough the lambda expressions, and the defines will then define average(1 ambda(xy)《/什+xy)2}) associate a name with each of them Thus in our environment we have pairings of square and average to the associated procedure objects, each with its own parameter list and body 6001 sICP Substitution model details Slide 3. 1.6 Here is an example using these procedures. To evaluate this (计:m222 first expression using the substitution model, I first need to get the values of the sub-expressions. The value of average I get by looking this name up in the environment, giving me the 3)) associated procedure object. The value of 5 is easy. The last sub- verage 5(*33) (average 5 9) square and the value 3, then substitute 3 into the body of square for x. This reduces to another combination which i can 47108 601 SICP ecognize is an application of a primitive procedure, so this reduces to the number 9. Thus the recursive application of the substitution model yields a simpler combinatio Note the format here. I first evaluate the sub-expressions, then substitute and apply the procedure. This is called an applicative order evaluation model Slide 3.1.7 Substitution model details Notice in particular that under this model I need to get the value of the operands first, which caused me to evaluate that interior (define square.(ambda (x y)(/(+ x y) 2))) (define average compound expression before I got around to applying average Once I have simple values, I can continue the substitution In this e the body of the procedure associated with rage 5 (square 3) average, now substituting 5 and 9 for x and y in that body rage5(33)) Note that there is no confusion about which x I am referring to, it's the one in the procedure associated with age, not the if operator is a primitive procedure. replace by result of operation 600SC the case of a compound expression, I must reduce this to a pler value before I can continue with the rest of the expression. Also note that when the application involves a primitive procedure, I just do the associated operation, eventually reducing this to the final answer The key idea is to see how this notion of substitution lets us trace out the evolution of an expression s evaluation reducing an application of a procedure to the evaluation of a simpler expression6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 3.1.5 Here is a more detailed example. First, let's create two procedures. These expressions will each generate a procedure, through the lambda expressions, and the defines will then associate a name with each of them. Thus, in our environment, we have pairings of square and average to the associated procedure objects, each with its own parameter list and body. Slide 3.1.6 Here is an example using these procedures. To evaluate this first expression using the substitution model, I first need to get the values of the sub-expressions. The value of average I get by looking this name up in the environment, giving me the associated procedure object. The value of 5 is easy. The last sub￾expression is itself a combination, so I recursively apply the rules. By the same reasoning I get the procedure associated with square and the value 3, then substitute 3 into the body of square for x. This reduces to another combination, which I can recognize is an application of a primitive procedure, so this reduces to the number 9. Thus the recursive application of the substitution model yields a simpler combination. Note the format here. I first evaluate the sub-expressions, then substitute and apply the procedure. This is called an applicative order evaluation model. Slide 3.1.7 Notice in particular that under this model I need to get the value of the operands first, which caused me to evaluate that interior compound expression before I got around to applying average. Once I have simple values, I can continue the substitution. In this case, I use the body of the procedure associated with average, now substituting 5 and 9 for x and y in that body. Note that there is no confusion about which x I am referring to, it's the one in the procedure associated with average, not the one in square. As before, I substitute into the body of this procedure, and then continue. I recursively apply the rules to each subexpression. In the case of a compound expression, I must reduce this to a simpler value before I can continue with the rest of the expression. Also note that when the application involves a primitive procedure, I just do the associated operation, eventually reducing this to the final answer. The key idea is to see how this notion of substitution lets us trace out the evolution of an expression's evaluation, reducing an application of a procedure to the evaluation of a simpler expression
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有