試験を検討

予習しつつ試験を書いてみます。

問題 2.59

union-set 演算との事。二つの集合の和集合との事。最初の引数に 2 番目の引数の要素を追加していくイメージで以下?

(use gauche.test)

(add-load-path ".")
(load "2.59")

(test-start "union-set")
(test-section "union-set")
(test* "(union-set '() '())"
       '()
       (union-set '() '()))
(test* "(union-set '(1 2 3) '())"
       '(1 2 3)
       (union-set '(1 2 3) '()))
(test* "(union-set '() '(1 2 3))"
       '(3 2 1)
       (union-set '() '(1 2 3)))
(test* "(union-set '(5 1 3) '(4 6 2))"
       '(2 6 4 5 1 3)
       (union-set '(5 1 3) '(4 6 2)))
(test* "(union-set '(7 5 2 1 4 6 3) '(5 8 7 4 9 6 10))"
       '(10 9 8 7 5 2 1 4 6 3)
       (union-set '(7 5 2 1 4 6 3) '(5 8 7 4 9 6 10)))
(test-end)

adjoin-set を使うとすると 2 番目の引数で追加される要素は逆順になりますな。

問題2.60

スルー。ここはどーゆー話になるかが楽しみ。

問題2.61

sort 済みだと話が早いって例か。element-of-set? は以下な形で例示されている。

(define (element-of-set? x set)
  (cond ((null? set) #f)
        ((= x (car set)) #t)
        ((< x (car set)) #f)
        (else
         (element-of-set? x (cdr set)))))

上記を踏襲すれば良いのだろうな。試験を以下に。

(use gauche.test)

(add-load-path ".")
(load "2.61")

(test-start "adjoin-set")
(test-section "adjoin-set")
(test* "(adjoin-set 1 '())"
       '(1)
       (adjoin-set 1 '()))
(test* "(adjoin-set 1 '(1 2 3))"
       '(1 2 3)
       (adjoin-set 1 '(1 2 3)))
(test* "(adjoin-set 2 '(1 2 3))"
       '(1 2 3)
       (adjoin-set 2 '(1 2 3)))
(test* "(adjoin-set 3 '(1 2 3))"
       '(1 2 3)
       (adjoin-set 3 '(1 2 3)))
(test* "(adjoin-set 4 '(1 2 3))"
       '(1 2 3 4)
       (adjoin-set 4 '(1 2 3)))
(test* "(adjoin-set 3 '(1 2 4))"
       '(1 2 3 4)
       (adjoin-set 3 '(1 2 4)))
(test* "(adjoin-set 2 '(1 3 4))"
       '(1 2 3 4)
       (adjoin-set 2 '(1 3 4)))
(test* "(adjoin-set 1 '(2 3 4))"
       '(1 2 3 4)
       (adjoin-set 1 '(2 3 4)))
(test-end)

むむ。なんとなく微妙なカンジがしますがスルー。

問題 2.62

この試験は問題 2.59 を流用すれば OK なのかなぁ。あ、順序づけられたリスト、だからちょっと話が違いますな。

(use gauche.test)

(add-load-path ".")
(load "2.59")

(test-start "union-set")
(test-section "union-set")
(test* "(union-set '() '())"
       '()
       (union-set '() '()))
(test* "(union-set '(1 2 3) '())"
       '(1 2 3)
       (union-set '(1 2 3) '()))
(test* "(union-set '() '(1 2 3))"
       '(1 2 3)
       (union-set '() '(1 2 3)))
(test* "(union-set '(1 3 5) '(2 4 6))"
       '(1 2 3 4 5 6)
       (union-set '(1 3 5) '(2 4 6)))
(test* "(union-set '(1 2 3 4 5 6 7) '(4 5 6 7 8 9 10))"
       '(1 2 3 4 5 6 7 8 9 10)
       (union-set '(1 2 3 4 5 6 7) '(4 5 6 7 8 9 10)))
(test-end)

なのか。これもどんな答えがとび出すかを楽しみにする事に (を

二進木

は時間があれば色々確認させて頂きながら、な方向で。