正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 5.2.8 Com pound Data In this way, we can create elements that are naturally thought of Treat a PAIR as a single unit as simple units that happen to themselves be created out of Can return as a value elements that are also thought of as simple units. This allows us (det ine (make-point x y to build up levels of hierarchy of data abstractions For example, suppose we want to build a system to reason about figures drawn in the plane. Those figures might be built out of line segments that have start and end points, and those points are built out of x and y coordinates Notice how there is a contract between make-point and point-x or point-y, in which the selectors get out the pieces that are 0O1 sIcP I glued together by the constructor. Because they are built on top of cons, car and cdr, they inherit the same contract that holds there. And in the case of segments, these pieces are glued together as if they are primitives, so that we have cons pairs whose elements are also cons pairs Thus we see how cons pairs have the property of closure, in which the result of consing can be treated as primitive input to another level of abstraction Slide 5.2.9 Pair Abstraction We can formalize what we have just seen, in terms of the abstraction of a pair. This abstraction has several standard parts A xB First, it has a constructor, for making instances of this abstraction The constructor has a kind of contract. in which objects a and b are glued together to construct a new object, called a Pair, with two pieces inside c出:n王,B→B Second, it has some selectors or accessors to get the pieces back (cd》=>y> out. Notice how the contract specifies the interaction between Predicate the constructor and the selectors, whatever is put together can be 1xx2)0b02 -->#t if <z> evaluates to a pair, else #f pulled back apart using the appropriate selector Typically, a data abstraction will also have a predicate, here called pair?. Its role is to take in any object, and return true if the object is of type pair. This allows us to test objects for their type, so that we know whether to apply particular selectors to that object The key issue here is the contract between the constructor and the selectors. The details of how a constructor puts things together are not at issue, so long as however the pieces are glued together, they can be separated back out into the original parts by the selectors Slide 5.2.10 Note how there exists a contract between the constructor So here we just restate that idea, one more time, stressing the idea of the contract that defines the interaction between car(cons<a><b>)→<> constructor and selectors. And we stress one more time the idea car(cons<a><b>)→<b> Note how pairs have the property of closure -we can us that pairs are closed, that is, they can be input to the operation of the result of a pair as an element of a new pair. making other pairs (cons(cons 1 2)3)6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 5.2.8 In this way, we can create elements that are naturally thought of as simple units that happen to themselves be created out of elements that are also thought of as simple units. This allows us to build up levels of hierarchy of data abstractions. For example, suppose we want to build a system to reason about figures drawn in the plane. Those figures might be built out of line segments that have start and end points, and those points are built out of x and y coordinates. Notice how there is a contract between make-point and point-x or point-y, in which the selectors get out the pieces that are glued together by the constructor. Because they are built on top of cons, car and cdr, they inherit the same contract that holds there. And in the case of segments, these pieces are glued together as if they are primitives, so that we have cons pairs whose elements are also cons pairs. Thus we see how cons pairs have the property of closure, in which the result of consing can be treated as primitive input to another level of abstraction. Slide 5.2.9 We can formalize what we have just seen, in terms of the abstraction of a pair. This abstraction has several standard parts. First, it has a constructor, for making instances of this abstraction. The constructor has a kind of contract, in which objects A and B are glued together to construct a new object, called a Pair, with two pieces inside. Second, it has some selectors or accessors to get the pieces back out. Notice how the contract specifies the interaction between the constructor and the selectors, whatever is put together can be pulled back apart using the appropriate selector. Typically, a data abstraction will also have a predicate, here called pair?. Its role is to take in any object, and return true if the object is of type pair. This allows us to test objects for their type, so that we know whether to apply particular selectors to that object. The key issue here is the contract between the constructor and the selectors. The details of how a constructor puts things together are not at issue, so long as however the pieces are glued together, they can be separated back out into the original parts by the selectors. Slide 5.2.10 So here we just restate that idea, one more time, stressing the idea of the contract that defines the interaction between constructor and selectors. And, we stress one more time the idea that pairs are closed, that is, they can be input to the operation of making other pairs
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有