Skip to content

Instantly share code, notes, and snippets.

@hozumi
Forked from anonymous/powerset.clj
Created January 26, 2011 17:19
Show Gist options
  • Save hozumi/797041 to your computer and use it in GitHub Desktop.
Save hozumi/797041 to your computer and use it in GitHub Desktop.
(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