Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ch3

  • Loading branch information...
commit 0bf935f103455b6ecba80a446cc4af622a982886 1 parent 52de07a
flamingo authored
15 ch3/ex3-50.scm
View
@@ -0,0 +1,15 @@
+#lang racket
+
+(require racket/stream)
+
+;; Example 3.50
+
+(define (my-stream-map proc . argstreams)
+ (if (stream-empty? (car argstreams))
+ empty-stream
+ (stream-cons
+ (apply proc (map stream-first argstreams))
+ (apply my-stream-map
+ (cons proc (map stream-rest argstreams))))))
+
+(stream-first (my-stream-map + (list 1 2 3) (list 2 3 4) (list 3 4 5)))
36 ch3/ex3-51.scm
View
@@ -0,0 +1,36 @@
+#lang racket
+
+;; Example 3.51
+
+(require racket/stream)
+
+(define (my-stream-ref s n)
+ (if (= n 0)
+ (stream-first s)
+ (my-stream-ref (stream-rest s) (- n 1))))
+
+(define (my-stream-map proc s)
+ (if (stream-empty? s)
+ empty-stream
+ (stream-cons (proc (stream-first s))
+ (my-stream-map proc (stream-rest s)))))
+
+(define (stream-enumerate-interval low high)
+ (if (> low high)
+ empty-stream
+ (stream-cons
+ low
+ (stream-enumerate-interval (+ low 1) high))))
+
+(define (display-line x)
+ (newline)
+ (display x))
+
+(define (show x)
+ (display-line x)
+ x)
+
+(define x (my-stream-map show (stream-enumerate-interval 0 10)))
+
+;(my-stream-ref x 5)
+;(my-stream-ref x 7)
65 ch3/ex3-52.scm
View
@@ -0,0 +1,65 @@
+#lang racket
+
+;; Example 3.52
+
+(require racket/stream)
+
+(define (my-stream-ref s n)
+ (if (= n 0)
+ (stream-first s)
+ (my-stream-ref (stream-rest s) (- n 1))))
+
+(define (my-stream-map proc s)
+ (if (stream-empty? s)
+ empty-stream
+ (stream-cons (proc (stream-first s))
+ (my-stream-map proc (stream-rest s)))))
+
+(define (my-stream-for-each proc s)
+ (if (stream-empty? s)
+ 'done
+ (begin (proc (stream-first s))
+ (my-stream-for-each proc (stream-rest s)))))
+
+(define (display-stream s)
+ (stream-for-each (lambda (x) (newline) (display x)) s))
+
+(define (stream-enumerate-interval low high)
+ (if (> low high)
+ empty-stream
+ (stream-cons
+ low
+ (stream-enumerate-interval (+ low 1) high))))
+
+(define (my-stream-filter pred stream)
+ (cond ((stream-empty? stream) empty-stream)
+ ((pred (stream-first stream))
+ (stream-cons (stream-first stream)
+ (my-stream-filter pred (stream-rest stream))))
+ (else
+ (my-stream-filter pred (stream-rest stream)))))
+
+;; -----
+
+(define sum 0)
+
+(define (accum x)
+ (set! sum (+ x sum))
+ sum)
+
+(define seq (my-stream-map accum (stream-enumerate-interval 1 10)))
+
+;; sum = 0
+
+(define y (my-stream-filter even? seq))
+
+;; sum = 6
+;;
+
+(define z (my-stream-filter (lambda (x) (= (remainder x 5) 0)) seq))
+
+;; 10
+
+;(my-stream-ref y 7)
+
+;(display-stream z)
28 ch3/ex3-53.scm
View
@@ -0,0 +1,28 @@
+#lang racket
+
+;; Example 3.53
+
+(require racket/stream)
+
+(define (my-stream-map proc . argstreams)
+ (if (stream-empty? (car argstreams))
+ empty-stream
+ (stream-cons
+ (apply proc (map stream-first argstreams))
+ (apply my-stream-map
+ (cons proc (map stream-rest argstreams))))))
+
+(define (add-streams s1 s2)
+ (my-stream-map + s1 s2))
+
+(define s (stream-cons 1 (add-streams s s)))
+
+;; 1 1+1 2+2 4+4 8+8 16+16
+;; 1 2 4 8 16 32 64
+
+(stream-ref s 0)
+(stream-ref s 1)
+(stream-ref s 2)
+(stream-ref s 3)
+(stream-ref s 4)
+(stream-ref s 5)
32 ch3/ex3-54.scm
View
@@ -0,0 +1,32 @@
+#lang racket
+
+;; Example 3.54
+
+(require racket/stream)
+
+(define (my-stream-map proc . argstreams)
+ (if (stream-empty? (car argstreams))
+ empty-stream
+ (stream-cons
+ (apply proc (map stream-first argstreams))
+ (apply my-stream-map
+ (cons proc (map stream-rest argstreams))))))
+
+(define (mul-streams s1 s2)
+ (my-stream-map * s1 s2))
+
+(define (integers-starting-from n)
+ (stream-cons n (integers-starting-from (+ n 1))))
+
+(define integers (integers-starting-from 1))
+
+(define factorials (stream-cons 1
+ (mul-streams factorials
+ (integers-starting-from 2)))) ;; n! = (n-1)!*n
+
+(stream-ref factorials 0)
+(stream-ref factorials 1)
+(stream-ref factorials 2)
+(stream-ref factorials 3)
+(stream-ref factorials 4)
+(stream-ref factorials 5)
34 ch3/ex3-55.scm
View
@@ -0,0 +1,34 @@
+#lang racket
+
+;; Example 3.55
+
+(require racket/stream)
+
+(define (integers-starting-from n)
+ (stream-cons n (integers-starting-from (+ n 1))))
+
+(define integers (integers-starting-from 1))
+
+(define (my-stream-map proc . argstreams)
+ (if (stream-empty? (car argstreams))
+ empty-stream
+ (stream-cons
+ (apply proc (map stream-first argstreams))
+ (apply my-stream-map
+ (cons proc (map stream-rest argstreams))))))
+
+(define (add-streams s1 s2)
+ (my-stream-map + s1 s2))
+
+;; s0, s0+s1, s0+s1+s2, ...
+
+(define (partial-sums stream)
+ (stream-cons (stream-first stream)
+ (add-streams (partial-sums stream)
+ (stream-rest stream))))
+
+(define int-partial-sums (partial-sums integers))
+
+(stream-ref int-partial-sums 1)
+(stream-ref int-partial-sums 2)
+(stream-ref int-partial-sums 3)
22 ch3/test_infinite_streams.scm
View
@@ -0,0 +1,22 @@
+#lang racket
+
+;; it's fukken awesome
+
+(require racket/stream)
+
+(define (divisible? x y)
+ (= (remainder x y) 0))
+
+(define (integers-starting-from n)
+ (stream-cons n (integers-starting-from (+ n 1))))
+
+(define (sieve stream)
+ (stream-cons
+ (stream-first stream)
+ (sieve (stream-filter
+ (lambda (x) (not (divisible? x (stream-first stream))))
+ (stream-rest stream)))))
+
+(define primes (sieve (integers-starting-from 2)))
+
+(stream-ref primes 50)
56 ch3/test_streams.scm
View
@@ -1,30 +1,48 @@
#lang racket
-(define (cons-stream a b)
- (cons a (stream-delay b)))
+(require racket/stream)
-(define (stream-car stream)
- (car stream))
+;; cons-stream is a special form
+;(define (cons-stream a b) ;; we don't need to compute b!!!
+; (cons a (stream-delay b)))
-(define (stream-cdr stream)
- (stream-force (cdr stream)))
+;(define (stream-car stream)
+; (car stream))
-(define (stream-ref s n)
+;(define (stream-cdr stream)
+; (stream-force (cdr stream)))
+
+(define (my-stream-ref s n)
(if (= n 0)
- (stream-car s)
- (stream-ref (stream-cdr s) (- n 1))))
+ (stream-first s)
+ (my-stream-ref (stream-rest s) (- n 1))))
-(define (stream-map proc s)
- (if (stream-null? s)
- the-empty-stream
- (cons-stream (proc (stream-car s))
- (stream-map proc (stream-cdr s)))))
+(define (my-stream-map proc s)
+ (if (stream-empty? s)
+ empty-stream
+ (stream-cons (proc (stream-first s))
+ (my-stream-map proc (stream-rest s)))))
-(define (stream-for-each proc s)
- (if (stream-null? s)
+(define (my-stream-for-each proc s)
+ (if (stream-empty? s)
'done
- (begin (proc (stream-car s))
- (stream-for-each proc (stream-cdr s)))))
+ (begin (proc (stream-first s))
+ (my-stream-for-each proc (stream-rest s)))))
(define (display-stream s)
- (stream-for-each (lambda (x) (newline) (display x)) s))
+ (stream-for-each (lambda (x) (newline) (display x)) s))
+
+(define (stream-enumerate-interval low high)
+ (if (> low high)
+ empty-stream
+ (stream-cons
+ low
+ (stream-enumerate-interval (+ low 1) high))))
+
+(define (my-stream-filter pred stream)
+ (cond ((stream-empty? stream) empty-stream)
+ ((pred (stream-first stream))
+ (stream-cons (stream-first stream)
+ (my-stream-filter pred (stream-rest stream))))
+ (else
+ (my-stream-filter pred (stream-rest stream)))))
Please sign in to comment.
Something went wrong with that request. Please try again.