正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 6.5.17 and why is this relevant? Well, suppose I also want to which gives us a clean version of sqrt compute cube roots. Given the separation I have made, this is easy. I just compute a fixed point of an average damp of a (fixed-point different function (lambda (y)(/x y)) On the other hand, if I were to go back to Heron of Alexandria's algorithm for square roots, I would have to change a lot of Compare this to Herons algorithm in textbook-same twined with cod different things in the code, and this leaves lots of opportunity for coding errors Thus, these higher order procedures nicely capture patterns of lambda (y)(/ x (square y))) computation, while suppressing unnecessary detail. This makes 1)) it easier for the programmer to focus on the problem of interest, 6001S without getting lost in the details of the code itself. 6.001 Notes: Section 6.6 Slide 6.6.1 Higher order procedures So we have introduced an interesting new concept in this lecture, the idea of a higher order procedure. Our goal was to takes a procedure as an argument or returns one as a value show how we could capture common patterns in a procedure (define hopl (lambda (f *)(+2(f (+ x 1))) where those patterns might be over numbers, or over procedures (+2( square(+31) or processes themselves. By doing this, we isolate the (+2《 square4) computational pattern or process from the details of the process itself, thus making it easier to create new computational hopl (lambda (*)(*2 z))3) Let's revisit this idea of higher order procedures again, to reinforce the main elements. Remem ber that a higher order procedure is a procedure that either takes procedures as input or produces a procedure as output Here is a simple example. Note that by the rules of evaluation, we know that f must be a procedure that maps umbers to numbers, given how it is used in the body of the lambda. and we also know that we can apply hopl to names for procedures or to pure lambda expressions that return procedures as values. Our substitution model demonstrates that this defines a correct process Slide 6.6.2 Type of hop1 And what is the type of hopl? by looking at where x is used in he body, we know it must be a number, since addition is applied (define hopl (lambda (f x)(+2 (f(+* 1))))to it. We also know that f must be a procedure since it is used the first subexpression of a combination moreover, it must be a number> number), number 2 number procedure of one argument, a number, since addition produces a number. and it must return a number. since its value will be used by addition again. And finally, the whole procedure we know will produce a number as output, since the result of the outermost addition operation is the value of the body6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 6.5.17 ... and why is this relevant? Well, suppose I also want to compute cube roots. Given the separation I have made, this is easy. I just compute a fixed point of an average damp of a different function. On the other hand, if I were to go back to Heron of Alexandria's algorithm for square roots, I would have to change a lot of different things in the code, and this leaves lots of opportunity for coding errors. Thus, these higher order procedures nicely capture patterns of computation, while suppressing unnecessary detail. This makes it easier for the programmer to focus on the problem of interest, without getting lost in the details of the code itself. 6.001 Notes: Section 6.6 Slide 6.6.1 So we have introduced an interesting new concept in this lecture, the idea of a higher order procedure. Our goal was to show how we could capture common patterns in a procedure, where those patterns might be over numbers, or over procedures or processes themselves. By doing this, we isolate the computational pattern or process from the details of the process itself, thus making it easier to create new computational processes. Let's revisit this idea of higher order procedures again, to reinforce the main elements. Remember that a higher order procedure is a procedure that either takes procedures as input, or produces a procedure as output. Here is a simple example. Note that by the rules of evaluation, we know that f must be a procedure that maps numbers to numbers, given how it is used in the body of the lambda. And we also know that we can apply hop1 to names for procedures or to pure lambda expressions that return procedures as values. Our substitution model demonstrates that this defines a correct process. Slide 6.6.2 And what is the type of hop1? By looking at where x is used in the body, we know it must be a number, since addition is applied to it. We also know that f must be a procedure since it is used as the first subexpression of a combination. Moreover, it must be a procedure of one argument, a number, since addition produces a number, and it must return a number, since its value will be used by addition again. And finally, the whole procedure we know will produce a number as output, since the result of the outermost addition operation is the value of the body
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有