南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams

Lecture 22-Streams
Lecture 22 - Streams

Streams
Streams

Lazy Evaluation in Scheme
Lazy Evaluation in Scheme

Lazy evaluation In Python,iterators and def ints(first): while True: generators allowed for lazy yield first evaluation first +=1 >>>s ints(1) Can represent large or >>next(s) infinite sequences 1 >>next(s) 2 Scheme doesn't have iterators. (define (ints first) (cons first (ints (first 1))) How about a list? Second argument to cons scm>(ints 1) is always evaluated maximum recursion depth exceeded
scm> (ints 1) maximum recursion depth exceeded Lazy evaluation ● In Python, iterators and generators allowed for lazy evaluation def ints(first): while True: yield first first += 1 (define (ints first) (cons first (ints (+ first 1))) >>> s = ints(1) >>> next(s) 1 >>> next(s) 2 ● Scheme doesn't have iterators. How about a list? Second argument to cons is always evaluated Can represent large or infinite sequences

Streams Instead of iterators, Scheme uses streams (define (ints first) (define (ints first) (cons first (cons-stream first (ints (first 1))) (ints (first 1))) scm>(ints 1) scm>(ints 1) maximum recursion depth exceeded (1.#[promise (not forced)]) Lazy evaluation,just like iterators in Python
Streams Instead of iterators, Scheme uses streams (define (ints first) (cons first (ints (+ first 1))) scm> (ints 1) maximum recursion depth exceeded (define (ints first) (cons-stream first (ints (+ first 1))) scm> (ints 1) (1 . #[promise (not forced)]) Lazy evaluation, just like iterators in Python

Streams scm>(define s (cons-stream 1 (cons-stream 2 nil))) s scm>s Stream:(linked)list whose rest (1.#[promise (not forced)]) is lazily evaluated scm>(car s) 1 O A promise to compute I don't need the rest of this list right now.Can you Sure,I promise to compute it compute it for me later? right after I finish watching Stranger Things. scm>
scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> s (1 . #[promise (not forced)]) scm> (car s) 1 Streams ● Stream: (linked) list whose rest is lazily evaluated ○ A promise to compute I don't need the rest of this list right now. Can you compute it for me later? scm> Sure, I promise to compute it right after I finish watching Stranger Things

Streams scm>(define s (cons-stream 1 (cons-stream 2 nil))) s scm>s cdr returns the rest of a list (1.#[promise (not forced)]) O For normal lists,the rest is scm>(cdr s) #[promise (not forced)] another list O The rest of a stream is a promise to compute the list I want the cdr of the list now. Just one more episode.I'm almost done with season 3. scm>
scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> s (1 . #[promise (not forced)]) scm> (cdr s) #[promise (not forced)] Streams ● cdr returns the rest of a list ○ For normal lists, the rest is another list ○ The rest of a stream is a promise to compute the list I want the cdr of the list now. scm> Just one more episode. I'm almost done with season 3

Streams scm>(define s (cons-stream 1 (cons-stream 2 nil))) s scm>s (1.#[promise (not forced)]) cdr-stream forces Scheme to scm>(cdr-stream s) compute the rest (2.#[promise (not forced)]) scm>(cdr-stream (cdr-stream s)) () cdr-stream! Fine!Here's your stupid list. scm>
scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> s (1 . #[promise (not forced)]) scm> (cdr-stream s) (2 . #[promise (not forced)]) scm> (cdr-stream (cdr-stream s)) () Streams ● cdr-stream forces Scheme to compute the rest cdr-stream ! scm> Fine! Here's your stupid list

Streams Remember,a stream is just a regular Scheme pair whose second element is a promise scm>(define s (cons-stream 1 (cons-stream 2 nil))) scm>(cdr s) #[promise (not forced)] scm>(cdr-stream s) (2 #[promise (not forced)]) scm>(cdr-stream (cdr-stream s)) IF A PROMISE CONTAINS () AN EMPTY LIST Promise: Promise: (cons-stream 2 nil) nil 1 2 IS IT AN EMPTY PROMISE?
scm> (cdr-stream s) (2 . #[promise (not forced)]) scm> (define s (cons-stream 1 (cons-stream 2 nil))) s scm> (cdr s) #[promise (not forced)] Streams Remember, a stream is just a regular Scheme pair whose second element is a promise 1 Promise: (cons-stream 2 nil) scm> (cdr-stream (cdr-stream s)) () 2 Promise: nil

Streams ● Streams O Promises O Examples O Streams Exam Problems
Streams ● Streams ○ Promises ○ Examples ○ Streams Exam Problems
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)24-SQL-II.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)25-Conclusion, and Final Exam Review.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)01 绪论 Introduction(主讲:曹洋).pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)02 傅里叶分析与卷积 Fourier Analysis and Convolution.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)03 数字图像处理基础 Basics of Digital Image Processing.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)04 图像模型 Basics of Image.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)05 空域滤波 Spatial Filtering.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)06 小波变换 Wavelet Analysis.pptx
- CodeIgniter 中国开发者社区:CodeIgniter4 中文手册(版本 4.0.0).pdf
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)07 图像复原 Image Restoration.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)08 自适应滤波 Adaptive Filter.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)要点复习 Review(主讲:曹洋).pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)09 图像压缩 Image Compression.pptx
- 中国科学技术大学:《计算机视觉》课程教学资源(参考论文)Tour Into the Picture_Using a Spidery Mesh Interface to Make Animation from a Single Image.pdf
- 中国科学技术大学:《计算机视觉》课程教学资源(参考论文)3D photography on your desk.pdf
- 中国科学技术大学:《计算机视觉》课程教学资源(PPT课件讲稿)第一章 绪论 Computer Vison(主讲:曹洋).ppt
- 中国科学技术大学:《计算机视觉》课程教学资源(PPT课件讲稿)第二章 视觉的基本知识 第一节 人类生理视觉系统.ppt
- 中国科学技术大学:《计算机视觉》课程教学资源(PPT课件讲稿)第二章 视觉的基本知识 第二节 视觉物理学特性.pptx
- 中国科学技术大学:《计算机视觉》课程教学资源(PPT课件讲稿)第二章 视觉的基本知识 第三节 视觉系统的几何特性.ppt