From: Ramakrishnan Muthukrishnan Date: Sat, 30 Jul 2011 16:13:55 +0000 (+0530) Subject: solutions to 3.70, 3.71 and 3.72 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/bcase/-?a=commitdiff_plain;h=c1300afed0b29af6580ed84ff1633a8e000aa1c1;p=sicp.git solutions to 3.70, 3.71 and 3.72 --- diff --git a/src/sicp/ex3_70.rkt b/src/sicp/ex3_70.rkt new file mode 100644 index 0000000..ba72cef --- /dev/null +++ b/src/sicp/ex3_70.rkt @@ -0,0 +1,250 @@ +#lang racket + +(define (merge-weighted p1 p2 weightfn) + (cond ((stream-null? p1) p2) + ((stream-null? p2) p1) + (else + (let ((p1car (stream-car p1)) + (p2car (stream-car p2))) + (let ((w1 (weightfn (car p1car) (car (cdr p1car)))) + (w2 (weightfn (car p2car) (car (cdr p2car))))) + (cond ((< w1 w2) + (cons-stream p1car + (merge-weighted (stream-cdr p1) p2 weightfn))) + ((> w1 w2) + (cons-stream p2car + (merge-weighted p1 (stream-cdr p2) weightfn))) + (else + (cons-stream p1car + (cons-stream p2car + (merge-weighted (stream-cdr p1) + (stream-cdr p2) + weightfn)))))))))) + + +(define (weighted-pairs s t weightfn) + (cons-stream + (list (stream-car s) (stream-car t)) + (merge-weighted + (stream-map (lambda (x) (list (stream-car s) x)) + (stream-cdr t)) + (weighted-pairs (stream-cdr s) (stream-cdr t) weightfn) + weightfn))) + +#| + +> (display-stream (weighted-pairs integers integers (lambda (i j) (+ i j)))) + +(1 1) +(1 2) +(1 3) +(2 2) +(1 4) +(2 3) +(1 5) +(2 4) +(3 3) +(1 6) +(2 5) +(3 4) +(1 7) +(2 6) +(3 5) +(4 4) +(1 8) +(2 7) +(3 6) +(4 5) +(1 9) +(2 8) +(3 7) +(4 6) +(5 5) +(1 10) +(2 9) +(3 8) +(4 7) +(5 6) +(1 11) +(2 10) +(3 9) +(4 8) +(5 7) +(6 6) +(1 12) +(2 11) +(3 10) +(4 9) +(5 8) +(6 7) +(1 13) +(2 12) +(3 11) +(4 10) +(5 9) +(6 8) +(7 7) +(1 14) +(2 13) +(3 12) +(4 11) +(5 10) +(6 9) +(7 8) +(1 15) +(2 14) +(3 13) +(4 12) +(5 11) +(6 10) +(7 9) +(8 8) +(1 16) +(2 15) +(3 14) + +|# + +;; part 2. + + +(display-stream (stream-filter (lambda (p) + (let ((i (car p)) + (j (car (cdr p)))) + (not (or (divides? 2 i) + (divides? 2 j) + (divides? 3 i) + (divides? 3 j) + (divides? 5 i) + (divides? 5 j))))) + (weighted-pairs integers integers (lambda (i j) (+ (* 2 i) (* 3 j) (* 5 i j)))))) + +#| + +(1 1) +(1 7) +(1 11) +(1 13) +(1 17) +(1 19) +(1 23) +(1 29) +(1 31) +(7 7) +(1 37) +(1 41) +(1 43) +(1 47) +(1 49) +(1 53) +(7 11) +(1 59) +(1 61) +(7 13) +(1 67) +(1 71) +(1 73) +(1 77) +(1 79) +(7 17) +(11 11) +(1 83) +(1 89) +(1 91) +(7 19) +(11 13) +(1 97) +(1 101) +(1 103) +(1 107) +(1 109) +(7 23) +(1 113) +(13 13) +(1 119) +(1 121) +(11 17) +(1 127) +(1 131) +(1 133) +(1 137) +(1 139) +(7 29) +(11 19) +(1 143) +(13 17) +(7 31) +(1 149) +(1 151) +(1 157) +(1 161) +(1 163) +(13 19) +(1 167) +(1 169) +(11 23) +(1 173) +(7 37) +(1 179) +(1 181) +(1 187) +(1 191) +(17 17) +(1 193) +(7 41) +(1 197) +(13 23) +(1 199) +(1 203) +(7 43) +(1 209) +(1 211) +(11 29) +(17 19) +(1 217) +(1 221) +(1 223) +(7 47) +(1 227) +(11 31) +(1 229) +(1 233) +(7 49) +(19 19) +(1 239) +(1 241) +(1 247) +(13 29) +(1 251) +(1 253) +(7 53) +(1 257) +(17 23) +(1 259) +(1 263) +(13 31) +(1 269) +(11 37) +(1 271) +(1 277) +(1 281) +(7 59) +(1 283) +(19 23) +(1 287) +(1 289) +(7 61) +(1 293) +(1 299) +(11 41) +(1 301) +(1 307) +(1 311) +(1 313) +(11 43) +(1 317) +(13 37) +(1 319) +(7 67) + +|# \ No newline at end of file diff --git a/src/sicp/ex3_71.rkt b/src/sicp/ex3_71.rkt new file mode 100644 index 0000000..312d55e --- /dev/null +++ b/src/sicp/ex3_71.rkt @@ -0,0 +1,14 @@ +#lang racket + +(define (stream-duplicates s) + (if (= (stream-car s) (stream-car (stream-cdr s))) + (cons-stream (stream-car s) + (stream-duplicates (stream-cdr s))) + (stream-duplicates (stream-cdr s)))) + +(define (cube-sum i j) + (+ (* i i i) (* j j j))) + +(display-stream (stream-duplicates (stream-map (lambda (p) + (cube-sum (car p) (car (cdr p)))) + (weighted-pairs integers integers cube-sum)))) \ No newline at end of file diff --git a/src/sicp/ex3_72.rkt b/src/sicp/ex3_72.rkt new file mode 100644 index 0000000..09fe4c0 --- /dev/null +++ b/src/sicp/ex3_72.rkt @@ -0,0 +1,51 @@ +#lang racket + +(define (stream-triplicates s) + (if (= (stream-car s) (stream-car (stream-cdr s)) (stream-car (stream-cdr (stream-cdr s)))) + (cons-stream (stream-car s) + (stream-triplicates (stream-cdr (stream-cdr s)))) + (stream-triplicates (stream-cdr s)))) + +(define (square-sum i j) + (+ (* i i) (* j j))) + +(display-stream (stream-triplicates (stream-map (lambda (p) + (square-sum (car p) (car (cdr p)))) + (weighted-pairs integers integers square-sum)))) + +#| + +325 +425 +650 +725 +845 +850 +925 +1025 +1105 +1250 +1300 +1325 +1445 +1450 +1525 +1625 +1690 +1700 +1825 +1850 +1885 +2050 +2125 +2210 +2225 +2405 +2425 +2465 +2525 +2600 +2650 +2665 + +|# \ No newline at end of file