正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 12.1 Slide 12.1.1 In the last lecture, we introduced mutation as a component of 6001s|cP our data structures We saw for example that set was a Environment mode way of changing the value associated with a variable in our system, and we saw that set-car! and set-cdr! were ways of changing the values of parts of list structure Now, several important things happened when we introduced mutation. First we introduced the notion of time and context into our interpretation Scheme. The order in which things were evaluated now mattered in terms of changes in returned values As a consequence, secondly we shifted from a functional 60015e programming perspective to a more state-based programming perspective, a point to which we will return. Third, we unfortunately introduced the opportunity for bugs and errors in our system, since shared objects allow the mutation of one to affect the value of the other, another point to which we will return. And finally, fourth, we broke the substitution model This last point needs to be addressed, and indeed in addressing it, we will also address many of these other points In this lecture, then, we are going to replace the substitution model with a stronger model, that incorporates the substitution model as a piece of it, but also accounts for state time, context and mutation Slide 12.1.2 6001s|cP To stress this idea that the substitution model no longer holds. Environment model consider the following example. Why does this code behave in Can you figure out why this code works? his manner? (lambda (n) We can see that make-counter is a higher-order (lambda ( (set! n ( n 1) procedure, that is, it returns a procedure as its value. Suppose (define ca (make-counter 0)) we use it to create a counter called ca. now if we all this procedure(or evaluate its application) several times, we see that and cb are independent t is behavior is to count starting at 1, increasing the returned (define cb (make-counter o value by one each time. Thus the standard substitution model 6 001 SICP (or functional programming model) no longer holds, because the same expression is being evaluated each time, but a different value results, depending on when we evaluate the expression If we create a second counter, cb, we end up with different structures. as shown in the last two examples, these two counters are independent, with ch now counting from 1, but ca continuing to count from its place. Thus, even though these objects were created by evaluating the same expression, they do not share any common state So, our substitution model is broken, caused by the introduction of mutation. We thus need a better model that would explain how this code behaves6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 12.1 Slide 12.1.1 In the last lecture, we introduced mutation as a component of our data structures. We saw, for example, that set! was a way of changing the value associated with a variable in our system, and we saw that set-car! and set-cdr! were ways of changing the values of parts of list structure. Now, several important things happened when we introduced mutation. First, we introduced the notion of time and context into our interpretation Scheme. The order in which things were evaluated now mattered in terms of changes in returned values. As a consequence, secondly we shifted from a functional programming perspective to a more state-based programming perspective, a point to which we will return. Third, we unfortunately introduced the opportunity for bugs and errors in our system, since shared objects allow the mutation of one to affect the value of the other, another point to which we will return. And finally, fourth, we broke the substitution model. This last point needs to be addressed, and indeed in addressing it, we will also address many of these other points. In this lecture, then, we are going to replace the substitution model with a stronger model, that incorporates the substitution model as a piece of it, but also accounts for state, time, context and mutation. Slide 12.1.2 To stress this idea that the substitution model no longer holds, consider the following example. Why does this code behave in this manner? We can see that make-counter is a higher-order procedure, that is, it returns a procedure as its value. Suppose we use it to create a counter, called ca. Now if we all this procedure (or evaluate its application) several times, we see that is behavior is to count starting at 1, increasing the returned value by one each time. Thus the standard substitution model (or functional programming model) no longer holds, because the same expression is being evaluated each time, but a different value results, depending on when we evaluate the expression. If we create a second counter, cb, we end up with different structures. As shown in the last two examples, these two counters are independent, with cb now counting from 1, but ca continuing to count from its place. Thus, even though these objects were created by evaluating the same expression, they do not share any common state. So, our substitution model is broken, caused by the introduction of mutation. We thus need a better model that would explain how this code behaves
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有