Created
November 16, 2017 01:44
-
-
Save gallettilance/2abae0b51a2b28b599d60e7e118efc15 to your computer and use it in GitHub Desktop.
Get all subsets of a set
This file contains 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 combine [xs ys res] | |
(if (empty? ys) res (concat (combine (cons (first ys) xs) (rest ys) '()) | |
(combine xs (rest ys) (cons (cons (first ys) xs) res)) ))) | |
(defn getsubsets [xs] | |
(cons '() (combine '() xs '()))) | |
(count (getsubsets '(1 2 3))) ;; 2^3 = 8 | |
(count (getsubsets '(1 2 3 4))) ;; 2^4 = 16 | |
(count (getsubsets '(1 2 3 4 5))) ;; 2^5 = 32 | |
(count (getsubsets '(1 2 3 4 5 6))) ;; 2^6 = 64 | |
(count (getsubsets '(1 2 3 4 5 6 7))) ;; 2^7 = 128 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment