#lang sicp
;;https://www.bilibili.com/video/BV1Xx41117tr
;;https://github.com/DeathKing/Learning-SICP/tree/master/SrtCN
;;第十二节
;;;流是集合
;;;从一个集合到另一个集合
;;;集合内建了控制流程
;;;解耦事件表面的顺序,与机器中实际计算春旭
;;;we decouple the apparent order of events in our programs
;;;form the actual order of events in the computer
(define (nth-stream n s)
(if (= n 0)
(head s)
(nth-stream (-1+ n) (tail s))))
(define (print-stream s)
(cond ((empty-stream s) "done")
(else (print (head s))
(print-stream (tail s)))))
(define (integers-from n)
(cons-stream
n
(integers-from (+ n 1))))
(define integers (integers-from 1))
(nth-stream 20 integers)
(define (no-seven x)
(not (= (permainder x 7) 0)))
(define ns
(filter no-seven integers))
(nth-stream 100 ns)
(print-stream ns)
;;;peter henderson 图
;;;
;;; |-------------------------------------------------------|
;;; | integers-from |
;;; | |
;;; | |----------------------------------->| |
;;; N | | |---->|----| |
;;; -------->|--->| |合并 |--|----->
;;; | | |----| |--------------| |---->|----| |
;;; | |---| 1+ |------>|integers-from |--->| |
;;; | |----| |--------------| |
;;; |-------------------------------------------------------|
;;;
;;;
;;;eratosthenes算法
(define (sieve s)
(cons-stream ;;;返回一个新的流(返回什么)
(head s)
(sieve (filter
(lambda (x)
(not (divisible? x (head s))))
(tail s)))))
(define primes
(sieve (integers-from 2)))
(nth-stream 20 primes)
;;;
;;;把信号处理的方法和计算中的递归结合在一起
;;;
;;; 分离流的头部和尾部
;;; -------------------------------------------------------|
;;; | sieve |
;;; | |
;;; | ------head--|----------------------->| |
;;; | / | | |
;;; | / |头部分同样应用于这个盒子 |-->|----\ |
;;; ----->|--< V |cons >-------->
;;; | \ |------------| |--------| |-->|----/ |
;;; | \ |filter | | | | |
;;; | ---->|not |----| sieve |--->| |
;;; | |divisible? | | | |
;;; | |------------| |--------| |
;;; |------------------------------------------------------|
;;;
;;;
;;;the operate on streams all at once
(define (add-streams s1 s2)
(cond ((empty-stream? s1) s2)
((empty-stream? s2) s1)
(else (cons-stream (+ (head s1) (head s2))
(add-streams (tail s1) (tail s2))))))
(define (scale-stream c s)
(map-stream (lambda (x)
(* x c))
s))
;;;无穷个1的流
;;;流和递归相结合
(define ones (cons-stream 1 ones))
;;;(cons 1 (delay ones))
(define integers (cons-stream 1
;;;尾部为将每个整数加1
(add-streams integers ones)))
;;;
;;; 累加器
;;; |\ |
;;; ones-------->| \ |1,初始值为1
;;; | \ V
;;; |+ >-----------------|----->integers
;;; | / |
;;; |--------->| / |
;;; | |/ |
;;; | |
;;; ---------------------------------|
;;;
;;;
;;;
;;;
;;;
;;;
;;; S0
;;; |
;;; |----------------------------|----------|
;;; | integrator | |
;;; | 累加器 | |
;;; | |\ | |
;;;s--|----dt----->| \ | |
;;; | | \ V |
;;; | |+ >-----------------|----|--->Sdt
;;; | | / | |
;;; | |--------->| / | |
;;; | | |/ | |
;;; | | | |
;;; | ---------------------------------| |
;;; |---------------------------------------|
;;;
;;;
(define (integral s initial-value dt)
(define int (cons-stream initial-value
(add-streams (scale-stream dt s) int)))
int)
;;;斐波那契数,以0和1开始
;;;0和1之后的数,是通过加和前两个数得来的
(define fibs
(cons-stream 0
(cons-stream 1
(add-streams fibs (tail fibs)))))
;;; fibs | 0 1 1 2 3 ...
;;; tail fibs | 1 1 2 3 ...
;;; -------------------------------
;;; fibs 0 1 | 1 2 3 5 ...
;;; 以0和1开始,后面是前两个数的加和
;;;定义流,然后自己使用自己(递归)
;;;过程与数据之间没有区别
;;;there's no difference really between procedures and data
;;;既然我们有递归过程,那么有递归数据也是正常的了
;;;so the fact that we have recusive procedures,
;;;well, then is should be natural that we have recursive data
;;;原因是cons-stream不需要其尾部分,就可以完成有意义的事情
;;;the reason is that cons-stream can do a useful thing without looking at its tail.
;;;(delay <exp>)中
;;;(delay ... )正好互相抵消,也就是变成<exp>
;;;以下引用自:
;;;=========================begin======================================
;;;https://github.com/xavier/sicp-notes/blob/master/lecture_06b.md
;;;y' = y^2
;;;y(0) = 1
;;;dt = 0.001
;;; 1
;;; |
;;; v
;;; y' +------------+ y
;;;--+->| integrator |---+-->
;;; | +------------+ |
;;; | |
;;; | +------------+ |
;;; +--| map square |<--+
;;; +------------+
(define y
(integeral dy 1 0.001))
(define dy
(map square y))
;;;另一种定义方式
(define (integral delayed-s initial-value dt)
(define int
(cons-stream
initial-value
(let ((s (force delayed-s)))
(add-streams (scale-stream dt s) int))))
int)
(define y
(integeral (delay dy) 1 0.001))
(define dy
(map square y))
;;;======================end===================================
;;;正则序求值
;;;normal-order evaluation
;;;应用序求值
;;;applicative-order evaluation
;;;you just put a promise to compute them there
;;;until you get down to a primitive operator
;;;miranda
;;阶乘
(define (fact-iter n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
;;;正则序的缺点之一就是无法有效的表达迭代
;;;so one of the disadvantages is that you can't really express iteration
;;;正则序和副作用是不相容的
;;;that normal-order evaluation and side effects just don't mix
;;;假设这个程序是正则序求值
(define x 0)
;;;恒等函数,输入n,返回n
(define (id n)
(set! x n)
n)
(define (inc a) (1+ a))
(define y (inc (id 3)))
x ---> 0,因为此时只是定义了y,y为一个promise,id是promise,inc也是promise,y并没有实际运行,id也没有运行
y ---> 4,运行y
x ---> 3,y已经运行了,所以x为3
;;;我们把计算机中事件发生的顺序与程序中的顺序分离开来
;;;we decouple the order of events in the computer from what we write in our programs.
;;;但是当我们谈及状态、赋值和改变的时候,这些又都是我们想要控制的
;;;but when we talk about state and set and change that's
;;;exactly what we do want control of
;;;我们的目的有着根本性的矛盾
;;;so it's almost as if there's this fundamental contradiction in what you want
;;;deposit 存款
(define (make-deposit-account balance deposit-stream)
(cons-stream
balance
(make-deposit-account)
(+ balance (head deposit-stream))
(tail deposit-stream)))
sicp-mit-12 流
于 2022-02-09 08:05:43 首次发布