-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture6A.rkt
216 lines (175 loc) · 5.51 KB
/
lecture6A.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#lang racket
(require "common.rkt")
; Author: Zilu Tian
; Date: March 22, 2020
; Streams, Part 1
; Challenge with Assignment: Time, Identity, Object, Sharing
;(cons-stream x y)
;(head s)
;(tail s)
;the-empty-stream
; Establish conventional interfaces that can glue things together
; map, filter, accumulator
(define (map-stream proc s)
(if (empty-stream? s)
the-empty-stream
(cons-stream
(proc (head s)
(map-stream proc (tail s))))))
(define (filter pred s)
(cond
((empty-stream? s) the-empty-stream)
((pred (head s))
(cons-stream (head s)
(filter pred
(tail s))))
(else (filter pred (tail s)))))
(define (accumulate combiner init-val s)
(if (empty-stream? s)
init-val
(combiner (head s)
(accumulate combiner init-val (tail s)))))
(define (enumerate-tree tree)
(if (leaf-node? tree)
(cons-stream tree the-empty-stream)
(append-streams
(enumerate-tree (left-branch tree))
(enumerate-tree (right-branch tree)))))
(define (append-streams s1 s2)
(if (empty-stream? s1)
s2
(cons-stream (head s1) (append-streams (tail s1) s2))))
(define (enum-interval low high)
(if (> low high)
the-empty-stream
(cons-stream low
(enum-interval (inc low) high))))
(define (sum-odd-squares tree)
(accumulate + 0 (map square (filter odd (enumerate-tree tree)))))
(define (old-fibs n)
(accumulate cons '() (filter odd (map fib (enum-interval 1 n)))))
(define (flatten st-of-st)
(accumulate append-streams the-empty-stream st-of-st))
(define (flatmap f s)
(flatten (map f s)))
; Given N, find all pairs 0 < J < I <= N s.t. I+J is prime
(define find-prime-pairs
(flatmap
(λ (i)
(map
(λ (j) (list i j))
(enum-interval 1 (dec i))))
(enum-interval 1 n)))
;(cons-stream x y)
;Abbreviation for (cons x (delay y))
;(head s) -> (car s)
;(tail s) -> (force (cdr s))
;
;delay: take an expression, and return the promoise for doing the computation upon request
;force: take the promise and compute it
;
;(head (tail (filter prime? (e-i 10000 1000000))))
;-> ; can use substition model as there is no side effect and state
;(head (tail (filter? prime? (cons 10000 (delay (e-1 10001 1000000))))))
;
;(delay <exp>)
;Abbreviation for (λ()<exp>)
;:: decouple the apparent order of the events in the program from the actual order of
;events in the computer
;(force p) = (p)
;
;issue:
;Result in many tail computation, inefficient comparing to a simple list solution
;(tail (tail (tail (tail ...))))
;
;solution:
;(delay <exp>)
;Abbreviation for (MEMO-PROC (λ () <exp> ))
;(MEMO-PROC) is to transform a procedure that only does computation once.
;When you call it for the first time, it will run the original procedure and remember its
;result. After that it won't do computation and just lookup
; proc: a procedure takes no arg
; memoization
(define (memo-proc proc)
(let ((already-run? null) (result null))
(λ ()
(if (not already-run?)
(begin
(set! result (proc))
(set! already-run? (not null))
result)
result))))
(define (nth-stream n s)
(if (= n 0)
(head s)
(nth-stream (dec n) (tail s))))
(define (integers-from n)
(cons-stream
n
(integers-from (+ n 1))))
; a stream of all integers
(define integers
(integers-from 1))
; Sieve of Eratosthenes
; list all the integers (2, 3, 4, ...). Start with 2. Check for primality
; Cross out all integers divisible by 2.
; Keep repeating it.
(define (sieve s)
(cons-stream
(head s)
(sieve (filter
(λ (x)
(not (divisible? x (head s))))
(tail s)))))
(define primes
(sieve (integers-from 2)))
; Programs that want to operate on all stream 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))
; An inf stream of ones
(define ones (cons-stream 1 ones))
(define integers-alt
(cons-stream 1
(add-streams integers-alt ones))) ; work because of 'delay
(define (integral s initial-value dt)
(define int
(cons-stream
initial-value
(add-streams (scale-stream dt s) int)))
int)
; recursive data
(define fib-stream
(cons-stream 0
(cons-stream 1
(add-streams fib-stream (tail fib-stream)))))
; Use delay to allow recursive definition
(define Y
(integral (delay dy) 1 .001)) ; won't complain about dy since it is delayed
(define dy
(map square y))
; Built explicit in the language: normal order evaluation (all delayed operations)
; applicative order evaluation (evaluate the operands before passing them)
; Cons of Normal Order Evaluation:
; - can't express iteration, all recursive ; dragging tails
; - Side-effect doesn't mix well with normal order evalution
; (define x 0)
; (define (id n) (set! x n) n)
; (define (inc a) (+ a 1))
;
; (define y (inc (id 3)))
; x --> 0 ; lazily eval, contains previous value
; y --> 4
; x --> 3 ; (id 3) sets x
; Implement bank account using Stream rather than maintaining a local state
; message-passing object oriented implementation
(define (make-deposit-account balance deposit-stream)
(cons-stream balance (make-deposit-account
(+ balance (head deposit-stream))
(tail deposit-stream))))