| The forms discussed. | Lecture 4 - slide 1 : 27 Program 1 |
"NEXT: infinite evaluation and errors"
((lambda (x) 1) some-infinite-calculation)
(define (some-infinite-calculation) (some-infinite-calculation))
((lambda (x) 1) (some-infinite-calculation))
((lambda (x) 1)
((lambda (x) (x x)) (lambda (x) (x x))) )
((lambda (x) 1) (/ 5 0))
"NEXT: delay and force examples"
(delay (+ 5 6))
(force (delay (+ 5 6)))
(force 11)
(define pr (delay (+ 5 6)))
(+ 7 pr)
(+ 7 (force pr))
"NEXT: Streams stuff - prerequisites"
(define-syntax cons-stream
(syntax-rules ()
((cons-stream x y)
(cons x (delay y)))))
(define head car)
(define (tail stream) (force (cdr stream)))
(define empty-stream? null?)
(define the-empty-stream '())
(define (stream-section n stream)
(cond ((= n 0) '())
(else (cons (head stream)
(stream-section
(- n 1)
(tail stream))))))
(define (add-streams s1 s2)
(let ((h1 (head s1))
(h2 (head s2)))
(cons-stream
(+ h1 h2)
(add-streams (tail s1) (tail s2)))))
"NEXT: Stream examples"
(define ones (cons-stream 1 ones))
ones
(stream-section 7 ones)
(define (integers-starting-from n)
(cons-stream n
(integers-starting-from (+ n 1))))
(define nat-nums
(integers-starting-from 1))
nat-nums
(stream-section 10 nat-nums)
(define nat-nums
(cons-stream 1
(add-streams ones nat-nums)))
(stream-section 10 nat-nums)
(define fibs
(cons-stream 0
(cons-stream 1
(add-streams (tail fibs) fibs))))
fibs
(stream-section 15 fibs)
"NEXT: The sieve of Eratosthenes"
(define (sieve stream)
(cons-stream
(head stream)
(sieve
(filter-stream
(lambda (x) (not (divisible? x (head stream))))
(tail stream)))))
(define (divisible? x y)
(= (remainder x y) 0))
(define (filter-stream p lst)
(cond ((empty-stream? lst) the-empty-stream)
((p (head lst)) (cons-stream (head lst) (filter-stream p (tail lst))))
(else (filter-stream p (tail lst)))))
(define primes (sieve (integers-starting-from 2)))
primes
(stream-section 100 primes)
"THE END"