EoPL reading (56) 2.2 An Abstraction for Inductive Data Type

昨晩のヤリ残しな試験を書いた。以下な部分。

                       (and (pair? variant)
                         (memq (car variant) (car type-info)) #t)

試験は以下。

(test* "argument is not pair"
       #f
       (bintree? 1))
(test* "car of argument is not 'leaf-node and 'interior-node"
       #f
       (bintree? '(x)))

あら? 上記は以下の通りパスしてるんですが

test argument is not pair, expects #f ==> ok
test car of argument is not 'leaf-node and 'interior-node, expects #f ==> ok

上記の試験、ってか実装を見るに述語な手続きは car がナニだったら OK なのか。試験を書いてみました。

(test* "car of argument is 'leaf-node"
       #t
       (bintree? '(leaf-node)))
(test* "car of argument is 'interior-node"
       #t
       (bintree? '(interior-node)))

試験パス。

test car of argument is 'leaf-node, expects #t ==> ok
test car of argument is 'interior-node, expects #t ==> ok

なんか微妙。ただ、オブジェクト生成時にきちんとチェック入ってるのでこの程度で OK なのか。このあたりの整合性のチェックとかも define-syntax だと楽なのか。

とりあえず中のデータとかはスルーで読み進めてみます。以下の定義をニラむ。

(add-load-path ".")
(load "define-datatype")

(define-datatype s-list s-list?
  (empty-s-list)
  (non-empty-s-list
   (first symbol-exp?)
   (rest s-list?)))

(define-datatype symbol-exp symbol-exp?
  (symbol-symbol-exp
   (data symbol?))
  (s-list-symbol-exp
   (data s-list?)))

相互に依存してますな。とりあえず正常ケイスの試験を書いてみる。

(use gauche.test)

(add-load-path ".")
(load "define-datatype")
(load "s-list")

(test-start "symbol-exp")
(test-section "s-list")
(test* "(empty-s-list) is empty-s-list"
       #t
       (s-list? '(empty-s-list)))
(test* "(non-empty-s-list (symbol-symbol-exp a) (empty-s-list)) is non-empty-s-list"
       #t
       (s-list? '(non-empty-s-list (symbol-symbol-exp a) (empty-s-list))))

(test-section "s-list-symbol-exp")
(test* "(symbol-symbol-exp a) is symbol-symbol-exp"
       #t
       (symbol-exp? '(symbol-symbol-exp a)))
(test* "(s-list-symbol-exp (empty-s-list)) is symbol-symbol-exp"
       #t
       (symbol-exp? '(s-list-symbol-exp (empty-s-list))))

(test-end)

でもこれって、述語に渡すリストの car が (ry
とほほほ。

自分メモ
# テキスト持ち歩いてないので
以下なナニは何だ

(define-datatype s-list s-list?
  (an-s-list
    (data (list-of (symbol-exp?)))))
(define list-of
  (lambda (pred)
    (lambda (val)
      (or (null? val)
          (and (pair? val)
               (pred (car val))
               ((list-of pred) (cdr val)))))))

高階手続き、というヤツですな。