正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 12 4.20 (ca)I gg We know what should happen in this case. We just did this The evaluation will again mutate the value of n to be one more make-counter: than its current value. thus changing the value from 1 to 2 in this frame Q b: (lambda ( (+n1) empty b: (set! n ( n 1))n (set! n (+ n 1))IE36001 SICP And having evaluated that part of the body, we now take the(oa)Is=>2 Slide 12 4.21 second part, n, and evaluate that with respect to E3. Chasing up make-counter the frame pointers finds the binding in El, and returns that value, 2. Since this is the value of the last expression in the body of ca, this is the value returned by the whole thing Thus we see how this local piece of state allows us to have a procedure, which when evaluated in successive turns, returns a empty (set! n ( n 1)) 1) (define ab (make-counter o))I ge lide12.4.2 So our environment model helps us understand how one counter, ca, can have some local state which it can keep mutating. Thus, it can return a different value each time it is What happens when we call make-counter again starting from 0, but giving it a different name Slide 12 4.23 (define cb (make-counter 0)) And just to recap, here is the environment structure we have to this point. There is a variable ca pointing to the procedure GeA make-counter shown, with local state of n equal to 2 n:2 b: (lambda ( +n1}) b: (set! n 60: sO6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 12.4.20 We know what should happen in this case. We just did this! The evaluation will again mutate the value of n to be one more than its current value, thus changing the value from 1 to 2 in this frame. Slide 12.4.21 And having evaluated that part of the body, we now take the second part, n, and evaluate that with respect to E3. Chasing up the frame pointers finds the binding in E1, and returns that value, 2. Since this is the value of the last expression in the body of ca, this is the value returned by the whole thing. Thus we see how this local piece of state allows us to have a procedure, which when evaluated in successive turns, returns a different value. Slide 12.4.22 So our environment model helps us understand how one counter, ca, can have some local state which it can keep mutating. Thus, it can return a different value each time it is applied. What happens when we call make-counter again, starting from 0, but giving it a different name. Slide 12.4.23 And just to recap, here is the environment structure we have to this point. There is a variable ca pointing to the procedure shown, with local state of n equal to 2
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有