正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide lll18 Your turn When we evaluate the set-car! expression, we first get 4+- the value of x, which we know points to the top list structure We also evaluate y, which points to the second list structure Then, we find the car box of the structure pointed to by x, and ((12) break the existing pointer in that box. We replace it with a pointer to the value of y, namely the second list structure as If we now evaluate x, we can see it points to a list of two elements. The first element happens to be a list of two elements, 6 001 SICP Ma I and 2, and the second element is just the number 4.Thus, the value of x prints out as shown Slide 1l.1. 19 Your turn Now remember that time becomes important once mutation is allowed. Thus the change we have just made to x stays in place y==>(1 2) x彐 when we go to evaluate the next mutation. This says to get the value of y, which still points to the second list structure, and car! x y) (12)4) we break the pointer in the cdr part of the cons pair pointed to by y. In its place, we insert a pointer to the value of the set-cdr! y (cdr x) second argument, which is just the cdr pointer of the cons ((14)4) pair pointed to by x If we then evaluate x again we now get the form shown. simply by tracing through the list structure. Notice that due to the sharing of structure, the value of x has changed, even though no explicit expression involving a change in x was evaluated Slide ll1.20 So here is a summary of the key points of this part of the Scheme provides built-in mutators set! to change a binding lecture set-car! and set-cdr! to change a pair Mutation introduces substantial complexity Substitution model is no longer sufficient to explain behavio 6.001 Notes: Section 11.26.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 11.1.18 When we evaluate the set-car! expression, we first get the value of x, which we know points to the top list structure. We also evaluate y, which points to the second list structure. Then, we find the car box of the structure pointed to by x, and break the existing pointer in that box. We replace it with a pointer to the value of y, namely the second list structure as shown. If we now evaluate x, we can see it points to a list of two elements. The first element happens to be a list of two elements, 1 and 2, and the second element is just the number 4. Thus, the value of x prints out as shown. Slide 11.1.19 Now remember that time becomes important once mutation is allowed. Thus the change we have just made to x stays in place when we go to evaluate the next mutation. This says to get the value of y, which still points to the second list structure, and we break the pointer in the cdr part of the cons pair pointed to by y. In its place, we insert a pointer to the value of the second argument, which is just the cdr pointer of the cons pair pointed to by x. If we then evaluate x again, we now get the form shown, simply by tracing through the list structure. Notice that due to the sharing of structure, the value of x has changed, even though no explicit expression involving a change in x was evaluated. Slide 11.1.20 So here is a summary of the key points of this part of the lecture. 6.001 Notes: Section 11.2
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有