正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 12 4.13 First. we know what a set! does. but let's look at this (ca)I Gi carefully. First, we will get the value of ( n 1)with respect to E2. This means we look up the value of n with counter: respect to E2. Since it is not bound there, we go up the enclosing environment pointer, to find the binding in E1. This oC gives us the value 0. Thus we add 0 to 1, to get 1 b: (1 ambda ( Notice how that local frame has capture n for us. Given that ( n 1)) c empty we can now evaluate the set! with respect to E2. This says, n) +n1})n starting in E2, trace up the environment chain until we find a set!n(+ n 1))116001 sIcp binding for n, which we find in el. we then change that binding to the newly computed value, which is 1 (ca)|c==>1 Slide 12.4.14 Having evaluated that part of the body of the procedure, we hen evaluate that next part, n, with respect to E2. Using the same rules, we chase up the chain of environment pointers until we find a binding for n. returning the value 1 as the value for this expression. Since this is the last expression in the body of b: (lambda ( the procedure, this is also the value we return for the application +n1)) of the procedure itself. empty The key thing to note here is how that local frame, El, captures b: (set! n ( n 1))n some state information that is accessible only by this procedure set! n ( n 1))Ir2601scpn In= I Calling ca gives us the ability to get this value of n, change or mutate it. and then return that value Slide 12.4.15 Now let's see what happens if we call ca again. We should expect to see the behavior of the value incrementing, so let's make-counter evaluate (ca) again with respect to the global environment Shown is a replication of the environment structure left from the last evaluation, with n having been mutated from 0 to l as part of that process b: (lambda ( +n1}) (set! n ( n 1))n 600SC6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 12.4.13 First, we know what a set! does, but let's look at this carefully. First, we will get the value of (+ n 1) with respect to E2. This means we look up the value of n with respect to E2. Since it is not bound there, we go up the enclosing environment pointer, to find the binding in E1. This gives us the value 0. Thus we add 0 to 1, to get 1. Notice how that local frame has capture n for us. Given that, we can now evaluate the set! with respect to E2. This says, starting in E2, trace up the environment chain until we find a binding for n, which we find in E1. We then change that binding to the newly computed value, which is 1. Slide 12.4.14 Having evaluated that part of the body of the procedure, we then evaluate that next part, n, with respect to E2. Using the same rules, we chase up the chain of environment pointers until we find a binding for n, returning the value 1 as the value for this expression. Since this is the last expression in the body of the procedure, this is also the value we return for the application of the procedure itself. The key thing to note here is how that local frame, E1, captures some state information that is accessible only by this procedure. Calling ca gives us the ability to get this value of n, change or mutate it, and then return that value. Slide 12.4.15 Now let's see what happens if we call ca again. We should expect to see the behavior of the value incrementing, so let's evaluate (ca) again with respect to the global environment. Shown is a replication of the environment structure left from the last evaluation, with n having been mutated from 0 to 1 as part of that process
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有