正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 15.7 Slide 15.7.1 6. Defining new procedures Want to add new procedures interpreter. We would like to be able to add new procedures. If I. For example, a scheme+ program you think about it, once we add this ability we have all the basic components of an interpreter: we can deal with primitive (define* twice*(lambda*(x*)(plus* x***))) expressions, with special forms, and with applications, and as along as we can create our own procedures, those applications could include arbitrary nesting of expressions How do we add new procedures? We would like to create a name for something we make with the equivalent of a lambda. We want to capture procedures within lambda's and then use that procedure as an application 6. Defining new procedures Slide 15.7.2 Want to add new procedu o do this, we need to add to our evaluator the ability to capture For example, a scheme* program a computation in a procedure. Earlier in the term we saw this (define* twice* (lambda* (x*)(plus* x with Scheme: that a lambda expression could capture a (twice* 4) common pattern of computation. Now we are going to see how to add that ability to an evaluator, by creating something that Add a case for lambdas to eval will deal with lambda-like expressions he value of l ambda* is a compound procedure .Extend apply to handle compound procedures Here is our strategy for accomplishing this. First, we will need plement environment model another dispatch in our evaluator, something that deals explicitly with lambda* expressions. We would like the a0 value of the l ambda* expression to return a compound procedure, however we decide to define it. Second, given the ability to create our own procedures, we will need to extend apply to handle such procedures. Right now app ly is just set up to handle built in things we inherit from Scheme, but we need to have it also handle procedures we create. And then we should go back and finish the environment model, since it will become essential in the application of procedures Slide 15.7.3 m.时:3? Adding the dispatch to eval is straightforward. Well need a - mm new tag checker for lambda* expressions, and a new dispatch clause inside eval to a procedure to handle the …1 wane.tp create of lambda* objects. Notice where we add this oAale-6oepsvd aww biar dispatch clause: it must come before the application case, since 1 ambda大 I form. It should really also come after the checks for the primitive expressions(numbers or symbols) The ordering of where within the special forms it falls is not crucial6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 15.7 Slide 15.7.1 Now we are ready to make our last big extension to our interpreter. We would like to be able to add new procedures. If you think about it, once we add this ability we have all the basic components of an interpreter: we can deal with primitive expressions, with special forms, and with applications, and as along as we can create our own procedures, those applications could include arbitrary nesting of expressions. How do we add new procedures? We would like to create a name for something we make with the equivalent of a lambda. We want to capture procedures within lambda's and then use that procedure as an application. Slide 15.7.2 To do this, we need to add to our evaluator the ability to capture a computation in a procedure. Earlier in the term we saw this with Scheme: that a lambda expression could capture a common pattern of computation. Now we are going to see how to add that ability to an evaluator, by creating something that will deal with lambda-like expressions. Here is our strategy for accomplishing this. First, we will need another dispatch in our evaluator, something that deals explicitly with lambda* expressions. We would like the value of the lambda* expression to return a compound procedure, however we decide to define it. Second, given the ability to create our own procedures, we will need to extend apply to handle such procedures. Right now apply is just set up to handle built in things we inherit from Scheme, but we need to have it also handle procedures we create. And then we should go back and finish the environment model, since it will become essential in the application of procedures. Slide 15.7.3 Adding the dispatch to eval is straightforward. We'll need a new tag checker for lambda* expressions, and a new dispatch clause inside eval to a procedure to handle the create of lambda* objects. Notice where we add this dispatch clause: it must come before the application case, since lambda* is a special form. It should really also come after the checks for the primitive expressions (numbers or symbols). The ordering of where within the special forms it falls is not crucial
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有