-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSICP-3.5.2-infinite-streams.scm
36 lines (30 loc) · 1.33 KB
/
SICP-3.5.2-infinite-streams.scm
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
(define (add-streams s1 s2)
(stream-map + s1 s2))
; implicit fibonacci infite series
(define fib
(cons-stream 0
(cons-stream 1
(add-streams fib (stream-cdr fib)))))
(define fib-expanded
(begin (display "shits been called \n") ; only gets called once!
(cons-stream 0
(cons-stream 1
(add-streams
(cons-stream 0
(cons-stream 1
(add-streams fib-expanded (stream-cdr fib-expanded))))
(cons-stream 1
(add-streams fib-expanded (stream-cdr fib-expanded))))))))
; explicit generator of fibonacci infinite series
(define (fib-generator a b)
(cons-stream a (fib-generator b (+ a b))))
(define (display-stream s iterations)
(if (> iterations 0)
(begin
(display "\n")
(display (stream-car s))
(display-stream (stream-cdr s) (- iterations 1)))))
(define what-this-done
(cons-stream 0
(cons-stream 1
(add-streams what-this-done what-this-done))))