正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 5.4.5 First, here's our data abstraction for rational numbers, which has Rational Number Abstraction the form we expect. It has a constructor, make-rat which Constructor Jhe-zae: integer, integer " Rat takes two integers as input, and produces a rational, with some 2. accessors kind of internal representation that we haven,'t specified. Well canom:Rat→> integon need selectors for getting back out the pieces of a rational Notice the type associated with numer and denom. Most important is the contract between these parts. We can apply either of the selectors to the object created by the atxy):·Rt:Rat,R constructor, and be guaranteed to get back the appropriate piece, ay nothing about i independent of how the object is actually represented We will also have some operations that apply to rationals, such L4 6001 SIP as rational addition or rational multiplication, and notice the types of these operations. They take two rationals as input, and produce a rational as output Of course, we are going to separate all of this from the actual implementation of rationals. We should ideally be able to take any implementation of rationals that satisfies these contracts and build our own procedures to use those objects, just by relying on the constructor and selectors, and the contract between them Rational Number Abstraction Slide 5.4.6 Now let's think about actually building an implementation for rationals. How do we go about building an implementation that satisfies the contract defined in the previous slide while 5. Abstraction Barrier providing all the pieces we laid out Slide 5.4.7 Rational number abstraction Well here is a simple implementation for rationals. We could use pairs as our underlying representation for rationals. Using 2. Acssorts this, we can easily construct the constructor and selectors for rationals, using cons, car and cdr. While this seems 5. Abstraction barrier like an obvious way to build rationals, there is an important oint here. In particular, we can enforce the contract that we 6. Concrete Representation Implementation require between make -rat, numer and denom by define (make-rat n d)(cons n d)) inheriting the contract between cons, car and cdr.At the same time, by relying on separate constructors and selectors for rationals, we shield the user from the implementation details and we will see shortly why that is important6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 5.4.5 First, here's our data abstraction for rational numbers, which has the form we expect. It has a constructor, make-rat which takes two integers as input, and produces a rational, with some kind of internal representation that we haven't specified. We'll need selectors for getting back out the pieces of a rational. Notice the type associated with numer and denom. Most important is the contract between these parts. We can apply either of the selectors to the object created by the constructor, and be guaranteed to get back the appropriate piece, independent of how the object is actually represented. We will also have some operations that apply to rationals, such as rational addition or rational multiplication, and notice the types of these operations. They take two rationals as input, and produce a rational as output. Of course, we are going to separate all of this from the actual implementation of rationals. We should ideally be able to take any implementation of rationals that satisfies these contracts and build our own procedures to use those objects, just by relying on the constructor and selectors, and the contract between them. Slide 5.4.6 Now let's think about actually building an implementation for rationals. How do we go about building an implementation that satisfies the contract defined in the previous slide, while providing all the pieces we laid out? Slide 5.4.7 Well here is a simple implementation for rationals. We could use pairs as our underlying representation for rationals. Using this, we can easily construct the constructor and selectors for rationals, using cons, car and cdr. While this seems like an obvious way to build rationals, there is an important point here. In particular, we can enforce the contract that we require between make-rat, numer and denom by inheriting the contract between cons, car and cdr. At the same time, by relying on separate constructors and selectors for rationals, we shield the user from the implementation details, and we will see shortly why that is important
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有