計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第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-a, b.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題 1.31-a, b.
コード(BBEdit, Emacs)
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define square (lambda (x) (* x x)))
(define identity (lambda (x) x))
;; a.再帰的プロセス
(define product-1
(lambda (term a next b)
(if (> a b)
1
(* (term a)
(product-1 term (next a) next b)))))
(define factorial-1
(lambda (n)
(product-1 identity
1
(lambda (x) (+ x 1))
n)))
(define pi-product-1
(lambda (n)
(* 4.0
(product-1 (lambda (x)
(/ (* x (+ x 2))
(square (+ x 1))))
2
(lambda (x) (+ x 2))
n))))
(print (factorial-1 10))
(print (pi-product-1 1000))
;; b.反復的プロセス
(define product
(lambda (term a next b)
(define inner
(lambda (a result)
(if (> a b)
result
(inner (next a)
(* (term a) result)))))
(inner a 1)))
(define factorial
(lambda (n)
(product identity
1
(lambda (x) (+ x 1))
n)))
(define pi-product
(lambda (n)
(* 4.0
(product (lambda (x)
(/ (* x (+ x 2))
(square (+ x 1))))
2
(lambda (x) (+ x 2))
n))))
(print (factorial 10))
(print (pi-product 1000))
(print (pi-product 5000))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample31.scm 3628800 3.1431607055322663 3628800 3.1431607055322663 3.1419067029355268 $
0 コメント:
コメントを投稿