当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

《Structure and Interpretation of Computer Programs》lecture 2 webhand

资源类别:文库,文档格式:PDF,文档页数:15,文件大小:1.5MB,团购合买
In the last lecture, we began looking at the programming This lecture nguage Scheme. with the intent of learning how that anguage would provide a basis for describing procedures and Adding procedures and procedural abstractions
点击下载完整版文档(PDF)

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 2.1 Slide 2.l.1 In the last lecture, we began looking at the programming This lecture nguage Scheme. with the intent of learning how that anguage would provide a basis for describing procedures and Adding procedures and procedural abstractions processes, and thus for understanding computational metaphors Using procedures to capture processes for controlling complex processes. In this lecture, we look at how to create procedural abstractions in our language, and how to use those abstractions to describe and capture computational Language elements -abstractions Well--we' ve got primitives (numbers and built in procedures) weve got means of combination(ways of creating comple expressions); and we've got our first means of abstraction(a way of giving a name to something ). But we are still stuck just writing out arithmetic expressions, as the only procedures we have are the built in ones We need a need another kind of abstraction--we need a way of capturing particular processes n our own procedures, and that's what we turn to next 1m003 6 001 SICP Slide 2.1.3 In Scheme, we have a particular expression for capturing a procedure It's called a lambda expression. It has the form Language elements--abstractions shown, an open parenthesis, the keyword lambda, followed by Need to capture ways of doing things-use some number of symbols enclosed within parentheses, followed by one or more legal expressions, followed by a close (lambda(x)(xx))

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 2.1 Slide 2.1.1 In the last lecture, we began looking at the programming language, Scheme, with the intent of learning how that language would provide a basis for describing procedures and processes, and thus for understanding computational metaphors for controlling complex processes. In this lecture, we look at how to create procedural abstractions in our language, and how to use those abstractions to describe and capture computational processes. Slide 2.1.2 Well -- we've got primitives (numbers and built in procedures); we've got means of combination (ways of creating complex expressions); and we've got our first means of abstraction (a way of giving a name to something). But we are still stuck just writing out arithmetic expressions, as the only procedures we have are the built in ones. We need a way to capture our own processes in our own procedures. So we need another kind of abstraction -- we need a way of capturing particular processes in our own procedures, and that's what we turn to next. Slide 2.1.3 In Scheme, we have a particular expression for capturing a procedure. It's called a lambda expression. It has the form shown, an open parenthesis, the keyword lambda, followed by some number of symbols enclosed within parentheses, followed by one or more legal expressions, followed by a close parenthesis

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2. 1. 4 The keyword lambda identifies this expression as a particular Language elements -abstractions special form. The set of symbols immediately following the lambda are called the formal parameters of the lambda, in this Need to capture ways of doing things-use case, there is just one parameter, x r parameters mbda(x)( xx)) 1m003 6 001 SICP Slide 2. 1.5 The subsequent expression we refer to as the body of the procedure. This is the particular pattern we are going to use to Language elements--abstractions capture a process Need to capture ways of doing things-use procedures r parameters lambda(x)(x x)h 4103 Slide 2.1.6 Language elements --abstractions The way to think about this lambda expression is that it is going to capture a common pattern of computation in a procedure--it Need to capture ways of doing th will actually build a procedure for parameters The way to read the expression is: To process (lambda(x)( xx)h- 410a3 6 001 SICP Slide 2.1.7 somethin Language elements--abstractions 6001S

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.1.4 The keyword lambda identifies this expression as a particular special form. The set of symbols immediately following the lambda are called the formal parameters of the lambda, in this case, there is just one parameter, x. Slide 2.1.5 The subsequent expression we refer to as the body of the procedure. This is the particular pattern we are going to use to capture a process. Slide 2.1.6 The way to think about this lambda expression is that it is going to capture a common pattern of computation in a procedure -- it will actually build a procedure for us. The way to read the expression is: To process ... Slide 2.1.7 ... something

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology multiply it by itself, and return that value Language elements -abstractions Need to capture ways of doing things-use parameters (lambda(x)( xx)l To process something multiply it by itself 1m003 6 001 SICP Slide 2.1.9 So in fact this particular lambda expression captures the process of"squaring". It says, if you give me a value for x I will return Language elements--abstractions the value of multiplying that value by itself. In a second we will Need to capture ways of doing things-use see how this happens procedures r parameters Notice that lambda expressions must be special forms. The (lambda(x)( x x)h-body normal rules for evaluating a combination do not apply here To process something multiply it by itself Instead, the value returned by evaluating a lambda expression is the actual procedure that it captures. Contained within that procedure will be a set of formal parameters, and a body that Special form-creates a procedure and returns it captures the common pattern of the process, as a function of 410 60153C those formal parameters Slide 2.1.10 Language elements--abstractions Now,where can we use such a procedure? Basically anywhere in our earlier expressions that we could use a built-in Use this anywhere you would use a procedure procedure, which for now means as the first element in a ( lambda(x×)(xx)5) combination For example, here is a compound expression, with two subexpressions. What is the value or meaning associated with it? The value of the first subexpression we just saw was a procedure. The value of the second subexpression is just the number 5. Now we have something similar to our earlier cases 410200 6 001 SICP ions a procedure applied to a value. The only difference is that her we have a procedure we built, rather than a pre-existing one We need to specify how such a procedure is applied to a set of arguments

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.1.8 ... multiply it by itself, and return that value. Slide 2.1.9 So in fact this particular lambda expression captures the process of "squaring". It says, if you give me a value for x I will return the value of multiplying that value by itself. In a second we will see how this happens. Notice that lambda expressions must be special forms. The normal rules for evaluating a combination do not apply here. Instead, the value returned by evaluating a lambda expression is the actual procedure that it captures. Contained within that procedure will be a set of formal parameters, and a body that captures the common pattern of the process, as a function of those formal parameters. Slide 2.1.10 Now, where can we use such a procedure? Basically anywhere in our earlier expressions that we could use a built-in procedure, which for now means as the first element in a combination. For example, here is a compound expression, with two subexpressions. What is the value or meaning associated with it? The value of the first subexpression we just saw was a procedure. The value of the second subexpression is just the number 5. Now we have something similar to our earlier cases, a procedure applied to a value. The only difference is that here we have a procedure we built, rather than a pre-existing one. We need to specify how such a procedure is applied to a set of arguments

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.1.11 So here is a summary of our earlier rules for evaluating expressions. The only change is to amplify what it means to Scheme basics apply a procedure to a set of arguments. When the procedure was a built-in arithmetic operator, we just did the obvious thing. 2. If amame retur value associated with name in enviroment. Now if the procedure is something built by evaluating a lambda|:出.-如 expression, we have a new rule. We take the body of the Evaluate all of the subexpressions of combina any order) b, apply the operator to the values of the operands (arguments)and procedure, substitute the value of the argument in place of the corresponding formal parameter, and then use the same rules to Rules for application evaluate the resulting expression dy of the procedure with each formal placed by the corresponding actual argument value. 6001 sICP Slide 2.1.12 Language elements --abstractions So let's go back to our example. Our rule says to replace the value of the second expression, 5, everywhere in the body that Use this anywhere you would use a procedure we see the formal parameter, x. This then reduces the ((lambda(x)(*xx))5 application of the lambda expression to a simpler expression 6001 SICP Slide 2.1.13 This reduces the application of a procedure to this simpler Language elements --abstractions expression, and we can apply our rules again. The symbol* is just a name for the built-in multiplication operation, and 5 is Use this anywhere you would use a procedure just self-evaluating, so this all reduces to ((lambda(x)(xx))5) (·55) 41∞03 60015e Slide 2.1.14 Language elements--abstractions Thus we see that our rules now cover the evaluation of Use this anywhere you would use a procedure compound expressions that include the application of procedures created by lambda expressions. In particular, the rules tell us to substitute into the body of a procedure for the formal parameters, reducing to a new expression, and then apply the same set of rules all over again, until we reach a final answer

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.1.11 So here is a summary of our earlier rules for evaluating expressions. The only change is to amplify what it means to apply a procedure to a set of arguments. When the procedure was a built-in arithmetic operator, we just did the obvious thing. Now if the procedure is something built by evaluating a lambda expression, we have a new rule. We take the body of the procedure, substitute the value of the argument in place of the corresponding formal parameter, and then use the same rules to evaluate the resulting expression. Slide 2.1.12 So let's go back to our example. Our rule says to replace the value of the second expression, 5, everywhere in the body that we see the formal parameter, x. This then reduces the application of the lambda expression to a simpler expression ... Slide 2.1.13 This reduces the application of a procedure to this simpler expression, and we can apply our rules again. The symbol * is just a name for the built-in multiplication operation, and 5 is just self-evaluating, so this all reduces to ... Slide 2.1.14 ... 25. Thus we see that our rules now cover the evaluation of compound expressions that include the application of procedures created by lambda expressions. In particular, the rules tell us to substitute into the body of a procedure for the formal parameters, reducing to a new expression, and then apply the same set of rules all over again, until we reach a final answer

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.1.15 Thus, lambda gives us the ability to capture procedure abstractions-- patterns of computation in a single procedure Language elements --abstractions But we don't want to have to write out lambda expressions everywhere we need to use this procedure Use this an you would use a procedure Instead, we can combine this procedural abstraction with our ((lambda(x)(xx))5) naming abstraction--that is, we can use a define expression to (55) give a name to a procedure. In this case, the name square will be paired with the value of the lambda expression, or quite literally with the actual procedure created by evaluating that (define lambda(x)(xx))) (square lambda Then we can use the name square wherever we want the 10003 6001 sICP procedure, since its value is the actual procedure. If you follow the rules of evaluation for the last expression, you will see that we get a procedure applied to a number, and the substitution of the argument into the body of the procedure reduces to a simpler expression, just as we saw earlier 6.001 Notes: Section 2.2 Slide 2.2.1 Lambda: making new procedures The second kind of special form we saw was a lambda expression, and lambda we said was our way of capturing rocesses inside procedures. Lambda is Greek for "procedure (lambda (x)(*x*)) maker", and is our way of saying" Take this pattern, and capture in a way that will let me reuse it multiple times". In our two world view, if we type this expression into the computer and ask it to be evaluated, the computer uses the key word lambda to determine that this is a particular special form. It will then use the rule designed for this type of expression 1 001 SICP Lambda: making new procedures Slide 2.2.2 It is important to stress that the actual value created inside the machine is some representation for the procedure itself. This (lambda (at) ( c x )) representation includes information about the parameters of the procedure, and the body of the procedure, that is, what pattern of computation is being captured, and what are the variables to be replaced in that pattern when we want to use this procedure But it is the actual procedure representation that is associated a the value of the expression

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.1.15 Thus, lambda gives us the ability to capture procedure abstractions -- patterns of computation in a single procedure. But we don't want to have to write out lambda expressions everywhere we need to use this procedure. Instead, we can combine this procedural abstraction with our naming abstraction -- that is, we can use a define expression to give a name to a procedure. In this case, the name square will be paired with the value of the lambda expression, or quite literally with the actual procedure created by evaluating that lambda. Then we can use the name square wherever we want the procedure, since its value is the actual procedure. If you follow the rules of evaluation for the last expression, you will see that we get a procedure applied to a number, and the substitution of the argument into the body of the procedure reduces to a simpler expression, just as we saw earlier. 6.001 Notes: Section 2.2 Slide 2.2.1 The second kind of special form we saw was a lambda expression, and lambda we said was our way of capturing processes inside procedures. Lambda is Greek for "procedure maker", and is our way of saying "Take this pattern, and capture in a way that will let me reuse it multiple times". In our two world view, if we type this expression into the computer and ask it to be evaluated, the computer uses the key word lambda to determine that this is a particular special form. It will then use the rule designed for this type of expression. Slide 2.2.2 It is important to stress that the actual value created inside the machine is some representation for the procedure itself. This representation includes information about the parameters of the procedure, and the body of the procedure, that is, what pattern of computation is being captured, and what are the variables to be replaced in that pattern when we want to use this procedure. But it is the actual procedure representation that is associated as the value of the expression

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.2.3 And what gets returned? Basically some representation like Lambda: making new procedures this, that identifies that this is a procedure created by evaluating printed representation of value a lambda, and an indication of where it lives inside the machine (lambda (ax)(* xx) 排[ compound-procedure (i.e. how to get at the parameters and body of the procedure). It is important to stress that evaluating a lambda creates an actual procedure object inside the execution world, and that value is actually returned as the value of the expression argument 6001 sICP Interaction of define and lambda Slide 2.2.4 (lambda (x)(** x)) Now let's look at the interaction between creating procedures == #[compound-procedure 91 and giving them names. First of all, I can create a lambda expression, and the value returned by that evaluation is the procedure itself. Note that the procedure is not executed or run here is no numeric value returned here as a square, rather the procedure itself is returned as the val 6001 SICP Slide 2.2.5 Interaction of define and lambda If I actually want to use that procedure I need to refer to it, and the easiest way to do this is to give it a name. So I can define square to be the value returned by that lambda expression a> undef which we just saw is a procedure. This then creates a pairing of the name square with the procedure that captures the pattern of multiplying a value by itself. 60015e Interaction of define and lambda Slide 2.2.6 1.(1 ambda《x)(*xx)) laving done that, I can now write any expression that uses the ==>#compound-procedure 91 name square anyplace that the associated lambda expression 2. (define square (lambda (*)(* xx))) would have been legitimate Note that the evaluation rules nicely cover this This expression 3.(square 4) just get the value sul expressions. Square is a name, so we look up its value, getting back the procedure. This can then be applied to the value of the next sub-expression, using the standard rules for procedure application, namely substitute 4 for x everywhere in the body of the procedure, then evaluate that new expression using the same

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.2.3 And what gets returned? Basically some representation like this, that identifies that this is a procedure created by evaluating a lambda, and an indication of where it lives inside the machine (i.e. how to get at the parameters and body of the procedure). It is important to stress that evaluating a lambda creates an actual procedure object inside the execution world, and that value is actually returned as the value of the expression. Slide 2.2.4 Now let's look at the interaction between creating procedures and giving them names. First of all, I can create a lambda expression, and the value returned by that evaluation is the procedure itself. Note that the procedure is not executed or run: there is no numeric value returned here as a square, rather the procedure itself is returned as the value. Slide 2.2.5 If I actually want to use that procedure I need to refer to it, and the easiest way to do this is to give it a name. So I can define square to be the value returned by that lambda expression, which we just saw is a procedure. This then creates a pairing of the name square with the procedure that captures the pattern of multiplying a value by itself. Slide 2.2.6 Having done that, I can now write any expression that uses the name square anyplace that the associated lambda expression would have been legitimate. Note that the evaluation rules nicely cover this. This expression is a standard combination, so we just get the values of the sub￾expressions. Square is a name, so we look up its value, getting back the procedure. This can then be applied to the value of the next sub-expression, using the standard rules for procedure application, namely substitute 4 for x everywhere in the body of the procedure, then evaluate that new expression using the same rules

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.2.7 Interaction of define and lambda Of course, I could have used the full lambda expression 1.(lambda (*)(*xx) place of the name. In this case, evaluation of the first sub- m> #[compound-procedure 91 expression will create the procedure, which is then applied to 2.(define square (lambda (*)(*x *)) > undef of the second sub-expression 3.(square 4) ==>16 4.((1 ambda(x)(*xx))4)=>1 6 005 sIc Interaction of define and lambda Slide 2.2.8 nbda (x)(*xx Because this action of creating a procedure and giving it a name mm> #[compound-procedure 9] is such a common activity, we have an alternative way of doing 2.(define square (lambda (*)(*x 2))) ==> undef this. This last expression is really the same as the second one, 3.(scu =>16 but is simply written in a more convenient form(which we call 4. dlambda()(xx)24-01=>16"syntactic sugar"meaning that the meaning is the same but the orm is sweeter). It is important to stress that in this last form This is a convenient shorthand(called"syntactic suger") there is a hidden lambda that is evaluated to create the for 2 above- this is a use of lambda! procedure, and then the define is used to give it a name tonne 6 001 SICP Slide 2.2.9 Lambda special form Let's look a little more carefully at this lambda special form syntax (lambda (x y)(/(+ x y) 2) As we have seen, the syntax, as shown at the top, has three pIe 1st operand position: the parameter list There is the key word, lambda, which identifies the type of a list of names (perhaps empty) determines the number of operands required expression. The first operand position of this expression identifies the formal parameters of the procedure. This is a list'2nd op raen po stions the ody (/(+xy)2 (or a sequence of names enclosed in a set of parentheses ). In this case, the procedure takes two arguments, which we will luated when the procedure is applie call x and y. Note that there can be an arbitrary number of semantics of lambda: arguments for a procedure, including zero (in which case we would use o as the parameter list), and that this component determines how many arguments must be passed to the procedure when it is applied The second operand position determines the body of the procedure, that is, the pattern of evaluation to be used This may be any valid Scheme expression. Note that this expression is NOT evaluated when the procedure is created. It is simply stored away as a pattern. It is only evaluated when the procedure is applied in some legal combination That determines the syntax of the lambda. The semantics of this kind of expression is very important, so we place it on a whole separate slide, which follows

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.2.7 Of course, I could have used the full lambda expression in place of the name. In this case, evaluation of the first sub￾expression will create the procedure, which is then applied to the value of the second sub-expression. Slide 2.2.8 Because this action of creating a procedure and giving it a name is such a common activity, we have an alternative way of doing this. This last expression is really the same as the second one, but is simply written in a more convenient form (which we call "syntactic sugar" meaning that the meaning is the same but the form is sweeter). It is important to stress that in this last form, there is a hidden lambda that is evaluated to create the procedure, and then the define is used to give it a name. Slide 2.2.9 Let's look a little more carefully at this lambda special form. As we have seen, the syntax, as shown at the top, has three pieces. There is the key word, lambda, which identifies the type of expression. The first operand position of this expression identifies the formal parameters of the procedure. This is a list (or a sequence of names enclosed in a set of parentheses). In this case, the procedure takes two arguments, which we will call x and y. Note that there can be an arbitrary number of arguments for a procedure, including zero (in which case we would use () as the parameter list), and that this component determines how many arguments must be passed to the procedure when it is applied. The second operand position determines the body of the procedure, that is, the pattern of evaluation to be used. This may be any valid Scheme expression. Note that this expression is NOT evaluated when the procedure is created. It is simply stored away as a pattern. It is ONLY evaluated when the procedure is applied in some legal combination. That determines the syntax of the lambda. The semantics of this kind of expression is very important, so we place it on a whole separate slide, which follows

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.2.10 and i feel like i should be shouting this out! l The semantics says that the value of a lambda expression is a procedure object. It is some internal representation of the THE VALUE OF procedure that includes information about the formal A LAMBDA EXPRESSION parameters and the body, or pattern of computation s A PROCEDURE tonne 6 001 SICP 6.001 Notes: Section 2.3 Slide 2.3.1 We have now seen most of the basic elements of scheme. We Using procedures to describe processes will continue to add a few more special forms, and introduce low can we use the idea of a procedure to capture a some additional built in procedures, but we now have enough elements of the language to start reasoning about processes, and especially to use procedures to describe computational processes So let's look at some examples of describing processes in procedures What does a procedure describe? Slide 2.3.2 First, what does a procedure describe? (33) One useful way of thinking about this is as a means of (2525) generalizing a common pattern of operations. For example foobar foobar) consider the three expressions shown here. The first two are straightforward. The third is a bit more general, since foobar is presumably a name for some numerical value However, each of these is basically just a specific instantiation of a process: the process of multiplying a value by itself, or the 6 001 SICP

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.2.10 ... and I feel like I should be shouting this out!! The semantics says that the value of a lambda expression is a procedure object. It is some internal representation of the procedure that includes information about the formal parameters and the body, or pattern of computation. 6.001 Notes: Section 2.3 Slide 2.3.1 We have now seen most of the basic elements of Scheme. We will continue to add a few more special forms, and introduce some additional built in procedures, but we now have enough elements of the language to start reasoning about processes, and especially to use procedures to describe computational processes. So let's look at some examples of describing processes in procedures. Slide 2.3.2 First, what does a procedure describe? One useful way of thinking about this is as a means of generalizing a common pattern of operations. For example, consider the three expressions shown here. The first two are straightforward. The third is a bit more general, since foobar is presumably a name for some numerical value. However, each of these is basically just a specific instantiation of a process: the process of multiplying a value by itself, or the process of "squaring

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 parameters

6.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

6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 2.3.7 Why is this a better way of capturing the pattern? Why? The primary reason is that by breaking the pattern up into Breaking computation into modules that capture haller modules, we isolate pieces of the computation in eparate abstractions. and these modules can then be reused by Enables reuse in other places(e.g. square) Isolates details of o other computations. In particular, the idea of square lkely to be of value elsewhere, so it makes sense to capture that May be many ways to divide up in its own procedure, and then use within this larger one As well, by doing this, we create code that is easier to read, as we use a simple name to capture the idea of s quare, and suppress the unnecessary details. By doing so, we isolate out arms 6001 sICP the use of a procedure from the details of its actual implementation, a trick to which we will return later in the rm Of course, there may be many different ways of modularizing a computational pattern Slide 2.3.8 Here is a finer scale modularization of the pattern into Enables reuse in other places(e.g. square oates details of computation within a procedure of a procedure from its details May be many ways to divide up (define aquare (lambda (x) (. xx))) de三 ine sun- quarc (lambda (x y) (+ (square x)(square y)))) define pythagoras (1 anda (y x)(sqrt《阻一q以xe8Yx))》) 262003 6001 SICP Slide 2.3.9 Abstracting the process Now, let's step away from the specifics of this example, and ages in capturing common patterns of computation talk about the process we just used Identify modules or stages of process In essence. we did several things we identified modules or Capture each module within a procedural abstraction parts of the computational process, which we could usefully Construct a procedure to control the interactions between the module isolate, we then captured each of those within their own Repeat the process within each module as necessary procedural abstraction; and finally we created a procedure to control the interactions between the individual modules. of course, we could apply this process within each of the modules in a recursive fashion Our goal now is to see how we can use this general approach to 4 6001 SICP capture computational processes in procedures 6.001 Notes: Section 2. 4

6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 2.3.7 Why is this a better way of capturing the pattern? The primary reason is that by breaking the pattern up into smaller modules, we isolate pieces of the computation in separate abstractions. And these modules can then be reused by other computations. In particular, the idea of square is likely to be of value elsewhere, so it makes sense to capture that in its own procedure, and then use within this larger one. As well, by doing this, we create code that is easier to read, as we use a simple name to capture the idea of square, and suppress the unnecessary details. By doing so, we isolate out the use of a procedure from the details of its actual implementation, a trick to which we will return later in the term. Of course, there may be many different ways of modularizing a computational pattern. Slide 2.3.8 Here is a finer scale modularization of the pattern into procedures. Note how each procedure uses the previous one within its body, using that idea of abstraction to separate the use of a procedure from its details. Slide 2.3.9 Now, let's step away from the specifics of this example, and talk about the process we just used. In essence, we did several things: we identified modules or parts of the computational process, which we could usefully isolate; we then captured each of those within their own procedural abstraction; and finally we created a procedure to control the interactions between the individual modules. Of course, we could apply this process within each of the modules, in a recursive fashion. Our goal now is to see how we can use this general approach to capture computational processes in procedures. 6.001 Notes: Section 2.4

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共15页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有