Computing a set of all subsets (power set)

前端 未结 2 1721
無奈伤痛
無奈伤痛 2021-01-24 17:11

I am trying to get a function (given as a parameter a set) to return a set whose elements are all the subset formed from the main set. Ex: {1;2;3} -> { {1}, {2}, {3}, {1,2}, {1,

2条回答
  •  执笔经年
    2021-01-24 17:49

    If you want to use the standard Set module, you can work with sets of strings (say) and sets of sets of strings like this:

    # module S = Set.Make(String);;
    . . .
    # module SS = Set.Make(S);;
    . . .
    
    # let s1 = S.singleton "abc";;
    val s1 : S.t = 
    # let ss1 = SS.singleton s1;;
    val ss1 : SS.t = 
    # List.map S.elements (SS.elements ss1);;
    - : S.elt list list = [["abc"]]
    

提交回复
热议问题