正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.2.13 To actually implement this, we can just build this as a"tagged"Representing Thunks structure: a list with the label of a thunk, plus a pointer to the Abstractly-a thunk is a promise"to return a value when tree structure of the expression, plus a pointer to the later needed (forced environment Thunks-delay-it and force-it Slide 17.2.14 (define (delay-it exp env)(list thunk exp errv)) With that in mind, we can now implement this idea. Delay define (thunk? ob])(tagged-list? ob] *thunk)) just constructs this tagged list. Remember that since exp (define (thunk-ermv thunk) (cadd thunk) just passed in as list structure from the parser, it is just glued together into new list structure, without evaluation. As with any data abstraction, we have a predicate for identifying a thunk and selectors for getting out the parts of the thunk, the expression and the environment in which to evaluate the expressIon. Slide 17.2.15 Thunks-delay-it and force-it The corresponding piece is that when given a thunk, we can (define (delay-it exp env) (list force it to be evaluated. Here. we can be careful. We will first (thunk? ob])(tagged-list check to see if the object is a delayed thing. If it is not a thunk define (thunk-exp thunk) (cadr ti (define (thunk-errv thunk)(caddo thu then we know that this is already reduced to a value, and we simply pass it along. This is exactly why we put the label on the(d tane (trunk?t b3) object in the first place, allowing us to distinguish between tree (actual-val ue (thunk-exp obj) (thunk-ermv obj))) structure that is part of the data structure being manipulated and (el se ob]))) tree structure that represents something waiting to be evaluated On the other hand, if this is a thunk, then we extract the parts of the thunk(expression and environment), and then we do the actual evaluation by applying actual-value (which we know will force the evaluation of this expression ) Notice as a consequence that if ob] on first evaluation reduces to another delayed thing, actual-value will continue to force it, until a value is returned not a delayed6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.2.13 To actually implement this, we can just build this as a "tagged" structure: a list with the label of a thunk, plus a pointer to the tree structure of the expression, plus a pointer to the environment. Slide 17.2.14 With that in mind, we can now implement this idea. Delay just constructs this tagged list. Remember that since exp is just passed in as list structure from the parser, it is just glued together into new list structure, without evaluation. As with any data abstraction, we have a predicate for identifying a thunk, and selectors for getting out the parts of the thunk, the expression and the environment in which to evaluate the expression. Slide 17.2.15 The corresponding piece is that when given a thunk, we can force it to be evaluated. Here, we can be careful. We will first check to see if the object is a delayed thing. If it is not a thunk, then we know that this is already reduced to a value, and we simply pass it along. This is exactly why we put the label on the object in the first place, allowing us to distinguish between tree structure that is part of the data structure being manipulated and tree structure that represents something waiting to be evaluated. On the other hand, if this is a thunk, then we extract the parts of the thunk (expression and environment), and then we do the actual evaluation by applying actual-value (which we know will force the evaluation of this expression). Notice as a consequence that if obj on first evaluation reduces to another delayed thing, actual-value will continue to force it, until a value is returned not a delayed object
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有