## 2014年3月29日土曜日

### Scheme - データによる抽象の構築(階層データ構造と閉包性(階層構造(木の写像)))

その他参考書籍

コード(BBEdit, Emacs)

sample.scm

```#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

;; これまでに書いた手続き

;; 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))
```

```\$ ./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))
\$
```