開発環境
- 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.1(引数としての手続き)、問題 1.31-a.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 1.31-a.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh ;; -*- coding: utf-8 -*- (define (product term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* (term a) result)))) (iter a 1)) (define (factorial n) (product id 1 inc n)) (define (pi-product a b) (define (pi-term x) (/ (* (- x 1.0) (+ x 1.0)) (square x))) (define (pi-next x) (+ x 2)) (product pi-term a pi-next b)) ;; 合成手続き (define (id x) x) (define (inc n) (+ n 1)) (define (square x) (* x x)) ;; (print "10! = " (factorial 10)) (print "πの近似値") (for-each (lambda (x) (print (* 4 (pi-product 3 x)))) '(100 1000 10000 100000 1000000))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample.scm 10! = 3628800 πの近似値 3.1573396892175642 3.1431638424192028 3.141749737149286 3.1416083615923225 3.1415942243865067 $
0 コメント:
コメントを投稿