計算機プログラムの構造と解釈[第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.32-a, b.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題 1.32-a, b.
コード(BBEdit, Emacs)
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define square (lambda (x) (* x x)))
(define identity (lambda (x) x))
(define inc (lambda (n) (+ n 1)))
;; a.再帰的プロセス
(define accumulate-1
(lambda (combiner null-value term a next b)
(define inner
(lambda (x)
(if (> x b)
null-value
(combiner (term x)
(inner (next x))))))
(inner a)))
(define product-1
(lambda (term a next b)
(accumulate-1 * 1 term a next b)))
(define factorial-1
(lambda (n)
(product-1 identity
1
inc
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 accumulate
(lambda (combiner null-value term a next b)
(define inner
(lambda (x result)
(if (> x b)
result
(inner (next x)
(combiner (term x)
result)))))
(inner a null-value)))
(define product
(lambda (term a next b)
(accumulate * 1 term a next b)))
(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))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample32.scm 3628800 3.1431607055322663 3628800 3.1431607055322663 $
0 コメント:
コメントを投稿