正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 15.7. 4 So what should eval-lambda do? Remember that eval tal put a tree structure representing a ( i+ssas,"aane.…ps lambda* expression, which it then passes to this procedure together with a pointer to the environment. So first, we need to walk down that tree structure, and grab off the arguments of the expression, the formal parameters if you like. That's what the cadr does. And we want to walk further along the tree and grab the body of the expression, that is the caddr Having I pulled off those two pieces, without evaluation, we then want to glue together some representation of a procedure as a set of arguments, a body and an environment. This should sound familiar, as it sounds a lot like what we have when we do evaluation with our"double bubble"notation in the environment model. There we have a similar representation for a procedure: formal parameters, body and environment Ma ke-compound is doing a similar thing here Note that there is no call to eval inside eval-lambda. none of these expressions is being evaluated; we are simply manipulating tree structure we are assuming that the body contains exactly one expression. That is a design choice that we could change 3 Also notice that we have made a decision about the body of a procedure. by using the caddr of the expressi Slide 15.7.5 3mhd”) So adding the new form to create pi ocedure objects is pretty I straightforward. We will have to eventually work out what make-compound does but that is basically an n←p abstraction issue Now the question is: how do I change app ly? Remember that app ly is going to evaluate the first subexpression of a tree structure, get out the appropriate procedure, evaluate all the arguments getting back a list of those values, and then we want to apply that procedure to that list In the case of primitive things, inherited things from Scheme like +, etc, apply was just a matter of doing the basic operation. Now, we need to extend apply In this case, when we go to apply an operator to a list of operands, we will need to check for type. Is it a primitive operation? If yes, we just apply the underlying Scheme procedure as before. Is it a compound operator, that is, are evaluating an expression in which we are using a procedure that we created in our evaluator using lambda*? In this case we need to implement the idea of the environment model Thus, we get the body of the operator, that is, we pull out the piece that we glued together when we made the procedure object. Now we want to evaluate that body. But we are going to evaluate that with respect to an environment, created by taking6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 15.7.4 So what should eval-lambda do? Remember that eval takes as input a tree structure representing a lambda* expression, which it then passes to this procedure, together with a pointer to the environment. So first, we need to walk down that tree structure, and grab off the arguments of the expression, the formal parameters if you like. That's what the cadr does. And we want to walk further along the tree and grab the body of the expression, that is the caddr. Having pulled off those two pieces, without evaluation, we then want to glue together some representation of a procedure as a set of arguments, a body, and an environment. This should sound familiar, as it sounds a lot like what we have when we do evaluation with our "double bubble" notation in the environment model. There we have a similar representation for a procedure: formal parameters, body and environment. Make-compound is doing a similar thing here. Note that there is no call to eval inside eval-lambda. None of these expressions is being evaluated; we are simply manipulating tree structure. Also notice that we have made a decision about the body of a procedure. By using the caddr of the expression, we are assuming that the body contains exactly one expression. That is a design choice that we could change. Slide 15.7.5 So adding the new form to create procedure objects is pretty straightforward. We will have to eventually work out what make-compound does, but that is basically an abstraction issue. Now the question is: how do I change apply? Remember that apply is going to evaluate the first subexpression of a tree structure, get out the appropriate procedure, evaluate all the arguments getting back a list of those values, and then we want to apply that procedure to that list. In the case of primitive things, inherited things from Scheme like +, * etc., apply was just a matter of doing the basic operation. Now, we need to extend apply. Slide 15.7.6 In this case, when we go to apply an operator to a list of operands, we will need to check for type. Is it a primitive operation? If yes, we just apply the underlying Scheme procedure as before. Is it a compound operator, that is, are we evaluating an expression in which we are using a procedure that we created in our evaluator using lambda*? In this case we need to implement the idea of the environment model. Thus, we get the body of the operator, that is, we pull out the piece that we glued together when we made the procedure object. Now we want to evaluate that body. But we are going to evaluate that with respect to an environment, created by taking
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有