計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- kscheme, Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の1(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.4(値として返される手続き)、問題 1.46.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題 1.46.
コード(BBEdit, Emacs)
(define iterative-improve
(lambda (enough? improve)
(define iter
(lambda (guess)
(if (enough? guess)
guess
(iter (improve guess)))))
iter))
(define average (lambda (x y) (/ (+ x y) 2)))
(define abs (lambda (x) (if (< x 0)
(* -1 x)
x)))
(define square (lambda (x) (* x x)))
(define sqrt1
(lambda (x)
((iterative-improve (lambda (guess)
(< (abs (- (square guess) x))
0.001))
(lambda (guess)
(average guess (/ x guess))))
1.0)))
(define tolerance 0.00001)
(define fixed-point
(lambda (f first-guess)
((iterative-improve (lambda (guess)
(< (abs (- guess (f guess))) tolerance))
f)
first-guess)))
(define sqrt2
(lambda (x)
(fixed-point (lambda (y) (average y (/ x y)))
1.0)))
(sqrt1 2.0)
(sqrt2 2.0)
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ kscheme < sample46.scm In : Out: iterative-improve In : Out: average In : Out: abs In : Out: square In : Out: sqrt1 In : Out: tolerance In : Out: fixed-point In : Out: sqrt2 In : Out: 0.141421568627450980389e1 In : Out: 0.141421568627450980389e1 In : $
0 コメント:
コメントを投稿