開発環境
- 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.1(プログラミングの要素)、1.1.7(例: Newton法による平方根)、問題 1.8.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 1.8.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh ;; -*- coding: utf-8 -*- (define (square x) (* x x)) (define (good-enough? guess previous-guess) (< (abs (- 1 (/ guess previous-guess))) 0.001)) (define (cube-root-iter guess previous-guess x) (if (good-enough? guess previous-guess) guess (cube-root-iter (improve guess x) guess x))) (define (improve guess x) (/ (+ (/ x (square guess)) (* 2 guess)) 3)) (define (good-enough? guess previous-guess) (< (abs (- 1 (/ guess previous-guess))) 0.001)) (define (cube-root x) (cube-root-iter 1.0 10.0 x))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
$ gosh gosh> (load "./sample.scm") #t gosh> (cube-root 1) 1.0 gosh> (cube-root 8) 2.000000000012062 gosh> (cube-root 27) 3.0000005410641766 gosh> (cube-root 64) 4.000000000076121 gosh> (cube-root 125) 5.000000000287929 gosh> (cube-root 1000000000000000) 100000.0000002152 gosh> (cube-root 2) 1.2599210500177698 gosh> (exit) $
0 コメント:
コメントを投稿