開発環境
- 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))の2(データによる抽象の構築)、2.2(階層データ構造と閉包性)、2.2.2(階層構造)、木の写像、問題 2.30, 2.31, 2.32.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 2.30, 2.31, 2.32.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
;; これまでに書いた手続き
(load "./procedures.scm")
;; 2.31
(define (square-tree1 tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree1 sub-tree)
(square sub-tree)))
tree))
(print "square-tree1: "
(square-tree1 (list 1
(list 2 (list 3 4) 5)
(list 6 7))))
;; 2.32
(define (tree-map proc tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map proc sub-tree)
(proc sub-tree)))
tree))
(define (square-tree tree)
(tree-map square tree))
(print "square-tree : "
(square-tree (list 1
(list 2 (list 3 4) 5)
(list 6 7))))
;; 2.33
(define (subsets s)
(if (null? s)
(list nil)
((lambda (rest)
(append rest ;先頭の要素を含まない部分集合全て
(map (lambda (items)
(cons (car s)
items)) ;先頭の要素を含む部分集合全て
rest)))
(subsets (cdr s)))))
(define s '(1 2 3))
(print "set: " s)
(print "subsets: " (subsets s))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample.scm square-tree1: (1 (4 (9 16) 25) (36 49)) square-tree : (1 (4 (9 16) 25) (36 49)) set: (1 2 3) subsets: (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) $
0 コメント:
コメントを投稿