0

我定义了以下内容:

    (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......我真的很困惑可能导致这种情况......有什么想法吗?

4

1 回答 1

2
  1. 我按原样获取了您的代码并将其放入文件中。

  2. 为 添加了一条跟踪线ancestor?:添加第一行(displayln `(ancestor? ,predicate1 ,predicate2))或添加(trace ancestor?)(在 a 之后(require racket/trace))。

  3. 这显示了代码中损坏的违规调用:(ancestor? 'odd? 'integer?)导致该确切错误。

(我不知道你的代码在做什么:这个想法是很容易机械地推导出问题。)

于 2013-05-10T13:58:49.193 回答