sicp-ex-3.72



<< Previous exercise (3.71) | Index | Next exercise (3.73) >>


zerocooldown

  
  
  
 (define (weighted-pairs s t weight) 
   (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) weight) 
     weight))) 
  
 (define (collect-consecutive-pairs-n s n evaluator-n) 
   (let ((next-n (take-n s n))) 
     (if (evaluator-n next-n) 
         (cons-stream next-n 
                      (collect-consecutive-pairs-n (stream-cdr s) 
                                                   n 
                                                   evaluator-n)) 
         (collect-consecutive-pairs-n (stream-cdr s) 
                                      n 
                                      evaluator-n)))) 
  
 (define (square-sum p) 
   (let ((i (car p)) 
         (j (cadr p))) 
     (+ (* i i) 
        (* j j)))) 
  
 (define 3-way-square-sum 
   (stream-map (lambda (t) 
                 (list (square-sum (car t)) t)) 
               (collect-consecutive-pairs-n (weighted-pairs integers 
                                                            integers 
                                                            square-sum) 
                                            3 
                                            (lambda (t) 
                                              (= (square-sum (car t)) 
                                                 (square-sum (cadr t)) 
                                                 (square-sum (caddr t)))))))