計算機プログラムの構造と解釈[第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.61.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.61.
コード(BBEdit, Emacs)
sample61.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 stream)
(for-each (lambda (n)
(print (stream-ref stream n)))
(enumerate-interval 0 9)))
(define (mul-series s1 s2)
(cons-stream (* (stream-car s1)
(stream-car s2))
(add-streams (scale-stream (stream-cdr s2)
(stream-car s1))
(mul-series (stream-cdr s1)
s2))))
(define (invert-unit-series stream)
(cons-stream 1
(mul-series (stream-cdr stream)
(scale-stream (invert-unit-series stream)
-1))))
(define s1 (invert-unit-series ones))
(define s2 (invert-unit-series integers))
(test s1)
(print)
(test (mul-series s1 ones))
(print)
(test s2)
(print)
(test (mul-series s2 integers))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample61.scm 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 -2 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 $
0 コメント:
コメントを投稿