2014年5月2日金曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.3(記号データ)、2.3.3(例: 集合の表現)、二進木としての集合、問題 2.65.を解いてみる。

その他参考書籍

問題 2.65.

コード(BBEdit, Emacs)

sample.scm

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

;; これまでに書いた手続き
(load "./tree.scm")

(define (union-set set1 set2)
  (define (union-list list1 list2)
    (cond ((null? list1) list2)
          ((null? list2) list1)
          ((equal? (car list1)
                   (car list2))
           (cons (car list1)
                 (union-list (cdr list1)
                             (cdr list2))))
          ((< (car list1)
              (car list2))
           (cons (car list1)
                 (union-list (cdr list1)
                             list2)))
          (else
           (cons (car list2)
                 (union-list list1
                             (cdr list2))))))
  (list->tree (union-list (tree->list set1)
                          (tree->list set2))))

(define (intersection-set set1 set2)
  (define (intersection-list list1 list2)
    (cond ((or (null? list1)
               (null? list2))
           '())
          ((equal? (car list1)
                   (car list2))
           (cons (car list1)
                 (intersection-list (cdr list1)
                                    (cdr list2))))
          ((< (car list1)
              (car list2))
           (intersection-list (cdr list1)
                              list2))
          (else
           (intersection-list list1
                              (cdr list2)))))
  (list->tree (intersection-list (tree->list set1)
                                 (tree->list set2))))

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

(for-each (lambda (pair)
            (let ((set1 (car pair))
                  (set2 (cdr pair)))
              (print "--------------------------------------------------")
              (print "set1")
              (print (tree->list set1))
              (print "set2")
              (print (tree->list set2))
              (print "union")
              (print (tree->list (union-set set1
                                            set2)))
              (print "intersection")
              (print (tree->list (intersection-set set1
                                                   set2)))))
          (flatmap (lambda (set1)
                     (map (lambda (set2)
                            (cons set1
                                  set2))
                          sets))
                   sets))

入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))

$ ./sample.scm
--------------------------------------------------
set1
()
set2
()
union
()
intersection
()
--------------------------------------------------
set1
()
set2
(1)
union
(1)
intersection
()
--------------------------------------------------
set1
()
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
()
--------------------------------------------------
set1
()
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
()
--------------------------------------------------
set1
()
set2
(1 3 5 7 9)
union
(1 3 5 7 9)
intersection
()
--------------------------------------------------
set1
()
set2
(2 4 6 8 10)
union
(2 4 6 8 10)
intersection
()
--------------------------------------------------
set1
(1)
set2
()
union
(1)
intersection
()
--------------------------------------------------
set1
(1)
set2
(1)
union
(1)
intersection
(1)
--------------------------------------------------
set1
(1)
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
(1)
--------------------------------------------------
set1
(1)
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
(1)
--------------------------------------------------
set1
(1)
set2
(1 3 5 7 9)
union
(1 3 5 7 9)
intersection
(1)
--------------------------------------------------
set1
(1)
set2
(2 4 6 8 10)
union
(1 2 4 6 8 10)
intersection
()
--------------------------------------------------
set1
(1 2 3 4 5)
set2
()
union
(1 2 3 4 5)
intersection
()
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1)
union
(1 2 3 4 5)
intersection
(1)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
(1 2 3 4 5)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
(1 2 3 4 5)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1 3 5 7 9)
union
(1 2 3 4 5 7 9)
intersection
(1 3 5)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(2 4 6 8 10)
union
(1 2 3 4 5 6 8 10)
intersection
(2 4)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
()
union
(1 2 3 4 5)
intersection
()
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1)
union
(1 2 3 4 5)
intersection
(1)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
(1 2 3 4 5)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1 2 3 4 5)
union
(1 2 3 4 5)
intersection
(1 2 3 4 5)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(1 3 5 7 9)
union
(1 2 3 4 5 7 9)
intersection
(1 3 5)
--------------------------------------------------
set1
(1 2 3 4 5)
set2
(2 4 6 8 10)
union
(1 2 3 4 5 6 8 10)
intersection
(2 4)
--------------------------------------------------
set1
(1 3 5 7 9)
set2
()
union
(1 3 5 7 9)
intersection
()
--------------------------------------------------
set1
(1 3 5 7 9)
set2
(1)
union
(1 3 5 7 9)
intersection
(1)
--------------------------------------------------
set1
(1 3 5 7 9)
set2
(1 2 3 4 5)
union
(1 2 3 4 5 7 9)
intersection
(1 3 5)
--------------------------------------------------
set1
(1 3 5 7 9)
set2
(1 2 3 4 5)
union
(1 2 3 4 5 7 9)
intersection
(1 3 5)
--------------------------------------------------
set1
(1 3 5 7 9)
set2
(1 3 5 7 9)
union
(1 3 5 7 9)
intersection
(1 3 5 7 9)
--------------------------------------------------
set1
(1 3 5 7 9)
set2
(2 4 6 8 10)
union
(1 2 3 4 5 6 7 8 9 10)
intersection
()
--------------------------------------------------
set1
(2 4 6 8 10)
set2
()
union
(2 4 6 8 10)
intersection
()
--------------------------------------------------
set1
(2 4 6 8 10)
set2
(1)
union
(1 2 4 6 8 10)
intersection
()
--------------------------------------------------
set1
(2 4 6 8 10)
set2
(1 2 3 4 5)
union
(1 2 3 4 5 6 8 10)
intersection
(2 4)
--------------------------------------------------
set1
(2 4 6 8 10)
set2
(1 2 3 4 5)
union
(1 2 3 4 5 6 8 10)
intersection
(2 4)
--------------------------------------------------
set1
(2 4 6 8 10)
set2
(1 3 5 7 9)
union
(1 2 3 4 5 6 7 8 9 10)
intersection
()
--------------------------------------------------
set1
(2 4 6 8 10)
set2
(2 4 6 8 10)
union
(2 4 6 8 10)
intersection
(2 4 6 8 10)
$

0 コメント:

コメントを投稿