6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.5.25 and infinite stream of 1s No matter how many times i ask Result: Infinite Data Structures for the next element of this sequence, I will al ways get a 1 Thus I have a structure that I can conceptualize as representing (streamer (streamer ones))=> 1 an infinite set of things. whenever i ask for some element in this sequence, it will always provide it to me, for any such The nfinite stream of 1sl element ones;111111 Result: Infinite Data Structures Slide 17.5.26 he comparison to Some very interesting behavi his may still seem odd, so let's think ( define oneg(on-t工e品n1oneg)) the standard evaluation model. Let's se that i tried to do stream-car (stream-cdr ones))=>1 ons in place of cons-stream In this cas The nfinite stream of 1'sl I get an error, because cons will evaluate both its arguments ones:111111 before constructing the data structure. Clearly I do not yet have a value for ones, so I can't glue it together with 1. with the lazy evaluation buried inside of streams, I can hold off on getting the value of this variable until I have completed the structure which means the name ones will be bound to something when I go to get its value. Thus lazy evaluation provides the means necessary to enable creation of infinite data structures Slide 17.5.27 Finite list procs turn into infinite stream procs So what does this buy us? Well, this way of thinking about infinite data structures let,'s us think about creating procedures Icon that operate as if the entire data structure were available to us Thus procedures that typically apply to lists can be turned into ar 82) procedures that handle infinite streams. Here is a procedure that (strean-cdr s2)))))) adds together two streams, using the stream data abstraction detine ints eons- stream1(add-st工ea丑 s ones intg》) This should take two infinite streams as input and create an output stream in which the first element is computed by adding together the first elements of the input streams, and is glued onto a promise to add the remaining streams together when demanded Using this idea we can create an infinite data structure of all the integers. We simply cons-stream the first integer, which is 1, onto a promise to add together the stream of ones and the stream of integers Let' s check this out since it seems a bit odd6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.5.25 ... and infinite stream of 1's. No matter how many times I ask for the next element of this sequence, I will always get a 1. Thus I have a structure that I can conceptualize as representing an infinite set of things. Whenever I ask for some element in this sequence, it will always provide it to me, for any such element. Slide 17.5.26 This may still seem odd, so let's think about the comparison to the standard evaluation model. Let's suppose that I tried to do this using cons in place of cons-stream. In this case, I get an error, because cons will evaluate both its arguments before constructing the data structure. Clearly I do not yet have a value for ones, so I can't glue it together with 1. With the lazy evaluation buried inside of streams, I can hold off on getting the value of this variable until I have completed the structure, which means the name ones will be bound to something when I go to get its value. Thus lazy evaluation provides the means necessary to enable creation of infinite data structures. Slide 17.5.27 So what does this buy us? Well, this way of thinking about infinite data structures let's us think about creating procedures that operate as if the entire data structure were available to us. Thus procedures that typically apply to lists can be turned into procedures that handle infinite streams. Here is a procedure that adds together two streams, using the stream data abstraction. This should take two infinite streams as input and create an output stream in which the first element is computed by adding together the first elements of the input streams, and is glued onto a promise to add the remaining streams together when demanded. Using this idea we can create an infinite data structure of all the integers. We simply cons-stream the first integer, which is 1, onto a promise to add together the stream of ones and the stream of integers. Let's check this out, since it seems a bit odd