正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.5.15 To see how lazy evaluation gives us this behavior, let's look in a Some details on stream procedures little more detail at this method. here is a standard stream define (stream-filter pred str) procedure, which looks exactly like our filter procedure (if (pred (stream-car str) (cons-stream (stream-car str for lists, the only difference is that we use stream abstractions (stream-filter pred in place of pair abstractions. But how does the lazy evaluation (stream-cdr str))) buried inside of cons-stream allow this procedure that (stream-filter pred streamed str)))) looks just like a normal list procedure to have this different behavior, decoupling the order of evaluation within the machine from the apparent order described by the procedure? Some details on stream procedures Slide 17.5.16 eam-fil (define (stream-filter pred str) deed, a standard question might be "Why (if (pred (strea r str) end up generating all of the elements of the at once?" cQn七工专西叫( tream-car七 The answer is here. When we apply this procedure to a stream, stream-filter pred (stream-cdr str)) it will recursively test each element in the stream, until it finds ( stream-filter pred one that satisfies the predicate. At that stage, note what streamed str)))) happens. We generate a stream with that element as the first element, and with a lazy(or delayed) promise to filter the rest of the stream when needed. Thus, we generate the first element of the new stream, and a lazy promise, not the entire stream Slide 17.5.17 ling order of evaluatie Let's check it out on our simple example. Suppose we filter the (atrean-filter prine> (atria 1 stream of integers from 1 to 100,000,000, using the predicate prime?. Let's follow this computation, noting how lazy evaluation controls the order of evaluation of the parts of the data structure coupling order of evaluation Slide 17.5.18 an-filter prime? (str-in 1 100000000)) Since stream-filter is just a standard procedure, we nents. Prime? will simply point to some procedure. (Stream-interval 1 100000000) needs to be evaluated, but we know that stream-interval is defined in terms of cons stream. So this returns one of these stream objects, which has the value of the first element, 1, already available, but simply has a promise(shown within that squiggly line )to get ne value of the next arg t. which is the stream interval from 2 to 100,000,000. At this stage, all that has been explicitly6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.5.15 To see how lazy evaluation gives us this behavior, let's look in a little more detail at this method. Here is a standard stream procedure, which looks exactly like our filter procedure for lists, the only difference is that we use stream abstractions in place of pair abstractions. But how does the lazy evaluation buried inside of cons-stream allow this procedure that looks just like a normal list procedure to have this different behavior, decoupling the order of evaluation within the machine from the apparent order described by the procedure? Slide 17.5.16 Indeed, a standard question might be "Why doesn't stream-filter end up generating all of the elements of the stream at once?" The answer is here. When we apply this procedure to a stream, it will recursively test each element in the stream, until it finds one that satisfies the predicate. At that stage, note what happens. We generate a stream with that element as the first element, and with a lazy (or delayed) promise to filter the rest of the stream when needed. Thus, we generate the first element of the new stream, and a lazy promise, not the entire stream. Slide 17.5.17 Let's check it out on our simple example. Suppose we filter the stream of integers from 1 to 100,000,000, using the predicate prime?. Let's follow this computation, noting how lazy evaluation controls the order of evaluation of the parts of the data structure. Slide 17.5.18 Since stream-filter is just a standard procedure, we need to get the values of its arguments. Prime? will simply point to some procedure. (Stream-interval 1 100000000) needs to be evaluated, but we know that stream-interval is defined in terms of cons￾stream. So this returns one of these stream objects, which has the value of the first element, 1, already available, but simply has a promise (shown within that squiggly line) to get the value of the next argument, which is the stream interval from 2 to 100,000,000. At this stage, all that has been explicitly
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有