I have this function which takes a list as input and computes the powerset.
let rec powerset = function
| [] -> [[]]
| x::xs -> List.collect (fun subset -> [subset; x::subset]) (powerset xs)
let ps = powerset [1..3]
val ps: int list list =
[[]; [1]; [2]; [1; 2]; [3]; [1; 3]; [2; 3]; [1; 2; 3]]
Is there a cleaner way to do it? I'm also a little confused by how it actually works. List.collect takes a function and a list and concats the results into a new list, which is then returned. But I have a little trouble following the flow in this case.
Why is the first list empty?