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

时间:2016-04-08 20:44:11

标签: clojure logic clojure-core.logic

我想在以下示例代码中从[:b :B]中删除[[:a :A] [:b :B] [:c :C]],如果我将:B替换为(lvar),则不再有效:

;; 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

1 个答案:

答案 0 :(得分:0)

我认为在您的示例中不起作用的原因是创建的(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)))