Haskell Powerset in Lexicographic Order -
i want write powerset function in haskell function declaration of:
powerset :: ord => [a] -> [[a]]
however, i'm trying make lexicographical ordering that:
powerset [1,2,3] = [[], [1], [1,2], [1,2,3], [1,3], [2], [2,3], [3]]
i've found other ways of doing powerset, such as:
powerset = filterm (const [true, false])
but nothing provides powerset in lexographic order. there way write powerset function provide or sort unsorted powerset ordering?
you may right fold:
powerset :: foldable t => t -> [[a]] powerset xs = []: foldr go [] xs go x acc = [x]: fmap (x:) acc ++ acc
then:
\> powerset [1, 2] [[],[1],[1,2],[2]] \> powerset [1, 2, 3] [[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]
(edited answer remove call tail
function)
Comments
Post a Comment