計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.5(ストリーム)、3.5.2(無限ストリーム)、ストリームの暗黙の定義、問題 3.59.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.59.
コード(BBEdit, Emacs)
sample59.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(load "./stream.scm")
(define (enumerate-interval low high)
(if (> low high)
'()
(cons low
(enumerate-interval (+ low 1)
high))))
(define (test msg stream)
(print msg)
(for-each (lambda (n)
(print (stream-ref stream n)))
(enumerate-interval 0 9)))
;; a.
(define (integrate-series stream)
(stream-map / stream integers))
(define s (integrate-series ones))
;; b.
(define exp-series
(cons-stream 1 (integrate-series exp-series)))
(define cosine-series
(cons-stream 1
(scale-stream (integrate-series sine-series)
-1)))
(define sine-series
(cons-stream 0
(integrate-series cosine-series)))
(test "a." s)
(test "e^x" exp-series)
(test "余弦" cosine-series)
(test "正弦" sine-series)
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample59.scm a. 1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9 1/10 e^x 1 1 1/2 1/6 1/24 1/120 1/720 1/5040 1/40320 1/362880 余弦 1 0 -1/2 0 1/24 0 -1/720 0 1/40320 0 正弦 0 1 0 -1/6 0 1/120 0 -1/5040 0 1/362880 $
0 コメント:
コメントを投稿