正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide lll 8 Example: Pair/List Mutation As we will see shortly, having mutation buys us some new power in our computational capabilities, but it also raises some (define b a) a=→>(12) L-n new problems, which we want to highlight first. For example, b=>(12} suppose I create the list of l and 2, and give it the name a. I also give the name b to the same structure, and remember that the second define expression literally binds the name b to the value of a, which we know is the pointer to this box-and pointer structure. Remember that there is no new creation of pairs in this case b literally points to the same structure IfI 6 001 SICP ao ask for the value of a or b, in each case i get the list(1 2) shown in red. This gets the value of a which is the list structure(define bast 12 or Slide ll1,g Now suppose that some time later, I evaluate the expression in the upper right. It gets the value of the second argument, 12 which is just 10. It then takes the car part of the box pointed to by a, breaks the current pointer in that box, and creates a (set-car! a 10) new pointer to the new value, 10 b=>(102) So what! Well. notice a subtle effect. If I ask for the value ofb it has changed! We get the value of b by tracing out the box and-pointer structure, to get(10 2). Yet nowhere in my code is there an explicit expression changing b. In this simple case, we know there is a tie between a and b, but in more general cases you can see how trouble can arise. Given the ability to share data structures, and to mutate data structures, one piece of code could change a value affecting some other variable without realizing it Example 2: Pair/List Mutation Slide lll1o To use these mutators it is important to understand their affect enex3a"b)x山- on variables and structures so that we can work backwards from a desired effect to determine the right mutation to cause that to happen. For example, consider the simple list structure shown here 4 6001 SICP ②D6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 11.1.8 As we will see shortly, having mutation buys us some new power in our computational capabilities, but it also raises some new problems, which we want to highlight first. For example, suppose I create the list of 1 and 2, and give it the name a. I also give the name b to the same structure, and remember that the second define expression literally binds the name b to the value of a, which we know is the pointer to this box-and￾pointer structure. Remember that there is no new creation of pairs in this case, b literally points to the same structure. If I ask for the value of a or b, in each case I get the list (1 2). Slide 11.1.9 Now suppose that some time later, I evaluate the expression shown in red. This gets the value of a which is the list structure in the upper right. It gets the value of the second argument, which is just 10. It then takes the car part of the box pointed to by a, breaks the current pointer in that box, and creates a new pointer to the new value, 10. So what! Well, notice a subtle effect. If I ask for the value of b it has changed!. We get the value of b by tracing out the box￾and-pointer structure, to get (10 2). Yet nowhere in my code is there an explicit expression changing b. In this simple case, we know there is a tie between a and b, but in more general cases you can see how trouble can arise. Given the ability to share data structures, and to mutate data structures, one piece of code could change a value affecting some other variable without realizing it. Slide 11.1.10 To use these mutators it is important to understand their affect on variables and structures, so that we can work backwards from a desired effect to determine the right mutation to cause that to happen. For example, consider the simple list structure shown here
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有