F # Powerset Function

The function below returns a set of parameters for a set (list).

let rec powerset = function
  | [] -> [[]]
  | x::xs -> List.collect (fun sub -> [sub; x::sub]) (powerset xs)

I don’t understand why this is what works. I understand recursion. I also understand how List.collect works. I know that recursion will continue until an instance of powerset [[]] returns. However, I try to track the return values ​​after this point, and I never get a full set of power.

+4
source share
1 answer

The power gain calculation algorithm is as follows:

(aka "input" ) A. , x. , poweret A ( P(A)) A. A : , x, , x. , , x, - A - x (A x ):

  all subsets of A that don't include x = P(A-x)

A, x? , x x !

  all subsets of A that include x = { for each S in P(A-x) : S+x }

, P(A):

  P(A) = P(A-x) + { for each S in P(A-x) : S+x }

, : P(A-x), powerset xs, x, .

+6

Source: https://habr.com/ru/post/1655898/


All Articles