-
-
Save hozumi/797041 to your computer and use it in GitHub Desktop.
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
(use '[clojure.test]) | |
(defn powerset | |
([ls] (lazy-seq (cons () (powerset '(()) ls)))) | |
([acc ls] | |
(if (empty? ls) | |
() | |
(let [fs (first ls) | |
added (map #(conj % fs) acc)] | |
(lazy-cat added | |
(powerset (concat acc added) (rest ls))))))) | |
(deftest test-powerset | |
(is (= '(()) (powerset '()))) | |
(is (= '(() (1)) (powerset '(1)))) | |
(is (= '(() (1) (2) (2 1)) (powerset '(1 2))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment