正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.2.1 Let's deal with the second issue first. What changes should we How can we implement lazy evaluation? make to eval and app ly in order to implement this new (define (l-apply procedure arguments env) i changed (cond ((primiti ve-procedure? procedure) idea of normal order evaluation(sometimes also called lazy pply-primitive-procedure evaluation). Note that it is called lazy evaluation because we (Iist-of-arg-values argunents env))) are only evaluating arguments when required, either because we ((compound-procedure? procedure) need to print them out or because we are down to primitive (procedure-body procedure procedures. So what changes are needed? 1t一E-de1ayd- arga ar e1器e《 error" Unknown proc" procedure))) How can we implement lazy evaluation? Slide 17.2.2 changed Let's start with the application of a compound procedure (cond ((primitive-procedure? procedure) something we have built with a lambda. We know we want to evaluate the body of that procedure in a new environment (list-of-arg-valuesargumentsenv))) ((compound-procedure? procedure) but what should the environment do? Extend- environment should take the set of parameters of the (extend-environrent (procedure ocedure) procedure, but glue together with them, not with the actual values, but with a set of delayed arguments. And what is a rocedure-environment procedure)))) (e1器e《eror" Unknown proc"pr delayed argument? Let's create a structure that let's us hold off on getting the actual value of an argument. That makes if you think of our example. When we want to apply a procedure we want to take the argument expressions and substitute them into the body without evaluation, but as unevaluated tree structure So our change in apply ing a compound procedure is to still evaluate the body of the procedure in a new environment, but rather than binding the procedure's parameters to their actual values, we will bind them to these pecial structures that keep track of the expression to be evaluated when required, plus we will have to keep track of the environment in which that evaluation should take place when required Slide 17.2.3 How can we im plement lazy evaluation? That last point is worth stressing, because it is a change. Prior (define (I-apply procedure arguments changed to this, apply didn t need to know about it's environment spply-prinitive-procedure We simply applied a procedure to a set of argument values Here, because we delay getting the argument values, we need to arguments env))) keep track of the information needed to actually compute the alues when needed. meaning the environment in which the expression should be evaluated Thus we need to pass the environment in as an argument to app l y so that we can pass procedure-env⊥ conment pr。ee (e1器e( error" Unknown proc"pro。 edure))) through to the construction of the delayed evaluation objects6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.2.1 Let's deal with the second issue first. What changes should we make to eval and apply in order to implement this new idea of normal order evaluation (sometimes also called lazy evaluation). Note that it is called lazy evaluation because we are only evaluating arguments when required, either because we need to print them out or because we are down to primitive procedures. So what changes are needed? Slide 17.2.2 Let's start with the application of a compound procedure, something we have built with a lambda. We know we want to evaluate the body of that procedure in a new environment, but what should the environment do? Extend￾environment should take the set of parameters of the procedure, but glue together with them, not with the actual values, but with a set of delayed arguments. And what is a delayed argument? Let’s create a structure that let's us hold off on getting the actual value of an argument. That makes sense if you think of our example. When we want to apply a procedure, we want to take the argument expressions and substitute them into the body without evaluation, but as unevaluated tree structure. So our change in applying a compound procedure is to still evaluate the body of the procedure in a new environment, but rather than binding the procedure's parameters to their actual values, we will bind them to these special structures that keep track of the expression to be evaluated when required, plus we will have to keep track of the environment in which that evaluation should take place, when required. Slide 17.2.3 That last point is worth stressing, because it is a change. Prior to this, apply didn't need to know about it's environment. We simply applied a procedure to a set of argument values. Here, because we delay getting the argument values, we need to keep track of the information needed to actually compute the values when needed, meaning the environment in which the expression should be evaluated. Thus we need to pass the environment in as an argument to apply so that we can pass it through to the construction of the delayed evaluation objects
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有