開発環境
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第1章(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.1(引数としての手続き)、問題1.31.を取り組んでみる。
その他参考書籍
問題1.31.
コード(Emacs)
(begin (load "procedures.scm") (newline) (define (p x) (display x) (newline)) (define (product term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b)))) (define (id x) x) (define (inc x) (+ x 1)) (define (factorial n) (product id 1 inc n)) (p (map factorial '(0 1 10))) (define (pi n) (* 4.0 (product (lambda (n) (/ (* (+ (* 2 n) 2) (+ (* 2 n) 4)) (square (+ (* 2 n) 3)))) 0 inc n))) (for-each (lambda (n) (p (pi n))) '(1 10 100)) (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)) (p (map factorial '(0 1 10))) (define (pi n) (* 4.0 (product (lambda (n) (/ (* (+ (* 2 n) 2) (+ (* 2 n) 4)) (square (+ (* 2 n) 3)))) 0 inc n))) (for-each (lambda (n) (p (pi n))) '(1 10 100)) 'done)
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ ksi < sample31.scm ksi> (1 1 3628800) 3.413333333333334 3.207709732466548 3.149302048638203 (1 1 3628800) 3.413333333333334 3.207709732466548 3.149302048638203 => done ksi> $
0 コメント:
コメントを投稿