Skip to content

Instantly share code, notes, and snippets.

/powerset.clj

Created Jan 26, 2011
Embed
What would you like to do?
get powerset of set
(use '[clojure.set]
'[clojure.test])
(defn powerset [ls]
(if (empty? ls) '(())
(union (powerset (next ls))
(map #(conj % (first ls)) (powerset (next ls))))))
(deftest test-powerset
(is (= '(()) (powerset '())))
(is (= '((1) ()) (powerset '(1))))
(is (= '((1) (1 2) (2) ()) (powerset '(1 2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment