如何从Clojure core.logic中的[[:a:a][:b:b][:c:c]]中删除[:b(lvar)]



在下面的示例代码中,我想从[[:a :A] [:b :B] [:c :C]]中删除[:b :B],如果用(lvar)替换:B,它将不再工作:

;; Helper Function
(defne not-membero [x l]
  ([_ []])
  ([_ [?y . ?r]]
   (!= x ?y)
   (not-membero x ?r)))

这些工作:

(run* [q]
  (membero q [[:a :A] [:b :B] [:c :C]])
  (not-membero q [[:b :B]]))
(run* [q]
  (membero q [[:a :A] [:b :B] [:c :C]])
  (!= q [:b :B]))
;; both return [[:a :A] [:c :C]], as expected

这些没有(注意lvar):

(run* [q]
  (membero q [[:a :A] [:b :B] [:c :C]])
  (not-membero q [[:b (lvar)]]))
(run* [q]
  (membero q [[:a :A] [:b :B] [:c :C]])
  (!= q [:b (lvar)]))
;; both return [[:a :A] [:b :B] [:c :C]], unexpected

我认为这在您的示例中不起作用的原因是创建的(lvar)与程序中的任何其他逻辑变量都是未绑定/无关的。如果您使用fresh逻辑变量,您的程序运行良好(至少我认为这是您想要的)

(run* [q]
  (fresh [x]
    (membero q [[:a :A] [:b x] [:c :C]])
    (not-membero q [[:b x]])))
=> ([:a :A] [:c :C])
(run* [q]
  (fresh [x]
    (membero q [[:a :A] [:b x] [:c :C]])
    (!= q [:b x])))
=> ([:a :A] [:c :C])

或者,在不知道元组中的:b项的情况下,这些返回相同的结果:

(run* [q]
  (fresh [x]
    (membero q [[:a :A] x [:c :C]])
    (not-membero q [x])))
(run* [q]
  (fresh [x]
    (membero q [[:a :A] x [:c :C]])
    (!= q x)))

相关内容

  • 没有找到相关文章

最新更新