正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.5.7 So how could we use this idea in our context? We could create How do we use this new lazy evaluation? a new data abstraction, called a stream. Here is one version of Our users could implement a stream abstraction this abstraction It has a constructor. cons-stream and 1 ambda《n8q) two selectors. stream-car and stream -cdr. You (cond ((oq? msg ' strean-car) x) (q?m细g'吨tm-cdx)望) can see by their names, that we expect them to behave a lot like unknown stream nss 892))) ists, with one exception. The exception is that we want the can-car s)(s 'stream-o second part of the stream to be lazy, or better yet memoized and oR eam-cdr g)(8’ strean= Here we have represented this in a message passing system, we( oscam iue rte an u anr) azy. This means that when I make a stream, I want to defer getting the value of the second part of the stream until I am actually required tream-odr cdr) could also have done this using cons directly, although we have to be careful about ensuring that cons does not force the evaluation of the cdr part of the pairing until asked to by some other primitive operation How do we use this new lazy evaluation? Slide 17.5.8 Our users could implement a stream abstraction a key change is that now I have a way of gluing together a (define (cons-strean x(y lazy-memo sequence of values in which only the first value is explicitly eond《《eq? evaluated when i do the construction of the data object the rerror "unknown tran mg"meg) )))second part of this structure is lazy: it is a promise to get the (strean-car s) value when asked for, but I don 't do the work of computing the stream-ear)) (stream-edr stream-cdr)) value at construction time dene(cong-t工eanx(y1azy-抛emo) low, what does this do to our thinking about building sequences of values? (define stream-car car) leine stream-cdr cdr) Slide 17.5.9 Stream object A stream object thus looks a lot like a pair, except that the A pair-like object, except the cdr part is lazy cdr part of the stream is lazy. It is not evaluated until some (not evaluated until needed) procedure actually needs its value, but once we have worked cons-sfream out its value, we keep track of it for later reference. Think about stream-cdr what happens now. For example, if I ask x to have the value of va| ue thunk-mem。 a cons-stream of 99 and(/1 0)what will 46.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.5.7 So how could we use this idea in our context? We could create a new data abstraction, called a stream. Here is one version of this abstraction. It has a constructor, cons-stream and two selectors, stream-car and stream-cdr. You can see by their names, that we expect them to behave a lot like lists, with one exception. The exception is that we want the second part of the stream to be lazy, or better yet memoized and lazy. This means that when I make a stream, I want to defer getting the value of the second part of the stream until I am actually required to. Here we have represented this in a message passing system. We could also have done this using cons directly, although we have to be careful about ensuring that cons does not force the evaluation of the cdr part of the pairing until asked to by some other primitive operation. Slide 17.5.8 A key change is that now I have a way of gluing together a sequence of values in which only the first value is explicitly evaluated when I do the construction of the data object. The second part of this structure is lazy: it is a promise to get the value when asked for, but I don't do the work of computing the value at construction time. Now, what does this do to our thinking about building sequences of values? Slide 17.5.9 A stream object thus looks a lot like a pair, except that the cdr part of the stream is lazy. It is not evaluated until some procedure actually needs its value, but once we have worked out its value, we keep track of it for later reference. Think about what happens now. For example, if I ask x to have the value of a cons-stream of 99 and (/ 1 0) what will happen?
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有