5

我使用 clojure 有一段时间了,但刚开始使用 core.logic。

给定一个域,1 2 3 4我想得到一个由两个向量组成的向量,比如([[1 2] [3 4]]).

注意:这只是我真正想做的简化版本。:) 见:https ://github.com/adamhoward/lineup

我在网上找到了非会员的定义:

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

我正在尝试像这样使用它:

(run 1 [q]
     (fresh [w x
             y z]
            (== q [[w x]
                   [y z]])
            (infd w x y z (domain 1 2 3 4))
            (everyg distinctfd [[w x] [y z]])
            (everyg #(not-membero % [y z]) [w x])))

在 Emacs 中运行它会给我一条Evaluation aborted.消息。

当我尝试退出时memberonot-membero我会回来([[1 2] [1 2]]),这对我来说很有意义。第一个向量中的每个元素[w x]都是第二个向量的成员[y z]

但是,当我打电话时,run 2我会回来([[1 2] [1 2]] [[1 2] [1 3]])。我不明白[[1 2] [1 3]]上述规则如何正确。我理解不everyg正确吗?任何指导(包括 rtfmanual、rtfbook、rtfdissertation)将不胜感激。

谢谢。

编辑:可能已经解决了这个问题。

仍然不确定来自membero但不是not-membero目标的奇怪结果,我发现我可以这样做:

(everyg #(distinctfd (conj [y z] %)) [w x])

[w x]conj'd to的每个元素都[y z]包含所有不同的值。这可能比非会员效率低,所以我仍然愿意接受任何帮助。

4

1 回答 1

1

您的示例似乎与此相同:

(run* [q]
  (fresh [w x y z]
    (fd/in w x y z (fd/domain 1 2 3 4))
    (== q [[w x] [y z]])
    (fd/distinct [w x y z])))

但是看到足球问题...

(ns test
  (:refer-clojure :exclude [==])
  (:use clojure.core.logic)
  (require [clojure.core.logic.fd :as fd]))

(defn game [g] (fresh [a b c d e] (== g [a b c d e])))

(def players (range 1 11))

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

(defne plays [player games]
  ([_ []])
  ([_ [?f ?s . ?r]]
   (membero player ?f) ; if you play this game
   (not-membero player ?s) ; do not play next game
   (plays player ?r))
  ([_ [?f ?s . ?r]]
   (membero player ?s)
   (not-membero player ?f)
   (plays player ?r)))

(defne goalies [games]
  ([[[_ _ _ _ ?a]
     [_ _ _ _ ?b]
     [_ _ _ _ ?c]
     [_ _ _ _ ?d]]]
    (fd/in ?a ?b ?c ?d (apply fd/domain players))
    (not-membero 1 [?a ?b ?c ?d]) ; star player != goalie
    (fd/distinct [?a ?b ?c ?d]))) ; not goalie twice

(defn -main [& args]
  (run 1 [q]
    (fresh [a b c d]
      (game a)
      (game b)
      (game c)
      (game d)
      (== q [a b c d])
      (goalies q)
      (everyg #(plays % q) players))))
于 2013-01-27T12:41:42.133 回答