在球拍中排序的奇怪行为

时间:2013-05-10 12:49:13

标签: sorting hashmap racket predicate

我有以下定义:

    (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的每一行代码...我真的很困惑可能导致这个...有什么想法吗?

1 个答案:

答案 0 :(得分:2)

  1. 我按原样使用您的代码并将其放入文件中。

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

    < / LI>
  3. 这会显示代码中违规的违规通话:(ancestor? 'odd? 'integer?)会导致确切的错误。

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