2014年4月30日水曜日

Scheme - データによる抽象の構築(記号データ(集合の表現(順序づけられたリストとしての集合(和集合))))

その他参考書籍

コード(BBEdit, Emacs)

sample.scm

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

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

(define (union-set set1 set2)
(cond ((null? set1) set2)
((null? set2) set1)
((= (car set1)
(car set2))
(union-set set1 (cdr set2)))
((< (car set1)
(car set2))
(cons (car set1)
(union-set (cdr set1)
set2)))
(else
(cons (car set2)
(union-set set1
(cdr set2))))))

(define set0 '())
(define set1 '(1))
(define set2 '(1 2 3 4 5))
(define set3 '(6 7 8 9 10))
(define set4 '(2 4 6 8 10))
(define set5 '(1 3 5 7 9))
(define sets (list set0 set1 set2 set3 set4 set5))

(for-each (lambda (pair)
(print "(union-set " (car pair) " " (cdr pair) ") = "
(union-set (car pair) (cdr pair))))
(flatmap (lambda (set1)
(map (lambda (set2)
(cons set1 set2))
sets))
sets))
```

```\$ ./sample.scm
(union-set () ()) = ()
(union-set () (1)) = (1)
(union-set () (1 2 3 4 5)) = (1 2 3 4 5)
(union-set () (6 7 8 9 10)) = (6 7 8 9 10)
(union-set () (2 4 6 8 10)) = (2 4 6 8 10)
(union-set () (1 3 5 7 9)) = (1 3 5 7 9)
(union-set (1) ()) = (1)
(union-set (1) (1)) = (1)
(union-set (1) (1 2 3 4 5)) = (1 2 3 4 5)
(union-set (1) (6 7 8 9 10)) = (1 6 7 8 9 10)
(union-set (1) (2 4 6 8 10)) = (1 2 4 6 8 10)
(union-set (1) (1 3 5 7 9)) = (1 3 5 7 9)
(union-set (1 2 3 4 5) ()) = (1 2 3 4 5)
(union-set (1 2 3 4 5) (1)) = (1 2 3 4 5)
(union-set (1 2 3 4 5) (1 2 3 4 5)) = (1 2 3 4 5)
(union-set (1 2 3 4 5) (6 7 8 9 10)) = (1 2 3 4 5 6 7 8 9 10)
(union-set (1 2 3 4 5) (2 4 6 8 10)) = (1 2 3 4 5 6 8 10)
(union-set (1 2 3 4 5) (1 3 5 7 9)) = (1 2 3 4 5 7 9)
(union-set (6 7 8 9 10) ()) = (6 7 8 9 10)
(union-set (6 7 8 9 10) (1)) = (1 6 7 8 9 10)
(union-set (6 7 8 9 10) (1 2 3 4 5)) = (1 2 3 4 5 6 7 8 9 10)
(union-set (6 7 8 9 10) (6 7 8 9 10)) = (6 7 8 9 10)
(union-set (6 7 8 9 10) (2 4 6 8 10)) = (2 4 6 7 8 9 10)
(union-set (6 7 8 9 10) (1 3 5 7 9)) = (1 3 5 6 7 8 9 10)
(union-set (2 4 6 8 10) ()) = (2 4 6 8 10)
(union-set (2 4 6 8 10) (1)) = (1 2 4 6 8 10)
(union-set (2 4 6 8 10) (1 2 3 4 5)) = (1 2 3 4 5 6 8 10)
(union-set (2 4 6 8 10) (6 7 8 9 10)) = (2 4 6 7 8 9 10)
(union-set (2 4 6 8 10) (2 4 6 8 10)) = (2 4 6 8 10)
(union-set (2 4 6 8 10) (1 3 5 7 9)) = (1 2 3 4 5 6 7 8 9 10)
(union-set (1 3 5 7 9) ()) = (1 3 5 7 9)
(union-set (1 3 5 7 9) (1)) = (1 3 5 7 9)
(union-set (1 3 5 7 9) (1 2 3 4 5)) = (1 2 3 4 5 7 9)
(union-set (1 3 5 7 9) (6 7 8 9 10)) = (1 3 5 6 7 8 9 10)
(union-set (1 3 5 7 9) (2 4 6 8 10)) = (1 2 3 4 5 6 7 8 9 10)
(union-set (1 3 5 7 9) (1 3 5 7 9)) = (1 3 5 7 9)
\$
```