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])
沒有得到它,但通過在結果中應用count-different-elements-in-list函數解決了問題。學習clojure需要時間。 – daniel
約束中使用的正常函數,如'count-different-elements-in-list',在core.logic中被視爲非關係目標。我已經達到了我的知識水平,所以我不能真正解釋它。 – Jared314
舉一個例子給它另一個鏡頭。它有助於爲您解決問題嗎? – Jared314