Created
May 6, 2015 14:33
-
-
Save SVMBrown/d7eeb6c41790315662d4 to your computer and use it in GitHub Desktop.
Cartesian Product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn cartesianProduct [a b] | |
(clojure.set/union | |
(map | |
#(into [] | |
(list* | |
(first b) | |
(if (coll? %) | |
% | |
(list %) | |
) | |
) | |
) | |
a | |
) | |
(if (empty? (rest b)) | |
'() | |
(cartesianProduct a (rest b)) | |
) | |
) | |
) | |
(defn genProduct [& args] | |
(if (empty? (rest (rest args))) | |
(cartesianProduct (first (rest args)) (first args)) | |
(cartesianProduct (apply genProduct (rest args)) (first args)) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment