Racket week1 Using Streams

Definition

A stream is a word that we use in computer science to mean an infinite sequence of values.

All right, something that can go on for as long as you need. It behaves like somethings infinitely big. Now, one thing about infinite sized things, you can't actually make them.

And the key idea we're going to use is to use a thunk to delay the evaluation of most of the sequence and only generate some prefix of the sequence That some other computation needs.

But it's a powerful concept for dividing labor up in a way that works in a lot of different software systems.

For use

one way is if you're implementing code that need to respond to a whole bunch of user events, mouse clicks, keyboard presses, things like that, we saw earlier in the course we could do that will call backs.

But another way to do it is to think of that as some stream of events.We'll as for each one as we need it, and then we'll compute some result with that thing so far.

And someone else will generate those events as they occur. Have you ever programmed with pipes in the UNIX shell system?

It turns out that the second command, pulls data from the first command as it needs it so it views the first command as a stream. And the first command's output is generating that stream.

There's also a nice connection with electrical engineering and circuits, that if you think of a timed circuit with feedback.

You can think of the different output values it's sending on it's output wires as forming an infinitely long sequence. And then the, the circuits reading those values Can read the ones that they're interested in.

image.png
image.png
(define nats
  (letrec ([f (lambda (x) (cons x (lambda () (f (+ x 1)))))])
      (lambda () (f 1))
  )
)

需要注意的是:(cons x (lambda () (f (+ x 1))),后面的lambda()表示的是next element is a thunk.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。