이 문제는 스트림(stream)에 관한 문제입니다.
map 프로시저를 여러 스트림을 인자로 받아 쓸 수 있게 하는 함수를 만드는 문제입니다.
이미 틀이 만들어져있고, 빈칸을 알맞게 쓰면 되는 문제입니다.^^

문제 없이 잘 되는군요.^^
처음에 apply와 map 프로시저가 기억이 나지 않아 고생하였습니다.
apply 프로시저는 프로시저를 받아
인자로 받은 리스트 각각에 프로시저를 적용하여 값을 내놓습니다.
예를 들어 (apply + (list 1 2 3 4))는 10을 내놓습니다.
map 프로시저는 리스트 각각에 프로시저를 적용하여 리스트를 다시 내놓습니다.
예를 들어 (map + (list 1 2 3 4) (list 50 60 70 80))은 (list 51 62 73 84)와 같습니다.
여기에 맞춰 프로시저를 작성하였습니다.
참조
해럴드 애빌슨, 김재우 역, <컴퓨터 프로그램의 구조와 해석>, 인사이트, 2007, pp. 422
; 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-map proc s)
; (if (stream-null? s)
; the-empty-stream
; (cons-stream (proc (stream-car s))
; (stream-map proc (stream-cdr s)))))
(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))))
; 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))))))
; execute
(define A (stream-enumerate-interval 1 5))
(define B (stream-enumerate-interval 6 10))
(define C (stream-enumerate-interval 11 15))
(display-stream A)
(display-stream B)
(display-stream C)
(newline)
(display-stream (stream-map + A B C))
- SICP Exercise 연습문제 3.53 (0)2008/07/19
- SICP Exercise 연습문제 3.52 (0)2008/07/19
- SICP Exercise 연습문제 3.51 (0)2008/07/19
- SICP Exercise 연습문제 3.50 (0)2008/07/19
- SICP Exercise 연습문제 3.48 (0)2008/07/18
- SICP Exercise 연습문제 3.47 (0)2008/07/18
- SICP Exercise 연습문제 3.46 (0)2008/07/18
글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.








댓글을 달아 주세요