正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology (define ca (make-counter 0))Ige Although there will be a lot of details in this example, you should try to follow the high level view of how the model make-counter: explains the computation First. lets evaluate this definition of ca to be the value of Q applying make-counter in the global environment. We have shown our structure for the global environment, with a b: (1 ambda ( binding for make-counter to a procedure object, as (+n1}) would be obtained when we evaluate the definition on the slide Slide 12.4.3 To do this, we know we need to get the value of applying (define ca (make-counter 0))I ge make-counter to o. Make-counter is a make-counter procedure so the next set of stages is something we have already seen. We will drop a frame, extend it into an environment by scoping it by the same environment as the passed in. Thus we get the structure shown, which is exactly like our previous procedure applications (define ca (make-counter 0))I gE Now watch carefully. We now have an evaluation of the body make-counter of that procedure with respect to an environment, and what is that body? It is, itself, a lambda, with no parameters and body that does a mutation and returns a value (lambda ()(set! n to 5 1))n)IE Slide 12. 4.5 (define ca (make-counter 0)) o we just apply our rule for lambdas. We create a double bubble for the procedure object created by the lambda. The I ge 4 make-counter code part of the object is just the formal parameter(in this case nothing)and the body(in this case(set! n 1))and then n). The key where does the second b: (lambda ( part of the double bubble go? What is the environment pointer we want here? +n1}) (-md()(et!n(31)n)|a6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 12.4.2 Although there will be a lot of details in this example, you should try to follow the high level view of how the model explains the computation. First, lets evaluate this definition of ca to be the value of applying make-counter in the global environment. We have shown our structure for the global environment, with a binding for make-counter to a procedure object, as would be obtained when we evaluate the definition on the previous slide. Slide 12.4.3 To do this, we know we need to get the value of applying make-counter to 0. Make-counter is a procedure so the next set of stages is something we have already seen. We will drop a frame, extend it into an environment by scoping it by the same environment as the environment pointer of the procedure, and within that frame, bind the formal parameters of the procedure to the arguments passed in. Thus we get the structure shown, which is exactly like our previous procedure applications. Slide 12.4.4 Now watch carefully. We now have an evaluation of the body of that procedure with respect to an environment, and what is that body? It is, itself, a lambda, with no parameters and a body that does a mutation and returns a value. Slide 12.4.5 So we just apply our rule for lambdas. We create a double bubble for the procedure object created by the lambda. The code part of the object is just the formal parameter (in this case nothing) and the body (in this case (set! n (+ n 1)) and then n). The key issue is where does the second part of the double bubble go? What is the environment pointer we want here?
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有