Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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)))))
Add Comment
Please, Sign In to add comment