2013-09-26 48 views
2

我嘗試做這樣的事情在core.logicClojure的core.logic計數元素

(defn count-different-elements-in-list [coll] 
    (count (set coll))) 

這部作品與整數就好

(should= 1 (count-different-elements-in-list '(1 1 1))) 
(should= 2 (count-different-elements-in-list '(1 1 2))) 
(should= 3 (count-different-elements-in-list '(1 3 2))) 

,但現在我想使用core.logic來解決一些東西,那裏得到的雜亂

(run* [a b c] 
    ;;the variables get values between 1 and 3 
    (fd/in a b c (fd/interval 1 3)) 
    ;; in the list there should only be 2 different values 
    (== 2 (count-different-elements-in-list '(a b c)))) 

但是問題出在這裏,abc沒有得到pas sed作爲函數的值。它們作爲變量傳遞。有三個變量count-different-elements-in-list返回值始終爲3,而core.logic找不到解決方案(空列表)。

但我正在尋找這個結果。

([1 1 2] [1 2 1] [2 1 1] 
[1 1 3] [1 3 1] [3 1 1] 
[2 2 1] [2 1 2] [1 2 2] 
[2 2 3] [2 3 2] [3 2 2] 
[3 3 1] [3 1 3] [1 3 3] 
[3 3 2] [3 2 3] [2 3 3]) 

回答

3

您需要core.logic/project邏輯瓦爾成非關係的目標,像正常功能count-different-elements-in-list。不幸的是,您不能限制爲單個值的有限域邏輯變量,如a,bc。 (見:this question

在你有例如,可以換出fd/infd/interval用於生成範圍和membero。這將刪除無約束的有限域變量,保持整數的範圍約束,並允許投影。

(def interval (vec (range 1 4))) 
(run* [a b c] 
    (membero a interval) 
    (membero b interval) 
    (membero c interval) 
    (project [a b c] 
    (== 2 (count-different-elements-in-list (list a b c))))) 
+0

沒有得到它,但通過在結果中應用count-different-elements-in-list函數解決了問題。學習clojure需要時間。 – daniel

+0

約束中使用的正常函數,如'count-different-elements-in-list',在core.logic中被視爲非關係目標。我已經達到了我的知識水平,所以我不能真正解釋它。 – Jared314

+0

舉一個例子給它另一個鏡頭。它有助於爲您解決問題嗎? – Jared314