正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 17.5.10 Stream object If I did this just using a normal cons, I would get an error, (not evaluated until because cons would evaluate each of its arguments before constructing the pair, causing the division by zero to take place stream-cdr In the case of cons-stream, we get a different behavio stream-car Cons-stream will create an object with the value of the thunk-nemo first argument as the first piece(99), but the second part is x (cons-stream 99 (/1 0))) simply stored as a promise to compute a value when needed -car x)=>99 and these two things are glued together. As a consequence, x is (stream-cdr error -divide by zero safely defined, and I get the first part of the stream without problem. It is only when I try to access the second part, using stream-cdr, that the evaluation of the deferred promise will take place, and I will see the error due to division by zero Thus, we see there is a difference between a stream object as a pair and a standard pair, in that the second part of a tream object is not evaluated until required We are now going to build on that idea to see how we can create very different data structures and very different ways of thinking about computation Slide 17.5.1l his may seem like a very straightforward change What I have Decoupling computation from description in essence done is say: here is an alternative way of gluing to Can separate order of events in computer from apparent things together into pairs, specifically gluing together the value of the first thing with a promise to get the value of the second thing. It doesn 't sound like a lot, but in fact it has a fundamental impact on how I can think about computation In particular, I can now decouple the computation of values from the description of those values, or said another way, I can separate the order of events that occur inside of the computer from the apparent order of events as captured in the procedure description. Let's look at an example Decoupling computation from description Slide 17.5.12 Can separate order of events in computer from appare Suppose I want to find the value of the 100th prime. Here is the standard way of doing that, using lists Enumerate filter (lambda (x) (prine? x)) nterval could generate a list of all the integers between (enunerate-interval 1 100000000)) I and 100,000,000. I could then filter that list, using a predicate that checks for primes( the details of the predicate are not important here). Given that new list of primes, I could then walk down the list to get the 100th element Notice what has happened here. I first had to generate a list sa integers I will have to check before I find my answer. Thus/ 100,000,000 elements long, because I am not certain how man have done a lot of computation and i have chewed up a lot of memory creating a huge data structure. Filter then runs down that list and generates a new list, not quite large list and involving a lot of inally list-ref just walks down this new list, finds the 100th element, keeps it, and throws everything else away! Using standard methods, I have to do all of the computation to get the value of an argument, before I can move on to the next stage of the computation. Thus, I have to generate an entire data structure before I can move on to6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 17.5.10 If I did this just using a normal cons, I would get an error, because cons would evaluate each of its arguments before constructing the pair, causing the division by zero to take place. In the case of cons-stream, we get a different behavior. Cons-stream will create an object with the value of the first argument as the first piece (99), but the second part is simply stored as a promise to compute a value when needed, and these two things are glued together. As a consequence, x is safely defined, and I get the first part of the stream without problem. It is only when I try to access the second part, using stream-cdr, that the evaluation of the deferred promise will take place, and I will see the error due to division by zero. Thus, we see there is a difference between a stream object as a pair and a standard pair, in that the second part of a stream object is not evaluated until required. We are now going to build on that idea to see how we can create very different data structures and very different ways of thinking about computation. Slide 17.5.11 This may seem like a very straightforward change. What I have in essence done is say: here is an alternative way of gluing to things together into pairs, specifically gluing together the value of the first thing with a promise to get the value of the second thing. It doesn't sound like a lot, but in fact it has a fundamental impact on how I can think about computation. In particular, I can now decouple the computation of values from the description of those values, or said another way, I can separate the order of events that occur inside of the computer from the apparent order of events as captured in the procedure description. Let's look at an example. Slide 17.5.12 Suppose I want to find the value of the 100th prime. Here is the standard way of doing that, using lists. Enumerate￾interval could generate a list of all the integers between 1 and 100,000,000. I could then filter that list, using a predicate that checks for primes (the details of the predicate are not important here). Given that new list of primes, I could then walk down the list to get the 100th element. Notice what has happened here. I first had to generate a list 100,000,000 elements long, because I am not certain how many integers I will have to check before I find my answer. Thus I have done a lot of computation and I have chewed up a lot of memory creating a huge data structure. Filter then runs down that list and generates a new list, not quite as long as the original, but still a very large list, and involving a lot of computation. Finally, list-ref just walks down this new list, finds the 100th element, keeps it, and throws everything else away! Using standard methods, I have to do all of the computation to get the value of an argument, before I can move on to the next stage of the computation. Thus, I have to generate an entire data structure before I can move on to
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有