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 f86fd87023322b8e1483e8e7eb6ea74e0b940fe8 1 parent 5f64ba7
@flaming0 authored
Showing with 49 additions and 0 deletions.
  1. +34 −0 ch3/ex3-56.scm
  2. +15 −0 ch3/ex3-58.scm
View
34 ch3/ex3-56.scm
@@ -0,0 +1,34 @@
+;; Example 3.56
+
+#lang racket
+
+(require racket/stream)
+
+(define (scale-stream stream factor)
+ (stream-map (lambda (x) (* x factor)) stream))
+
+(define (merge s1 s2)
+ (cond ((stream-empty? s1) s2)
+ ((stream-empty? s2) s1)
+ (else
+ (let ((s1car (stream-first s1))
+ (s2car (stream-first s2)))
+ (cond ((< s1car s2car)
+ (stream-cons s1car (merge (stream-rest s1) s2)))
+ ((> s1car s2car)
+ (stream-cons s2car (merge s1 (stream-rest s2))))
+ (else
+ (stream-cons s1car
+ (merge (stream-rest s1)
+ (stream-rest s2)))))))))
+
+(define S (stream-cons 1 (merge (scale-stream S 2)
+ (merge (scale-stream S 3) (scale-stream S 5)))))
+
+(stream-ref S 0)
+(stream-ref S 1)
+(stream-ref S 2)
+(stream-ref S 3)
+(stream-ref S 4)
+(stream-ref S 5)
+(stream-ref S 6)
View
15 ch3/ex3-58.scm
@@ -0,0 +1,15 @@
+;; Example 3.58
+
+#lang racket
+
+(require racket/stream)
+
+(define (expand num den radix)
+ (stream-cons
+ (quotient (* num radix) den)
+ (expand (remainder (* num radix) den) den radix)))
+
+(define s1 (expand 1 7 10))
+
+;; 1 4 2 8 5 7
+;; 0.142857142...
Please sign in to comment.
Something went wrong with that request. Please try again.