計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - 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))の1(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.3(一般方法としての手続き)、問題 1.36.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題 1.36.
コード(BBEdit, Emacs)
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define tolerance 0.00001)
(define fixed-point
(lambda (f first-guess)
(define close-enough?
(lambda (v1 v2)
(< (abs (- v1 v2)) tolerance)))
(define try
(lambda (guess)
(display guess)
(newline)
((lambda (next)
(if (close-enough? guess next)
next
(try next)))
(f guess))))
(try first-guess)))
(print "平均緩和を使わなかった場合")
(fixed-point (lambda (x)
(/ (log 1000)
(log x)))
2.0)
(print "平均緩和を使った場合")
(define average (lambda (x y) (/ (+ x y) 2)))
(fixed-point (lambda (x)
(average (/ (log 1000)
(log x))
x))
2.0)
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample36.scm 平均緩和を使わなかった場合 2.0 9.965784284662087 3.004472209841214 6.279195757507157 3.759850702401539 5.215843784925895 4.182207192401397 4.8277650983445906 4.387593384662677 4.671250085763899 4.481403616895052 4.6053657460929 4.5230849678718865 4.577114682047341 4.541382480151454 4.564903245230833 4.549372679303342 4.559606491913287 4.552853875788271 4.557305529748263 4.554369064436181 4.556305311532999 4.555028263573554 4.555870396702851 4.555315001192079 4.5556812635433275 4.555439715736846 4.555599009998291 4.555493957531389 4.555563237292884 4.555517548417651 4.555547679306398 4.555527808516254 4.555540912917957 平均緩和を使った場合 2.0 5.9828921423310435 4.922168721308343 4.628224318195455 4.568346513136242 4.5577305909237005 4.555909809045131 4.555599411610624 4.5555465521473675 $
0 コメント:
コメントを投稿