Skip to content

Commit

Permalink
part b not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiorama committed Mar 4, 2010
1 parent 862e1a9 commit c3ddea3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions ex370.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(load "./chapter3.scm")

(define (merge-weighted s1 s2 weight-fn)
(cond ((stream-null? s1) s2)
((stream-null? s1) s1)
(else
(let ((s1car (stream-car s1))
(s2car (stream-car s2)))
(if (weight-fn s1car s2car)
(cons-stream s1car (merge-weighted (stream-cdr s1) s2 weight-fn))
(cons-stream s2car (merge-weighted s1 (stream-cdr s2) weight-fn)))
))))


(define (weighted-pairs s t w)
(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) w)
w)))

;; a. 和 i + j に従って順序づけられた、i <= j なる正の整数の対 (i,j) のすべてのストリーム
;;
;; 上のようなストリームを生成する pairs-a 関数があるとして
;; weight 計算するときは、(stream-car pairs-a) と (stream-cdr pairs-a) の大小を比較して、
;; car か cddr へ進むようなことをするのか
;;
(define (weight-a x y)
(< (+ (car x) (cadr x))
(+ (car y) (cadr y))))

(define sum-order-pairs (weighted-pairs integers integers weight-a))

;; b. 和 2i + 3j + 5ij に従って順序づけられた、i <= j で、i も j も 2, 3, 5 で割り切れない正の整数の対 (i,j) のすべてのストリーム
;;
;;
(define (weight-b x y)
(< (+ (* 2 (car x)) (* 3 (cadr x)) (* 5 (car x) (cadr x)))
(+ (* 2 (car y)) (* 3 (cadr y)) (* 5 (car y) (cadr y)))))

0 comments on commit c3ddea3

Please sign in to comment.