-
-
Save hadielmougy/5d468409e2e5d75e1f33fba92cca721b to your computer and use it in GitHub Desktop.
powerset in clojure using sets,union
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
(ns powerset.core-test | |
(:use (clojure set test))) | |
(defn powerset [s] | |
(apply union | |
#{s} ;the complete set of all s | |
(map (fn [i] (powerset (disj s i))) s))) | |
(deftest test-powerset | |
(is (= #{#{}} | |
(powerset #{}))) | |
(is (= #{ #{} #{1}} | |
(powerset #{1}))) | |
(is (= #{ #{} #{1} #{2} #{1 2}} | |
(powerset #{1 2}))) | |
(is (= #{#{} #{1} #{2} #{1 2} #{3} #{1 3} #{2 3} #{1 2 3}} | |
(powerset #{1 2 3})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment