正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 6.3.1 So we have now seen how procedures can be passed in as Computing derivatives arguments to other procedures. But we can also have procedures return procedures as values Where might that be f:x→x2 f Df:x→>2x Well here is a simple mathematical example. What is a derivative? First, we know that given some function f, such as We can easily write f in either case (define f (lambda(x)(x xx))) the two shown here, there are standard rules for finding the But what is D2? derivative of f which we denote Df as shown We can easily write down a procedure to compute f, but what is )? In particular, can we write a procedure for D that would take the derivative of any function? Computing derivatives Slide 6.3.2 So what do we know about this beast? Well, it needs to map a f: f:x→x3 function to another function, or said slightly differently, it needs Df x→)2x Df:x→3x2 to take as input any procedure that represents a numerical function, and it needs to return a procedure with the property that applying that returned procedure to any value will give us a maps a function(or procedure)to a different function good approximation to the derivative of the original function at that value D(x)≈/(x+a)-f Numerically the equation shown will do a decent job for us 48 6001 SICP Slide 6.3.3 Computing derivatives o how do we then capture the idea of D? Well, here it is Look carefully at the form Deriv takes one argument, which we f:x> x→ see must be a procedure of one numerical argument. Inside the body of the lambda associated with deriv is a second lambda D(x)=(x+)f(x) This is also a procedure of one numerical argument, that returns(define deriv a numerical value lambda (n) So the type of this procedure is as shown (lambda(x)(((f (+ x epsilon))(f x)) epsilon))) 60015 Using Slide 6.3. 4 M(yy) Now does this make sense? Sure (define ensile Suppose we define square as shown, and we take the derivative Notice the nested parentheses: the first compound ambda(x)t( lambda的)yy》( x epsilon ambda的)ryy》x) subexpression should return a procedure as a value, for this to make sense. And to see that it does. use the substitution model We substitute the value of square everywhere in the body of C(((ambda b)t yy))(+ 5 epsilon)) deriv that we see the formal parameter f. The mess that we get da bCyyn5)) tly that Now, if we complete the process, we substitute 5 for x, which 10001 me reduces to an expression similar to those we have seen before So we see that this kind of higher order procedure also has a6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 6.3.1 So we have now seen how procedures can be passed in as arguments to other procedures. But we can also have procedures return procedures as values. Where might that be useful? Well here is a simple mathematical example. What is a derivative? First, we know that given some function f, such as the two shown here, there are standard rules for finding the derivative of f, which we denote Df, as shown. We can easily write down a procedure to compute f, but what is D? In particular, can we write a single procedure for D that would take the derivative of any function? Slide 6.3.2 So what do we know about this beast? Well, it needs to map a function to another function, or said slightly differently, it needs to take as input any procedure that represents a numerical function, and it needs to return a procedure with the property that applying that returned procedure to any value will give us a good approximation to the derivative of the original function at that value. Numerically, the equation shown will do a decent job for us. Slide 6.3.3 So how do we then capture the idea of D? Well, here it is. Look carefully at the form. Deriv takes one argument, which we see must be a procedure of one numerical argument. Inside the body of the lambda associated with deriv is a second lambda. This is also a procedure of one numerical argument, that returns a numerical value. So the type of this procedure is as shown. Slide 6.3.4 Now does this make sense? Sure. Suppose we define square as shown, and we take the derivative. Notice the nested parentheses: the first compound subexpression should return a procedure as a value, for this to make sense. And to see that it does, use the substitution model. We substitute the value of square everywhere in the body of deriv that we see the formal parameter f. The mess that we get does exactly that. Now, if we complete the process, we substitute 5 for x, which reduces to an expression similar to those we have seen before. So we see that this kind of higher order procedure also has a
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有