Skip to content

Instantly share code, notes, and snippets.

@gallettilance
Created November 16, 2017 01:44
Show Gist options
  • Save gallettilance/2abae0b51a2b28b599d60e7e118efc15 to your computer and use it in GitHub Desktop.
Save gallettilance/2abae0b51a2b28b599d60e7e118efc15 to your computer and use it in GitHub Desktop.
Get all subsets of a set
(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