因此,我接受两个集合并想要迭代它们,以便返回一个包含两个集合的叉积的新集合。
(defn cartesian
"computes Cartesian product of A and B"
[A B]
use "set", "for")
我对 Clojure 很陌生,所以我不确定为什么要使用“set”,“for”包含在其中。
但 A 和 B 将是集合。现在我想迭代每一个并返回它们的笛卡尔积。示例:
(cartesian #{1 2} #{3 4 5}) => #{[1 3] [1 4] [1 5] [2 3] [2 4] [2 5]}
但我不确定到达那里的步骤。我已经查看了文档等,但找不到我要找的东西。这个问题的其他答案涉及列表等。但我必须使用 2 套。
我正在查看的 atm 正在使用 doseq[e A]
并在其中 doseq[x B]
然后添加每个向量对 [ex] 到一个新的 Set,然后返回它。但这看起来不像是标准的功能解决方案。我走在正确的轨道上吗?如何将其添加到新集合中?
So I take in two Sets and wanna iterate through them in order to return a new set containing the cross product of the two sets.
(defn cartesian
"computes Cartesian product of A and B"
[A B]
use "set", "for")
I'm very new to Clojure so I'm not sure why use "set, "for" is included at all.
But A and B will be Sets. Now I wanna iterate through each one and return the Cartesian product of them. Example:
(cartesian #{1 2} #{3 4 5}) => #{[1 3] [1 4] [1 5] [2 3] [2 4] [2 5]}
I'm not sure on the steps to get there though. I have looked through documentation etc but can't find what I'm looking for. Other answer to this deal with lists etc. But I have to use 2 Sets.
What I'm looking at atm is using doseq[e A]
and inside that doseq[x B]
then add each vector-pair [e x]
to a new Set and then return it. This doesn't seem like a standard functional solution though. Am I on the right track? How do I add it to a new Set?
发布评论
评论(2)
您可以使用 :
You can accomplish that using
for
:使用来自 cartesian-product “https://github.com/clojure/math.combinatorics”rel="nofollow noreferrer">clojure.math.combinatorics。要获得您想要的确切结果(向量集),请使用
into
使用map
转换器:Use cartesian-product from clojure.math.combinatorics. To get exact result you want (set of vectors), use
into
withmap
transducer: