読者です 読者をやめる 読者になる 読者になる

(wat-aro)

無職から有職者にランクアップしました

SICP 問題 3.56

scheme SICP
(define (merge s1 s2)
  (cond ((stream-null? s1) s2)
        ((stream-null? s2) s1)
        (else (let ((s1car (stream-car s1))
                    (s2car (stream-car s2)))
                (cond ((< s1car s2car)
                       (cons-stream s1car
                                    (merge (stream-cdr s1) s2)))
                      ((> s1car s2car)
                       (cons-stream s2car
                                    (merge s1 (stream-cdr s2))))
                      (else
                       (cons-stream s1car
                                    (merge (stream-cdr s1)
                                           (stream-cdr s2)))))))))

(define S (cons-stream 1 (merge (scale-stream S 2)
                                (merge (scale-stream S 3)
                                       (scale-stream S 5)))))
gosh> (stream-ref S 0)
1
gosh> (stream-ref S 1)
2
gosh> (stream-ref S 2)
3
gosh> (stream-ref S 3)
4
gosh> (stream-ref S 4)
5
gosh> (stream-ref S 5)
6
gosh> (stream-ref S 6)
8
gosh> (stream-ref S 7)
9
gosh> (stream-ref S 8)
10