計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs(Text Editor)
- Scheme (プログラミング言語)
- kscheme, Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.2(階層データ構造と閉包性)、2.2.3(公認インターフェースとしての並び)、写像の入れ子、問題2.41.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題2.41.
コード(Emacs)
(define map
(lambda (proc items)
(if (null? items)
'()
(cons (proc (car items))
(map proc (cdr items))))))
(define enumerate-interval
(lambda (low high)
(if (< high low)
'()
(cons low
(enumerate-interval (+ low 1) high)))))
(define filter
(lambda (predicate sequence)
(cond ((null? sequence) '())
((predicate (car sequence))
(cons (car sequence)
(filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence))))))
(define accumulate
(lambda (op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence))))))
(define flatmap
(lambda (proc seq)
(accumulate append '() (map proc seq))))
(define ordered-triples
(lambda (n s)
(filter (lambda (triple)
(= (+ (+ (car triple)
(cadr triple))
(caddr triple))
s))
(flatmap (lambda (k)
(flatmap (lambda (j)
(map (lambda (i)
(list i j k))
(enumerate-interval 1 j)))
(enumerate-interval 1 k)))
(enumerate-interval 1 n)))))
(define print-triples
(lambda (triples)
(if (null? triples)
(newline)
(begin (newline)
(display (car triples))
(print-triples (cdr triples))))))
(print-triples (ordered-triples 10 10))
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ kscheme < sample41.scm kscm> kscm> kscm> kscm> kscm> kscm> kscm> kscm> (3 3 4) (2 4 4) (2 3 5) (1 4 5) (2 2 6) (1 3 6) (1 2 7) (1 1 8) #<undefined> kscm> $
0 コメント:
コメントを投稿