開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の1(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.4(値として返される手続き)、Newton法、抽象と第一級手続き、問題 1.46.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 1.46.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh ;; -*- coding: utf-8 -*- ;; これまでに書いた手続き (load "./procedures.scm") ;; 問題 1.46 ;; 反復改良法(iterative improvement) (define (iterative-improve good-enough? improve) (define (iter guess) (if (good-enough? guess) guess (iter (improve guess)))) iter) (define (sqrt x) (define (good-enough? guess) (< (abs (- x (square guess))) 0.00001)) (define (improve guess) (average guess (/ x guess))) ((iterative-improve good-enough? improve) x)) (print "√2 = " (sqrt 2.0)) (print "√4 = " (sqrt 4.0)) (define (fixed-point f first-guess) (define (good-enough? guess) (< (abs (- (f guess) guess)) 0.00001)) ((iterative-improve good-enough? f) first-guess)) (print "余弦関数の不動点: " (fixed-point cos 1.0)) (print "y = sin y + cos y の解: " (fixed-point (lambda (y) (+ (sin y) (cos y))) 1.0))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
gosh> √2 = 1.4142156862745097 √4 = 2.0000000929222947 余弦関数の不動点: 0.7390893414033928 y = sin y + cos y の解: 1.2587228743052672 #t gosh>
0 コメント:
コメントを投稿