From: Ramakrishnan Muthukrishnan Date: Sat, 9 Jul 2011 15:30:49 +0000 (+0530) Subject: solution to 3.55, 3.56 and 3.57 X-Git-Url: https://git.rkrishnan.org/pf/frontends/%5B//%5D%20/uri/URI:DIR2-RO:%5B%5E?a=commitdiff_plain;h=3f804070ae64fb8d44227676eb8c02074c144e25;p=sicp.git solution to 3.55, 3.56 and 3.57 --- diff --git a/src/sicp/ex3_55.rkt b/src/sicp/ex3_55.rkt new file mode 100644 index 0000000..e53d5e8 --- /dev/null +++ b/src/sicp/ex3_55.rkt @@ -0,0 +1,21 @@ +#lang racket + +(define (partial-sums s) + (define (partial-sums-iter sum stream) + (cons-stream (+ sum (stream-car stream)) + (partial-sums-iter (+ sum (stream-car stream)) + (stream-cdr stream)))) + (partial-sums-iter 0 s)) + +#| +> (stream-ref (partial-sums (integers-starting-from 1)) 0) +1 +> (stream-ref (partial-sums (integers-starting-from 1)) 1) +3 +> (stream-ref (partial-sums (integers-starting-from 1)) 2) +6 +> (stream-ref (partial-sums (integers-starting-from 1)) 3) +10 +> (stream-ref (partial-sums (integers-starting-from 1)) 4) +15 +|# \ No newline at end of file diff --git a/src/sicp/ex3_56.rkt b/src/sicp/ex3_56.rkt new file mode 100644 index 0000000..d06885d --- /dev/null +++ b/src/sicp/ex3_56.rkt @@ -0,0 +1,40 @@ +#lang racket + +(define S (cons-stream 1 (merge (scale-stream S 2) + (merge (scale-stream S 3) + (scale-stream S 5))))) + +#| + +> (display-stream S) + +1 +2 +3 +4 +5 +6 +8 +9 +10 +12 +15 +16 +18 +20 +24 +25 +27 +30 +32 +36 +40 +45 +48 +50 +54 +60 +64 +72 + +|# diff --git a/src/sicp/ex3_57.rkt b/src/sicp/ex3_57.rkt new file mode 100644 index 0000000..e6735fc --- /dev/null +++ b/src/sicp/ex3_57.rkt @@ -0,0 +1,15 @@ +#lang racket + +#| + +We do (n - 1) additions for the memo-proc based delay implementation. + +With the simple delay implementation, for a given n, it constructs the +fib sequence: + + fib (n) = fib (n-1) + fib (n-2) + +In our case, the number of additions for nth fib will be equal to the +number of additions for the fib(n-1) and that for fib(n-2) + 1 + +|# \ No newline at end of file