SICP Exercise 3.76

August 26, 2009

SICP Exercise 3.76

(define (smooth input-stream)
  (let* ((v1 (stream-car input-stream))
         (s (stream-cdr input-stream))
         (v2 (stream-car s)))
    (cons-stream (/ (+ v1 v2) 2)
                 (smooth s))))

SICP Exercise 3.75

August 26, 2009

SICP Exercise 3.75

(define (make-zero-crossings input-stream last-value last-avpt)
  (let ((avpt (/ (+ (stream-car input-stream) last-value) 2)))
    (cons-stream (sign-change-detector avpt last-avpt)
                 (make-zero-crossings (stream-cdr input-stream)
                                      (stream-car input-stream) 
                                      avpt))))

SICP Exercise 3.74

August 26, 2009

SICP Exercise 3.74

(define zero-crossings
  (stream-map sign-change-detector sense-data 
              (cons-stream 0 sense-data)))

SICP Exercise 3.73

August 26, 2009

SICP Exercise 3.73

(define (RC R C dt)
  (lambda (i v0)
    (add-streams (integral (scale-stream ones (/ 1.0 C)) v0 dt)
                 (scale-stream i R))))

SICP Exercise 3.72

August 25, 2009

SICP Exercise 3.72

(define (ex-3-72-weight pair)
  (let ((i (car pair))
        (j (cadr pair)))
    (+ (square i) (square j))))
(define the-pairs
  (weighted-pairs integers integers ex-3-72-weight))
(define (search-ex-3-72 S-pairs)
  (let* ((w1 (ex-3-72-weight (stream-car S-pairs)))
         (rest-of-S-pairs (stream-cdr S-pairs))
         (w2 (ex-3-72-weight (stream-car rest-of-S-pairs)))
         (rest-of-rest-of-S-pairs (stream-cdr rest-of-S-pairs))
         (w3 (ex-3-72-weight (stream-car rest-of-rest-of-S-pairs))))
    (cond ((= w1 w2 w3)
           (newline)
           (display (stream-car S-pairs))
           (newline)
           (display (stream-car rest-of-S-pairs))
           (newline)
           (display (stream-car rest-of-rest-of-S-pairs))
           (cons-stream w1 (search-ex-3-72 (stream-cdr rest-of-rest-of-S-pairs))))
          ((= w2 w3)
           (search-ex-3-72 rest-of-S-pairs))
          (else 
           (search-ex-3-72 rest-of-rest-of-S-pairs)))))
(define S-ex-3-72 (search-ex-3-72 the-pairs))

; (1 18)
; (6 17)
; (10 15)
;Value: 325

; (5 20)
; (8 19)
; (13 16)
;Value: 425

; (5 25)
; (11 23)
; (17 19)
;Value: 650

; (7 26)
; (10 25)
; (14 23)
;Value: 725

SICP Exercise 3.71

August 25, 2009

SICP Exercise 3.71

The first six Ramanujan numbers are 1729, 4104, 13832, 20683, 32832, and 39312.

(define (Ram-weight pair)
  (let ((i (car pair))
        (j (cadr pair)))
    (+ (* i i i) (* j j j))))
(define the-pairs
  (weighted-pairs integers integers Ram-weight))
(define (search-Ram S-pairs)
  (let* ((w1 (Ram-weight (stream-car S-pairs)))
         (rest-of-S-pairs (stream-cdr S-pairs))
         (w2 (Ram-weight (stream-car rest-of-S-pairs))))
    (if (= w1 w2)
        (cons-stream w1 (search-Ram (stream-cdr rest-of-S-pairs)))
        (search-Ram rest-of-S-pairs))))
(define S-Ram (search-Ram the-pairs))

SICP Exercise 3.70

August 25, 2009

SICP Exercise 3.70

(define (weighted-pairs s t weight)
  (define (merge s1 s2)
    (cond ((stream-null? s1) s2)
          ((stream-null? s2) s1)
          (else
           (let ((s1car (stream-car s1))
                 (s2car (stream-car s2)))
             (cond ((<= (weight s1car) (weight s2car))
                    (cons-stream s1car (merge (stream-cdr s1) s2)))
                   (else 
                    (cons-stream s2car (merge s1 (stream-cdr s2)))))))))
  (cons-stream
   (list (stream-car s) (stream-car t))
   (merge 
    (stream-map (lambda (x) (list (stream-car s) x))
                (stream-cdr t))
    (weighted-pairs (stream-cdr s) (stream-cdr t) weight))))

;; a
(define (weight-a pair)
  (+ (car pair) (cadr pair)))
(define sa (weighted-pairs integers integers weight-a))

;; b
(define (weight-b pair)
  (+ (* 2 (car pair)) (* 3 (cadr pair)) 
     (* 5 (car pair) (cadr pair))))
(define (divisible? x y) (= (remainder x y) 0))
(define i-235 
  (stream-filter (lambda (x)
                   (and (not (divisible? x 2))
                        (not (divisible? x 3))
                        (not (divisible? x 5))))
                 integers))
(define sb (weighted-pairs i-235 i-235 weight-b))

SICP Exercise 3.69

August 22, 2009

SICP Exercise 3.69

(define (triples s t u)
  (cons-stream 
   (list (stream-car s) (stream-car t) (stream-car u))
   (interleave
    (stream-map (lambda (x) (cons (stream-car s)  x))
                (pairs t (stream-cdr u)))
    (triples (stream-cdr s) (stream-cdr t) (stream-cdr u)))))

(define tri (triples integers integers integers))

(define (Py-test l)
  (= (+ (square (car l)) (square (cadr l)))
     (square (caddr l))))

(define Py-tri (stream-filter Py-test tri))

SICP Exercise 3.68

August 21, 2009

SICP Exercise 3.68

It doesn’t work. The evaluation of (pairs intergers intergers) will result in an infinite loop of calling interleave and pairs recursively. The original version does not have such a problem because pairs calls cons-stream, which delays the call to interleave.


SICP Exercise 3.67

August 21, 2009

SICP Exercise 3.67

(define (interleave-3 s1 s2 s3)
  (cond ((stream-null? s1) (interleave s2 s3))
        ((stream-null? s2) (interleave s1 s3))
        ((stream-null? s3) (interleave s1 s2))
        (else (cons-stream 
               (stream-car s1)
               (cons-stream 
                (stream-car s2)
                (cons-stream 
                 (stream-car s3)
                 (interleave-3 (stream-cdr s1)
                               (stream-cdr s2)
                               (stream-cdr s3))))))))
(define (pairs s t)
  (cons-stream
   (list (stream-car s) (stream-car t))
   (interleave-3
    (stream-map (lambda (x) (list (stream-car s) x))
                (stream-cdr t))
    (stream-map (lambda (x) (list x (stream-car t)))
                (stream-cdr s))
    (pairs (stream-cdr s) (stream-cdr t)))))

Follow

Get every new post delivered to your Inbox.