EoPL reading (59) 2.2 An Abstraction for Inductive Data Type
Exercise 2.5
短時間な現実トウヒの中で、以下なナニがでっち上がりました。
(add-load-path ".") (load "define-datatype") (load "bintree") (load "leaf-sum") (define max-interior (lambda (tree) (define (max-interior-inner rslt t) (let ((x (leaf-sum t))) (cases bintree t (leaf-node (datum) rslt) (interior-node (key left right) (let ((ret (if (or (> x (cadr rslt)) (not (car rslt))) (list key x) rslt))) (if (and (eq? 'leaf-node (car left)) (eq? 'leaf-node (car right))) ret (max-interior-inner (if (eq? 'leaf-node (car left)) ret (max-interior-inner ret left)) right)))))) ) (car (max-interior-inner '(#f 999) tree)) ) )
なんか不満。ここに至るまでも相当な紆余曲折があったんですが、上記も相当微妙。もう少し検討してみます。現時点で以下な試験にパスしてます。
(use gauche.test) (add-load-path ".") (load "bintree") (load "max-interior") (define tree-a (interior-node 'a (leaf-node 2) (leaf-node 3))) (define tree-b (interior-node 'b (leaf-node -1) tree-a)) (define tree-c (interior-node 'c tree-b (leaf-node 1))) (define tree-d (interior-node 'd (leaf-node -2) (leaf-node -3))) (define tree-e (interior-node 'e tree-d tree-c)) (define tree-f (interior-node 'f (leaf-node -1) (leaf-node -5))) (define tree-g (interior-node 'g tree-d tree-f)) (test-start "max-interior") (test-section "max-interior") (test* "sample1" 'a (max-interior tree-b)) (test* "sample2" 'c (max-interior tree-c)) (test* "tree-e" 'c (max-interior tree-e)) (test* "tree-g" 'd (max-interior tree-g)) (test-end)
上記の試験もなんとなく微妙。