## 2016年10月25日火曜日

### Scheme - データによる抽象の構築(階層データ構造と閉包性(並びの表現(last-pair?、逆順、両替計算プログラム、可変長引数、ドット末尾記法(dotted-tail notation)、パリティ(偶奇性))))

その他参考書籍

コード(Emacs)

```(begin
(newline)

(define (p obj) (display obj) (newline))

(define (last-pair list)
(if (null? (cdr list))
list
(last-pair (cdr list))))
(define (reverse list)
(define (iter list result)
(if (null? list)
result
(iter (cdr list) (cons (car list) result))))
(iter list '()))

(p (last-pair (list 23 72 149 34)))
(p (reverse (list 1 4 9 16 25)))

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

(define (first-denomination coin-values) (car coin-values))
(define (except-first-denomination coin-values) (cdr coin-values))

(define (no-more? coin-values) (null? coin-values))

(define (cc amount coin-values)
(if (zero? amount)
1
(if (or (< amount 0) (no-more? coin-values))
0
(+ (cc amount
(except-first-denomination coin-values))
(cc (- amount (first-denomination coin-values))
coin-values)))))

(p (cc 50 us-coins))
(p (cc 10 uk-coins))

(define (same-parity n . rest)
(define (iter parity? rest)
(if (null? rest)
'()
(if (parity? (car rest))
(cons (car rest) (iter parity? (cdr rest)))
(iter parity? (cdr rest)))))
(cons n (iter (if (even? n) even? odd?) rest)))

(p (same-parity 1 2 3 4 5 6 7))
(p (same-parity 2 3 4 5 6 7))

'done)
```

```\$ ksi < sample17.scm
ksi>
(34)
(25 16 9 4 1)
50
50
(1 3 5 7)
(2 4 6)
=> done
ksi> \$
```