## 2016年10月30日日曜日

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

その他参考書籍

コード(Emacs)

```(begin
(newline)
(define (p obj) (display obj) (newline))

(p '2.30)
(define tree (list 1
(list 2 (list 3 4) 5)
(list 6 7)))
(define (square-tree-1 tree)
(if (null? tree)
'()
(if (pair? tree)
(cons (square-tree-1 (car tree))
(square-tree-1 (cdr tree)))
(square tree))))
(p (square-tree-1 tree))

(define (square-tree-2 tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree-2 sub-tree)
(square sub-tree)))
tree))
(p (square-tree-2 tree))

(p '2.31)
(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))
(p (square-tree tree))

(p '2.32)
(define s (list 1 2 3))
(define (subsets s)
(if (null? s)
(list '())
((lambda (rest)
(append rest
(map (lambda (subset)
(cons (car s) subset))
rest)))
(subsets (cdr s)))))
(p (subsets s))

'done)
```

```\$ ksi < sample30.scm
ksi>
2.3
(1 (4 (9 16) 25) (36 49))
(1 (4 (9 16) 25) (36 49))
2.31
(1 (4 (9 16) 25) (36 49))
2.32
(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
=> done
ksi> \$
```