正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 9.1.6 Bert's data structure To complete the representation, Bert just has to ensure that he (define( make-complex-from-rect rl im)(list ri im)) implements selectors that together with the constructors meet (define(make-complex-from-polar mg an) the contract for complex number abstractions For real and (st ( mg(cos an)) C mg(sin an)))) imag parts, this is easy, as we can just rely on the contract for lists For mag and angle, however, we must get out the real (define(imag cx)(cadr cx)) and imaginary parts(since these are the underlying representational pieces )using the appropriate selectors, then (define (angle cx)(atan o(real cx) compute the appropriate values from those parts This then complete Bert's implementation DOt sIcP Slide 9.1.7 Notice that bert made a design choice. He made a decision to Ernie s data structure represent complex numbers by a list of their real and imaginary(define(make-complex-from-rect rl im) (list (sqrt(+(square rl)(square im))) parts. Let's suppose at the same time we also asked Bert's friend (atan im rl))) Ernie, to create an implementation of complex numbers. Since (define( make-complex-from-polar mg an)(list mg an)) Ernie is Canadian he likes cold weather. and thus decides to implement complex numbers in polar form. Thus, his basic representation is a list of magnitude and angle, which means his constructor for polar form is just a list, but if he is given a real and imaginary part, he will first have to convert them to polar form then store those values as a list Ernies data structure Slide 9. 8 (define(make-complex-from-rect rl im) Completing Ernie's task is similar to Berts. For the magnitude (list(sqrt(+(square rl)(square im))) and angle selectors, we can just use the underlying list selectors (atan im rl))) to complete the contract. For selectors for Cartesian coordinates, ( define(make-complex-from-polar mg an)(list mg an)) I we will need to select out the underlying parts, convert using some trigonometry to the appropriate values, and then return 仰(m those values Notice that Ernie's implementation for complex numbers thus also meets the contract for such objects. All that has changed with respect to Bert's implementation is the choice of how to 6001 SICP glue basic components together Slide 9. 1.9 Whose number is it? So far this sounds fine. We have two different implementations of complex numbers, one in Cartesian coordinates and one in uppose we pick up the following object lar coordinates, but both seem to satisfy the contract for data abstraction. what's the big deal? I, suppose we find a complex number lying on the floor, such as the one shown6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 9.1.6 To complete the representation, Bert just has to ensure that he implements selectors that together with the constructors meet the contract for complex number abstractions. For real and imag parts, this is easy, as we can just rely on the contract for lists. For mag and angle, however, we must get out the real and imaginary parts (since these are the underlying representational pieces) using the appropriate selectors, then compute the appropriate values from those parts. This then completes Bert's implementation. Slide 9.1.7 Notice that Bert made a design choice. He made a decision to represent complex numbers by a list of their real and imaginary parts. Let's suppose at the same time we also asked Bert's friend, Ernie, to create an implementation of complex numbers. Since Ernie is Canadian, he likes cold weather, and thus decides to implement complex numbers in polar form. Thus, his basic representation is a list of magnitude and angle, which means his constructor for polar form is just a list, but if he is given a real and imaginary part, he will first have to convert them to polar form, then store those values as a list. Slide 9.1.8 Completing Ernie's task is similar to Bert's. For the magnitude and angle selectors, we can just use the underlying list selectors to complete the contract. For selectors for Cartesian coordinates, we will need to select out the underlying parts, convert using some trigonometry to the appropriate values, and then return those values. Notice that Ernie's implementation for complex numbers thus also meets the contract for such objects. All that has changed with respect to Bert's implementation is the choice of how to glue basic components together. Slide 9.1.9 So far this sounds fine. We have two different implementations of complex numbers, one in Cartesian coordinates and one in polar coordinates, but both seem to satisfy the contract for the data abstraction. What's the big deal? Well, suppose we find a complex number lying on the floor, such as the one shown
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有