正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology computed is the first value. Everything else is just sitting around as a promise to do some work later Slide 17.5. 19 Having evaluated the two arguments to stream-filter/Decoupling order of evaluation (strearfilter prine (str-in 1 100000000)1 we can now apply that procedure, i.e. evaluate its body. What does that do? It applies the predicate(the first argument )to the first value of the stream(the second argument ). Since in this case I is not prime, stream-filter will return the prime value of the second clause of its if expression. This is a recursive call to stream-filter with the same predicate but now with the stream-cdr of the second argument. This means we should now force that promis etting stream -interval from 2 to 100,000,000 Decoupling order of evaluation Slide 17.5.20 (streanfilter prine, (str-in 1 1000000001) Here is where a potential confusion can arise. It might seem like evaluating the stream-cdr of this stream should cause all the remaining elements of the stream, everything from 2 to 100.000.000. to be evaluated But remember that 工 airfilter prime stream-interval says to do a cons-stream of the first argument onto a promise to get the rest of the value tream-filter prime So in fact it returns another stream object, with the next element in the sequence and another promise to generate the remaining elements. That is what will be supplied to stream- filter Slide 17.5.2 Decoupling order of evaluation So now stream-filter can evaluate its body testing ith its predicate to see if the first element of this stream is a prime. It is, so it returns a cons-stream of the first element of the input stream, which is a 2, and a promise, and here the promise is to do a stream-filter on the remaining things And notice what this stream-filter Iwtream-filter prim is It is a promise to filter using prime on the stream- cdr of the object we started with Thus we now have two delayed promises. We have a promise to do the filter, and inside of it is a promise to generate the rest of the initial stream. Thus we can see that we will only pull out values from the stream as we need them. If we ask for the next element in this stream, we would then force the evaluation of the stream-filter expression which would force the evaluation of the stream -interval expression By hiding the lazy evaluation within the constructor, then building our procedures on top of that abstraction, we can easily enable the separation of the actual order of computation from the apparent order of computation6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. computed is the first value. Everything else is just sitting around as a promise to do some work later. Slide 17.5.19 Having evaluated the two arguments to stream-filter we can now apply that procedure, i.e. evaluate its body. What does that do? It applies the predicate (the first argument) to the first value of the stream (the second argument). Since in this case 1 is not prime, stream-filter will return the value of the second clause of its if expression. This is a recursive call to stream-filter with the same predicate but now with the stream-cdr of the second argument. This means we should now force that promise, getting stream-interval from 2 to 100,000,000. Slide 17.5.20 Here is where a potential confusion can arise. It might seem like evaluating the stream-cdr of this stream should cause all the remaining elements of the stream, everything from 2 to 100,000,000, to be evaluated. But remember that stream-interval says to do a cons-stream of the first argument onto a promise to get the rest of the values. So in fact it returns another stream object, with the next element in the sequence and another promise to generate the remaining elements. That is what will be supplied to stream￾filter. Slide 17.5.21 So now stream-filter can evaluate its body, testing with its predicate to see if the first element of this stream is a prime. It is, so it returns a cons-stream of the first element of the input stream, which is a 2, and a promise, and here the promise is to do a stream-filter on the remaining things. And notice what this stream-filter is. It is a promise to filter using prime? on the stream￾cdr of the object we started with. Thus we now have two delayed promises. We have a promise to do the filter, and inside of it is a promise to generate the rest of the initial stream. Thus we can see that we will only pull out values from the stream as we need them. If we ask for the next element in this stream, we would then force the evaluation of the stream-filter expression, which would force the evaluation of the stream-interval expression. By hiding the lazy evaluation within the constructor, then building our procedures on top of that abstraction, we can easily enable the separation of the actual order of computation from the apparent order of computation
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有