我定义了以下内容:
(struct type (parent dirty) #:mutable #:transparent)
(define types (make-hash))
(define (add-key predicate parent)
(begin
(hash-ref! types parent (type empty #t)) ;;if the parent doesn't exist, is created with no parent.
(let([node (hash-ref types predicate #f)])
(if(or (boolean? node) ;;the node is not on the list
(not(equal? (type-parent node) parent))) ;;the node has a different parent
(hash-set! types predicate (type parent #t))
(printf "nothing to do\n")
))))
(define (ancestor? predicate1 predicate2)
(let ([node (hash-ref types predicate2 #f)])
(cond [(false? node)(error "following predicate is not in types: " predicate2)]
[(empty? (type-parent node)) #f]
[(equal? (type-parent node) predicate1) #t]
[else (ancestor? predicate1 (type-parent node))])))
它似乎工作得很好,我可以做这样的事情:
> (ancestor? integer? even?)
#t
> (ancestor? list? even?)
#f
> (ancestor? integer? odd?)
#t
>
我似乎只有一个问题,sort
因为(sort '(integer? odd? number? list? even?) ancestor?)
抛出了以下错误:following predicate is not in types: integer?
当然,这是在我的实现中定义的。问题是我确信键值对存在,我可以操纵它,我可以手动运行每一行代码ancestor
......我真的很困惑可能导致这种情况......有什么想法吗?