正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 6.4.6 Using com mon patterns over data structures o see how we can use these ideas, lets go back to the idea of er ideas about summation, but now for sums of a particular form. Suppose we common patterns using these general procedures. Suppose we want to compute a particular kind of want to add up the values of a function at a regularly spaced set of intervals. The mathematical expression captures this idea, for n+l regularly spaced samples, each delta apart, starting at ∑f(a+i=fa)+f(a+)+(a+2)+…+f(a+n3)someponta Slide 6.4.7 Using common patterns over data structures Well, we can easily capture this idea using our notions of (define (generate-interval a b) operations over lists. To generate a list of integers, we can (if o a b create a procedure, generate-interval, that cons'es up a list (cons a (generate-interval ( 1 a) b)))) Given the set of integers between 0 and n, which we can create (define (sun f start inc terms) using this procedure, we can"map"a procedure down that list, o1d-r⊥ght+0 omputing the function f applied to sample points: note how we (map (lambda (x) (t ( tart ( x inc)))) venerate- interva10七xm))》 multiply each term in the integer list by a constant (inc), then add an offset(start)to reflect the general formula. This creates a new list, of samples of f applied to a set of points. To add f(a+i6 them up, we fold" them together So we see that we can combine our idea of higher order procedures on numbers with higher order procedures that operate over data structures 6.001 Notes: Section 6.5 Slide 6.5.1 ntegration as a procedure Let's stop to stress an important point. Why are higher order procedures useful? It is not just to make writing easier. Instead higher order procedures tremendously increase the expressive power in our language. They form building blocks that suppress unnecessary detail inside of units that we can treat as primitives, thus allowing us to build more complex structures out of units that themselves may be complex structures To stress this idea, let's take the concept of sum which we just captured as a higher order procedure, and let's see what we can do if we treat sum as a primitive procedure, a primitive building 4 block for other procedures6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 6.4.6 To see how we can use these ideas, let’s go back to the idea of summation, but now for sums of a particular form. Suppose we want to add up the values of a function at a regularly spaced set of intervals. The mathematical expression captures this idea, for n+1 regularly spaced samples, each delta apart, starting at some point a. Slide 6.4.7 Well, we can easily capture this idea using our notions of operations over lists. To generate a list of integers, we can create a procedure, generate-interval, that cons’es up a list. Given the set of integers between 0 and n, which we can create using this procedure, we can “map” a procedure down that list, computing the function f applied to sample points: note how we multiply each term in the integer list by a constant (inc), then add an offset (start) to reflect the general formula. This creates a new list, of samples of f applied to a set of points. To add them up, we “fold” them together. So we see that we can combine our idea of higher order procedures on numbers with higher order procedures that operate over data structures. 6.001 Notes: Section 6.5 Slide 6.5.1 Let's stop to stress an important point. Why are higher order procedures useful? It is not just to make writing easier. Instead, higher order procedures tremendously increase the expressive power in our language. They form building blocks that suppress unnecessary detail inside of units that we can treat as primitives, thus allowing us to build more complex structures out of units that themselves may be complex structures. To stress this idea, let's take the concept of sum which we just captured as a higher order procedure, and let's see what we can do if we treat sum as a primitive procedure, a primitive building block for other procedures
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有