이 문제는 pair의 확장판인 triples 프로시저를 만드는 문제입니다.
pairs와 같이 세 부분을 나눴습니다.
그리고 stream-map 프로시저에 들어가는 스트림을
(stream-cdr t)와 (stream-cdr u)로 만들어낸 pair 스트림을 넣었습니다.
왜냐하면 pairs와 마찬가지로 s를 행으로 하는 행렬표를 만들었기 때문입니다.
그리고 피타고라스의 정리를 따르는 것은 stream-filter 프로시저를 썼습니다.

잘 되는군요.^^
pythagorean_triples 스트림을 10개까지 찍고 싶었지만 에러를 내놓았습니다.
역시 양이 너무 많아서 그러는 듯싶습니다.OTL...
참조
해럴드 애빌슨, 김재우 역, <컴퓨터 프로그램의 구조와 해석>, 인사이트, 2007, pp. 445
; stream
(define true (= 0 0))
(define false (= 1 0))
(define (cons-stream a b)
(cons a (delay b)))
(define the-empty-stream '())
(define stream-null? null?)
(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))
; section 3.5
(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))
(define (stream-for-each proc s)
(if (stream-null? s)
'done
(begin (proc (stream-car s))
(stream-for-each proc (stream-cdr s)))))
(define (display-stream s)
(stream-for-each display-line s))
(define (display-line x)
(newline)
(display x))
(define (stream-enumerate-interval low high)
(if (> low high)
the-empty-stream
(cons-stream
low
(stream-enumerate-interval (+ low 1) high))))
(define (stream-filter pred stream)
(cond ((stream-null? stream) the-empty-stream)
((pred (stream-car stream))
(cons-stream (stream-car stream)
(stream-filter pred
(stream-cdr stream))))
(else (stream-filter pred (stream-cdr stream)))))
(define (memo-proc proc)
(let ((already-run? false) (result false))
(lambda ()
(if (not already-run?)
(begin (set! result (proc))
(set! already-run? true)
result)
result))))
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream))
; exercise 3.50
(define (stream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(cons-stream
(apply proc (map stream-car argstreams))
(apply stream-map
(cons proc (map stream-cdr argstreams))))))
;;;SECTION 3.5.2
(define (add-streams s1 s2)
(stream-map + s1 s2))
(define ones (cons-stream 1 ones))
(define integers (cons-stream 1 (add-streams ones integers)))
; exercise 3.54
(define (mul-streams s1 s2)
(stream-map * s1 s2))
; exercise 3.55
(define (partial-sums S)
(cons-stream (stream-car S) (add-streams (stream-cdr S)
(partial-sums S))))
; exercise 3.56
(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)))))))))
; print-stream-n
(define (print-stream-n S n)
(define (iter i)
(if (= i n)
'done
(begin (display (stream-ref S i))
(display " ")
(if (= (remainder (+ i 1) 5) 0)
(newline))
(iter (+ i 1)))))
(iter 0))
; exercise 3.59
(define (div-streams s1 s2)
(stream-map / s1 s2))
(define (integrate-series coff_stm)
(let ((integrate_s (cons-stream 1 ; 상수 c
(div-streams coff_stm integers))))
(stream-cdr integrate_s)))
(define exp-series
(cons-stream 1 (integrate-series exp-series)))
(define cosine-series
(cons-stream 1
(integrate-series (scale-stream sine-series -1))))
(define sine-series
(cons-stream 0
(integrate-series cosine-series)))
; exercise 3.60
(define (mul-series s1 s2)
(cons-stream (* (stream-car s1) (stream-car s2))
(add-streams (mul-series (stream-cdr s1) s2)
(scale-stream (stream-cdr s2) (stream-car s1)))))
; exercise 3.61
(define (invert-unit-series S)
(define X
(cons-stream 1
(scale-stream (mul-series (stream-cdr S) X) -1)))
X)
; exercise 3.62
; s1 / s2
(define (div-series s1 s2)
(if (= 0 (stream-car s2))
(error "Divide by zero" s2)
(mul-series s1 (invert-unit-series s2))))
; exercise 3.63
(define (sqrt-improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (sqrt-stream x)
(define guesses
(cons-stream 1.0
(stream-map (lambda (guess)
(sqrt-improve guess x))
guesses)))
guesses)
; exercise 3.64
(define (stream-limit stm tolerance)
(let ((element1 (stream-car stm)) ; 앞의 원소
(element2 (stream-car (stream-cdr stm)))) ; 뒤의 원소
(if (< (abs (- element1 element2)) tolerance)
element2
(stream-limit (stream-cdr stm) tolerance))))
(define (sqrt x tolerance)
(stream-limit (sqrt-stream x) tolerance))
; exercise 3.65
(define ln2-stream
(partial-sums (ln2-summands 1)))
(define (ln2-summands n)
(cons-stream (/ 1.0 n)
(stream-map - (ln2-summands (+ n 1)))))
(define (euler-transform s)
(let ((s0 (stream-ref s 0)) ; Sn-1
(s1 (stream-ref s 1)) ; Sn
(s2 (stream-ref s 2))) ; Sn+1
(cons-stream (- s2 (/ (square (- s2 s1))
(+ s0 (* -2 s1) s2)))
(euler-transform (stream-cdr s)))))
(define (make-tableau transform s)
(cons-stream s
(make-tableau transform
(transform s))))
(define (accelerated-sequence transform s)
(stream-map stream-car
(make-tableau transform s)))
(define (square x) (* x x))
; section 3.5.3
(define (interleave s1 s2)
(if (stream-null? s1)
s2
(cons-stream (stream-car s1)
(interleave s2 (stream-cdr s1)))))
(define (pairs s t)
(cons-stream
(list (stream-car s) (stream-car t))
(interleave
(stream-map (lambda (x) (list (stream-car s) x))
(stream-cdr t))
(pairs (stream-cdr s) (stream-cdr t)))))
; exercise 3.66
(define a (pairs integers integers))
; exercise 1.16
(define (i-fast-expt b n)
(fast-expt-iter 1 b n))
(define (fast-expt-iter a b n)
(cond ((= n 0) a)
((even? n) (fast-expt-iter a (square b) (/ n 2)))
(else (fast-expt-iter (* a b) b (- n 1)))))
(define (even? n)
(= (remainder n 2) 0))
; 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 (stream-cdr t) (stream-cdr u)))
(triples (stream-cdr s) (stream-cdr t) (stream-cdr u)))))
(define pythagorean_triples
(stream-filter (lambda (x)
(= (+ (square (car x)) (square (cadr x))) (square (caddr x))))
(triples integers integers integers)))
; execute
(define b (triples integers integers integers))
(print-stream-n b 10)
(print-stream-n pythagorean_triples 5)
- SICP Exercise 연습문제 3.72 (0)2008/07/27
- SICP Exercise 연습문제 3.71 (0)2008/07/27
- SICP Exercise 연습문제 3.70 (0)2008/07/27
- SICP Exercise 연습문제 3.69 (0)2008/07/27
- SICP Exercise 연습문제 3.68 (0)2008/07/26
- SICP Exercise 연습문제 3.67 (0)2008/07/25
- SICP Exercise 연습문제 3.66 (0)2008/07/25
글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.







댓글을 달아 주세요