開発環境
- 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.40.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 1.40.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
;; これまでに書いた手続き
(load "./compound_procedures.scm")
(define (derive g)
(define dx 0.0001)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
(define (newton-transform g)
(lambda (x)
(- x
(/ (g x)
((derive g) x)))))
(define (newtons-method g guess)
(fixed-point (newton-transform g) guess))
(define (cubic a b c)
(lambda (x)
(+ (cube x)
(* a
(square x))
(* b x)
c)))
(define a (cubic 0 0 0))
(define b (cubic 1 2 3))
(define c (cubic 2 3 4))
(define d (cubic 1 3 5))
(define e (cubic 2 4 6))
(define x1 (newtons-method a 1))
(define x2 (newtons-method b 1))
(define x3 (newtons-method c 1))
(define x4 (newtons-method d 1))
(define x5 (newtons-method e 1))
(print x1 ": " (a x1))
(print x2 ": " (b x2))
(print x3 ": " (c x3))
(print x4 ": " (d x4))
(print x5 ": " (e x5))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample.scm 6.456847381580125e-5: 2.6919163715398973e-13 -1.2756822036415494: 4.086331273356336e-11 -1.6506291909254565: 2.349291428060951e-9 -1.4026279408451816: 2.078669680827261e-9 -1.7113457392237208: 2.9196645101592367e-10 $
一応Wolfram|Alpha: Computational Knowledge Engineで確認。
0 コメント:
コメントを投稿