正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.5.28 Finite list procs turn into infinite stream procs So what does int s or integers look like? Well we know that cond((nu11?s1)’()) the first element will be a 1. be nu11?82)·()) that onto a promise to get the rest of the integers. Okay we know ask for the d element of this strea (add-streans (strean-cdr 81) (strean-cdr 82)))))) (define int en一8team1(add-器 trans ones int)) Slide 17.5.29 Finite list procs turn into infinite stream procs well the second element was a promise, a promise(because of the construction using lazy evaluation) to add together the stream of ones and the stream of ints. To get the second element we now need to evaluate that promise (+(stream-car sl)(stream-car s2)) 礁dd-at工阻器( strean=cdr81) Notice that at this stage, both ones and ints are available,(define inta (strean-edr 82)))))) the first elements are available, add-streams can lance/feeing that is, have been bound to values in the environment. Each is a (oons-stream 1 (add-streans ones ints))) stream with an evaluated first element and a promise. and sine one:: 111111 ints add together the first elements of the two streams, and glue the add-gtreams ones sum together with a promise to add the remaining streams Finite list procs turn into infinite stream procs Slide 17.5.30 (define (add-streams s1 s2) If we were to ask for the third element of ints we would eond((u11?a1)(})) (nu11?s2}·()) then force this new promise. We would evaluate a promise to add together two streams: a promise to get the next element of (+(stream -car sl)(stream-car s2)) ones and a promise to get the next element of ints. In this (strean-cdr 82))))) way, we can walk our way down ints. Whenever we ask for en器8七xeam1(add-器txe确mone8ints)) the next element in the sequence add-streams will 111111 ints 123 tug"on the two input streams to generate the next element in add-streams one add-streams (str-ccr ones) each, add them, and generate a new promise to create the (atr-cdr ints) remainder of the stream6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.5.28 So what does ints or integers look like? Well we know that the first element will be a 1, because we cons-streamed that onto a promise to get the rest of the integers. Okay, suppose we know ask for the second element of this stream... Slide 17.5.29 ... well the second element was a promise, a promise (because of the construction using lazy evaluation) to add together the stream of ones and the stream of ints. To get the second element we now need to evaluate that promise. Notice that at this stage, both ones and ints are available, that is, have been bound to values in the environment. Each is a stream with an evaluated first element and a promise. And since the first elements are available, add-streams can thus add together the first elements of the two streams, and glue the sum together with a promise to add the remaining streams. Slide 17.5.30 If we were to ask for the third element of ints we would then force this new promise. We would evaluate a promise to add together two streams: a promise to get the next element of ones and a promise to get the next element of ints. In this way, we can walk our way down ints. Whenever we ask for the next element in the sequence, add-streams will "tug" on the two input streams to generate the next element in each, add them, and generate a new promise to create the remainder of the stream
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有