正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 5.3 Slide 5.3.1 Using pair abstractions to build procedures So how do we use the idea of pairs to help us in creating Here are some data abstractions computational entities? To illustrate this, lets stick with our (define Pl example of points and segments. Suppose we construct a couple|(define p2 point 4 3)) sA of points, using the appropriate constructor, and we then glue (define s1 these points together into a segment 12345 6001 sIcP Using pair abstractions to build procedures Slide 5.3.2 Here are some data abstractions Now suppose we want to think about the operation of stretching (define p1 (make-point 1 2)) a point, that is, pulling (or pushing) it along a line from the (define p2(make-point 4 3))5 origin through the point. Ideally, we would just think about this (define sl (make-seg pl p2))4 in terms of operations on elements of a point, without worrying about how the point is actually implemented We do this with (lambda (pt s 2345 the code shown (make-point ( scale (point-x pt)) ( scale (point-y pt))))) Note how this code creates a new data object. If we stretch (stretch-point p1 2)-(2. 4) point Pl, we get a new point. Also note, as an aside, how a cons pl pair prints out, with open and close parentheses, and with the 6001 StCP values of the two parts within those parentheses, separated by a dot. Thus, the point created by applying our stretch procedure has a different value for the x and y parts than the original point, which is still hanging around. Thus, as we might expect from the actual code, we get out the values of the parts of Pl, but then make a new data object with scaled versions of those values as the parts Slide 5.3.3 Using pair abstractions to build procedures nd we can generalize this idea to handle operations on Generalize to other structures builds on constructors and selectors for the appropriate data es|(define stretch-seg segments, as well as points. Note how each of these procedur structure, so that in examining the code, we have no sense of the underlying implementation. These structures happen to be built (stretch-point (end-pt seg out of cons pairs, but from the perspective of the code designer, (deti ne we rely only on the contract for constructors and selectors for (po1nt-x( start=p。⊥nt器eq)) points and segments mh时 end- point sec))))))》6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 5.3 Slide 5.3.1 So how do we use the idea of pairs to help us in creating computational entities? To illustrate this, let’s stick with our example of points and segments. Suppose we construct a couple of points, using the appropriate constructor, and we then glue these points together into a segment. Slide 5.3.2 Now suppose we want to think about the operation of stretching a point, that is, pulling (or pushing) it along a line from the origin through the point. Ideally, we would just think about this in terms of operations on elements of a point, without worrying about how the point is actually implemented. We do this with the code shown. Note how this code creates a new data object. If we stretch point P1, we get a new point. Also note, as an aside, how a cons pair prints out, with open and close parentheses, and with the values of the two parts within those parentheses, separated by a dot. Thus, the point created by applying our stretch procedure has a different value for the x and y parts than the original point, which is still hanging around. Thus, as we might expect from the actual code, we get out the values of the parts of P1, but then make a new data object with scaled versions of those values as the parts. Slide 5.3.3 And we can generalize this idea to handle operations on segments, as well as points. Note how each of these procedures builds on constructors and selectors for the appropriate data structure, so that in examining the code, we have no sense of the underlying implementation. These structures happen to be built out of cons pairs, but from the perspective of the code designer, we rely only on the contract for constructors and selectors for points and segments
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有