正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.3.3 s atteran capture this by giving a name to the part of the What does a procedure describe? that changes with each instantiation; identifying that ame as a formal parameter; and then capturing that pattern as the body of a lambda expression, together with the set of (2525) (foobar foobar) formal parameters, all within a l ambda expression Common pattern to Name for thing tha changes 26203 6 01 sICP Modularity of common patterns Slide 2.3.4 Here is Now lets consider a more complex pattern, as shown here (sqrt(+(33)(*44))) qrt(+(*99)(*1616) (sqrt(+(*44)(*44)) 6001 SICP Slide 2.3.5 Modularity of common patterns In this case, there are two things that vary, so we will need two Here is a common pattern parameters to capture this. Otherwise we could just do the same (sqrt(+(*33)(*44))) thing we did last time, and replicate the pattern, with the (sqxt(+(*99)(*1616))) parameters in place of the things that change, as shown. (sqrt(+(*44)(*44))) (sart (+(*xx)(*yy))) 262003 60015e Modularity of common patterns Slide 2.3.6 But a better way to capture this pattern is to realize that there Here is a common pattern are really two things going on. One is the sub-pattern of (sqrt(+(*99)(*1616))) squaring things. The other is the use of the results of two 44)(*44))) different squaring operations within the larger pattern But here is a cleaner way of capturing pattems So we could capture each of those patterns within its own (define square (lambda (x)(*xx)) procedure, with its own parameters and bod (define pythagoras Note that in doing this, we are relying on a property of a (lambda (at y) combination, namely that a combination involving a named (sqrt ( (square x)(square y))) procedure is equivalent to the pattern captured by the 262000 procedure, with values substituted for the formal parameters6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.3.3 So we can capture this by giving a name to the part of the pattern that changes with each instantiation; identifying that name as a formal parameter; and then capturing that pattern as the body of a lambda expression, together with the set of formal parameters, all within a lambda expression. Slide 2.3.4 Now lets consider a more complex pattern, as shown here. Slide 2.3.5 In this case, there are two things that vary, so we will need two parameters to capture this. Otherwise we could just do the same thing we did last time, and replicate the pattern, with the parameters in place of the things that change, as shown. Slide 2.3.6 But a better way to capture this pattern is to realize that there are really two things going on. One is the sub-pattern of squaring things. The other is the use of the results of two different squaring operations within the larger pattern. So we could capture each of those patterns within its own procedure, with its own parameters and body. Note that in doing this, we are relying on a property of a combination, namely that a combination involving a named procedure is equivalent to the pattern captured by the procedure, with values substituted for the formal parameters
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有