From: Ramakrishnan Muthukrishnan Date: Thu, 28 Jul 2011 16:55:28 +0000 (+0530) Subject: solutions to 3.64, 3.65, 3.66, 3.67, 3.68 and 3.69 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/bcase/quickstart.html?a=commitdiff_plain;h=8b52e1f31c0570c4180f1772f9d1226d349be37c;p=sicp.git solutions to 3.64, 3.65, 3.66, 3.67, 3.68 and 3.69 --- diff --git a/src/sicp/ex3_64.rkt b/src/sicp/ex3_64.rkt new file mode 100644 index 0000000..19f5d15 --- /dev/null +++ b/src/sicp/ex3_64.rkt @@ -0,0 +1,10 @@ +#lang planet neil/sicp + +;; stream-of-numbers -> floating point number -> number +(define (stream-limit s limit) + (let ([num0 (stream-car s)] + [num1 (stream-car (stream-cdr s))]) + (let ([diff (- num0 num1)]) + (if (< (abs diff) limit) + num1 + (stream-limit (stream-cdr s) limit))))) \ No newline at end of file diff --git a/src/sicp/ex3_65.rkt b/src/sicp/ex3_65.rkt new file mode 100644 index 0000000..7d5be8a --- /dev/null +++ b/src/sicp/ex3_65.rkt @@ -0,0 +1,7 @@ +#lang racket + +(define (ln2-summands n) + (cons-stream (/ 1.0 n) + (stream-map - (ln2-summands (+ n 1))))) + +(define ln2 (partial-sums (ln2-summands 1))) \ No newline at end of file diff --git a/src/sicp/ex3_66.rkt b/src/sicp/ex3_66.rkt new file mode 100644 index 0000000..530b289 --- /dev/null +++ b/src/sicp/ex3_66.rkt @@ -0,0 +1,14 @@ +#lang racket + +#| + +Writing down the pairs, one can sort of form the following judgement. + +For any particular pair of the form S0Tm, the number of terms which precedes it +is (+ (* 2 (- m 1)) 1) for m >= 2 + +For a pair of the form S1Tm, it is (* (- m 1) 4) for m >= 2. + +Similarly one can get working formulae for other row, column pairs. + +|# \ No newline at end of file diff --git a/src/sicp/ex3_67.rkt b/src/sicp/ex3_67.rkt new file mode 100644 index 0000000..7a32c7e --- /dev/null +++ b/src/sicp/ex3_67.rkt @@ -0,0 +1,22 @@ +#lang racket + +(define (interleave s1 s2) + (if (stream-null? s1) + s2 + (cons-stream (stream-car s1) + (interleave s2 (stream-cdr s1))))) + +(define (pairs s t) + (cons-stream + (list (stream-car s) (stream-car t)) + (interleave (interleave + (stream-map (lambda (x) (list (stream-car s) x)) + (stream-cdr t)) + (stream-map (lambda (y) (list y (stream-car t))) + (stream-cdr s))) + (pairs (stream-cdr s) (stream-cdr t))))) + + + + + diff --git a/src/sicp/ex3_68.rkt b/src/sicp/ex3_68.rkt new file mode 100644 index 0000000..4af41dc --- /dev/null +++ b/src/sicp/ex3_68.rkt @@ -0,0 +1,9 @@ +#lang racket + +#| + +'interleave' is a procedure and not a special form because of which the arguments +of the interleave will be evaluated before interleave itself is called. This will +result in an infinite number of calls to 'pair'. + +|# \ No newline at end of file diff --git a/src/sicp/ex3_69.rkt b/src/sicp/ex3_69.rkt new file mode 100644 index 0000000..6b0905e --- /dev/null +++ b/src/sicp/ex3_69.rkt @@ -0,0 +1,11 @@ +#lang racket + +(define (triples s t u) + (cons-stream (append (stream-car (pairs s t)) + (list (stream-car u))) + (interleave + (stream-map (lambda(p) (append p (list (stream-car u)))) + (stream-cdr (pairs s t))) + (triples (stream-cdr s) + (stream-cdr t) + (stream-cdr u))))) \ No newline at end of file