我尝试在 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 没有作为值传递给函数。它们作为变量传递。使用三个变量 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])