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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -