開発環境
- 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.44.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 1.44.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define (compose f g)
(lambda (x)
(f (g x))))
(define (repeated f n)
(define (iter k result)
(if (= k 0)
result
(iter (- k 1)
(compose f result))))
(iter n
(lambda (x) x)))
(define (smooth f)
(define dx 0.00001)
(lambda (x)
(/ (+ (f (- x dx))
(f x)
(f (+ x dx)))
3)))
(define (n-fold-smoothed f n)
((repeated smooth n) f))
(for-each (lambda (n)
(print n
": "
((n-fold-smoothed (lambda (x)
(if (< x 0)
0.0
1.0))
n)
0)))
'(0 1 2 3 4 5 6 7 8 9 10))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample.scm 0: 1.0 1: 0.6666666666666666 2: 0.6666666666666666 3: 0.6296296296296297 4: 0.6172839506172839 5: 0.6049382716049383 6: 0.5953360768175583 7: 0.5866483767718336 8: 0.578875171467764 9: 0.5718132398516486 10: 0.5653948415722535 $
0 コメント:
コメントを投稿