正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology This means that this expression always has the same value, no matter when I evaluate it, and the procedure acts like a mapping from an input value to an output value, independent of time Assignment--set Slide 11.1.6 Substitution model- functional programming Once we introduce mutation or assignment into our language, (define z 10) this model no longer holds. In particular, an expressions value +x5)=>15 now depends on when it is evaluated, that is, what other (+x5)=>15 ame scope as binding expressions have been evaluated before it. In fact, notice the With assignment use of the term when" We have now introduced time into our (define x 10 language in a very fundamental way. Now, two expression +x5) 15-expression value" depends on when it is evaluated with identical syntax may have different semantics, because 【set!x94} they inherently rely on the context surrounding their evaluation (+x5}=>99 To see this, consider the example shown. We again define x to 1001 SICP have the value 10. If we immediately evaluate the expression ( x 5) we will still get the value 15. Now suppose that at some future point, we mutate the value of x to have some new value, 94. Remember that set! finds the existing binding for x and changes it. If we then evaluate (+x 5) we now get 99 as a value, since the value of x has Notice, we now have two syntactically identical expressions, but with different values or meanings or semantics Thus, time now matters. Adding the ability to mutate a value means that context will influence the values of expressions. As we will see, having mutation makes some things much easier to do, but at the cost of greater potential for unanticipated effects Slide 11.1.7 Compound Data Not only can we mutate names, we can also mutate other basic data structures. For pairs, we also have procedures that change creates a new pair p their pieces To remind you, the constructor for a pair is the primitive selectors procedure cons, and the associated selectors are car and returns car part of pair returns cdr part of pair cdr. Now we introduce two mutators, one for each part of the mutators. data structure Their forms are shown on the slide (set-car! P new-x) changes car pointer in pair Both of these are normal procedures. Their behavior is to (set-cdr! p new-y) changes evaluate the first argument, which is expected to be a pair(or i Pair, anytype - undef something made by cons). They also evaluate their second 6 001 SICP argument to get a new value or structure. The behavior is to then take the pair pointed to by the first argument and change or mutate either the car or cdr part of that pair ( depending on which expression we are using) to point to the value of the second argument, thereby breaking the current pointer in the car or cdr part of the pair Notice the type definition of these procedures. In particular, the return type is unspecified, since the procedure is Ised strictly for the side effect of changing the pair structure6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. This means that this expression always has the same value, no matter when I evaluate it, and the procedure acts like a mapping from an input value to an output value, independent of time. Slide 11.1.6 Once we introduce mutation or assignment into our language, this model no longer holds. In particular, an expressions value now depends on when it is evaluated, that is, what other expressions have been evaluated before it. In fact, notice the use of the term "when". We have now introduced time into our language in a very fundamental way. Now, two expressions with identical syntax may have different semantics, because they inherently rely on the context surrounding their evaluation. To see this, consider the example shown. We again define x to have the value 10. If we immediately evaluate the expression (+ x 5) we will still get the value 15. Now suppose that at some future point, we mutate the value of x to have some new value, 94. Remember that set! finds the existing binding for x and changes it. If we then evaluate (+ x 5) we now get 99 as a value, since the value of x has changed. Notice, we now have two syntactically identical expressions, but with different values or meanings or semantics. Thus, time now matters. Adding the ability to mutate a value means that context will influence the values of expressions. As we will see, having mutation makes some things much easier to do, but at the cost of greater potential for unanticipated effects. Slide 11.1.7 Not only can we mutate names, we can also mutate other basic data structures. For pairs, we also have procedures that change their pieces. To remind you, the constructor for a pair is the primitive procedure cons, and the associated selectors are car and cdr. Now we introduce two mutators, one for each part of the data structure. Their forms are shown on the slide. Both of these are normal procedures. Their behavior is to evaluate the first argument, which is expected to be a pair (or something made by cons). They also evaluate their second argument to get a new value or structure. The behavior is to then take the pair pointed to by the first argument and change or mutate either the car or cdr part of that pair (depending on which expression we are using) to point to the value of the second argument, thereby breaking the current pointer in the car or cdr part of the pair. Notice the type definition of these procedures. In particular, the return type is unspecified, since the procedure is used strictly for the side effect of changing the pair structure
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有