正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology global environment. Our rule can now be used. The value of variable z with respect to the global environment is clearly 10, as that is the pairing associated with it. If I ask for the value of z with respect to the environment el, I start in frame a Since there is no binding for z in that frame, I move up the enclosing environment pointer to the next frame, in this case, the global environment, and look for the binding of z there. Since there is one, i return the value 10 Finally, if I ask for the value of x with respect to El, it points to the first frame A, and I look there for a binding Since there is one. I return that value. 15 Slide 12.2.5 Name-rule On the other hand, if I ask for the value of x with respect to the in environment E gives global environment, I first look in Frame B for a binding. This the value of x in the first frame of E where Xis bound returns the value 3 the value associated with x in that frame z|=>10xlx=>15 In E1, the binding of x in frame A shadows the binding of x in B In this case. x has a different value in different environments In one, its value is 15, in another it is 3 We say that the it hides it so that if we start in Frame A, we only see the binding/&x OF 15 binding of x in Frame A shadows the binding of x in Frame B Ⅴ isible there ·□ Thus. we see how the name rule tells us the mechanism by which we look up the value associated with a variable in some environment Slide 12.2.6 Given that we have a rule for looking up values associated with creates or replaces a binding in the first frame of E variables, what about a rule for creating bindings for variables? That, we know, is a define expression, and we have a rule for dealing with such expressions. a define special form when evaluated in an environment E, creates or replaces a binding for that variable in the first frame of E, and it always does this in the first frame ofe 6 01 SIP Slide 12.2.7 Here is our environment structure from before. Let's look at a define special form evaluated in environment E what defines do when they are evaluated with respect to creates or replaces a binding in the first frame of E this structure ·口A6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. global environment. Our rule can now be used. The value of variable z with respect to the global environment is clearly 10, as that is the pairing associated with it. If I ask for the value of z with respect to the environment E1, I start in frame A. Since there is no binding for z in that frame, I move up the enclosing environment pointer to the next frame, in this case, the global environment, and look for the binding of z there. Since there is one, I return the value 10. Finally, if I ask for the value of x with respect to E1, it points to the first frame A, and I look there for a binding. Since there is one, I return that value, 15. Slide 12.2.5 On the other hand, if I ask for the value of x with respect to the global environment, I first look in Frame B for a binding. This returns the value 3, the value associated with x in that frame. In this case, x has a different value in different environments. In one, its value is 15, in another it is 3. We say that the binding of x in Frame A shadows the binding of x in Frame B, it hides it so that if we start in Frame A, we only see the binding visible there. Thus, we see how the name rule tells us the mechanism by which we look up the value associated with a variable in some environment. Slide 12.2.6 Given that we have a rule for looking up values associated with variables, what about a rule for creating bindings for variables? That, we know, is a define expression, and we have a rule for dealing with such expressions. A define special form, when evaluated in an environment E, creates or replaces a binding for that variable in the first frame of E, and it always does this in the first frame of E. Slide 12.2.7 Here is our environment structure from before. Let's look at what defines do when they are evaluated with respect to this structure
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有