正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 5.1.8 Procedural abstraction example: sqrt Now, let's use the tools we seen so far to implement this method To find an approximation of square root ofx: Notice how the first procedure uses the ideas of wishful Improve the guess by averaging G and x/G thinking, and recursive procedures to capture the basic idea of ( define try (lambda (guess x) Herons method. Try is a procedure that takes a current guess quers s and the x, and captures the top-level idea of the method. It try improve gues (define improve (lambda (guoss checks to see if the guess is sufficient. If it is, it simply returns (define average (lambda (a b)(/(+a b)2))) the value of that guess. If it is not, then it tries again, with a new t ats (1 souare wues) m)o oo)) Note how we are using wishful thinking to reduce the problem (det ine sort (lambda (x) (try 1 x))) to another version of the same problem, and to abstract out the DOt sIcP m idea of both getting a new guess and checking for how good the guess is. These are procedures we can subsequently write, for example, as shown. Finally, notice how the recursive call to try will use a different argument for guess, since we will evaluate the expression before substituting into the body Also notice the recursive structure of try and the use of the special form if to control the evolution of this procedure The method for improve simply incorporates the ideas from the algorithm, again with a procedure abstraction to separate out idea of averaging from the procedure for improving the guess Finally, notice how we can build a square root procedure on top of the procedure for try Slide 5.1.9 The universe of procedures for sqrt If we think of each of these procedures as its own black box abstraction, then we can visualize the universe containing these orocedures as shown. Each procedure exists with its own contract, but each is accessible to the user, simply by referring to it by name improVe Good-enut? While this sounds fine in principle, there is a problem with this viewpoint. Some of these procedures are general methods, such as average and sart, and should be accessible to the user who might utilize them elsewhere Some of t however,such as try or good-enuf?, are really specific e to the computation for square roots. Ideally we would like to capture those procedures in a way that they can only be used by sgrt but not by other methods The universe of procedures for sqrt Slide 5.1.10 Abstractly, this is what we would like to do. We would like to move the abstractions for the special purpose procedures inside of the abstraction for sart so that only it can use them while generally useful procedures available to the Improve In this way, these internal procedures should become part of the implementation details for sgrt but be invisible to outside Good-enu? sers6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 5.1.8 Now, let's use the tools we seen so far to implement this method. Notice how the first procedure uses the ideas of wishful thinking, and recursive procedures to capture the basic idea of Heron's method. Try is a procedure that takes a current guess and the x, and captures the top-level idea of the method. It checks to see if the guess is sufficient. If it is, it simply returns the value of that guess. If it is not, then it tries again, with a new guess. Note how we are using wishful thinking to reduce the problem to another version of the same problem, and to abstract out the idea of both getting a new guess and checking for how good the guess is. These are procedures we can subsequently write, for example, as shown. Finally, notice how the recursive call to try will use a different argument for guess, since we will evaluate the expression before substituting into the body. Also notice the recursive structure of try and the use of the special form if to control the evolution of this procedure. The method for improve simply incorporates the ideas from the algorithm, again with a procedure abstraction to separate out idea of averaging from the procedure for improving the guess. Finally, notice how we can build a square root procedure on top of the procedure for try. Slide 5.1.9 If we think of each of these procedures as its own black box abstraction, then we can visualize the universe containing these procedures as shown. Each procedure exists with its own contract, but each is accessible to the user, simply by referring to it by name. While this sounds fine in principle, there is a problem with this viewpoint. Some of these procedures are general methods, such as average and sqrt, and should be accessible to the user, who might utilize them elsewhere. Some of them, however, such as try or good-enuf?, are really specific to the computation for square roots. Ideally we would like to capture those procedures in a way that they can only be used by sqrt but not by other methods. Slide 5.1.10 Abstractly, this is what we would like to do. We would like to move the abstractions for the special purpose procedures inside of the abstraction for sqrt so that only it can use them, while leaving more generally useful procedures available to the user. In this way, these internal procedures should become part of the implementation details for sqrt but be invisible to outside users
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有