相关疑难解决方法(0)

懒洋洋地生成powerset

我想计算一组的powerset.因为我一次不需要整个powerset,所以最好懒得生成它.

例如:

powerset (set ["a"; "b"; "c"]) =
seq {
  set [];
  set ["a"];
  set ["b"];
  set ["c"];
  set ["a"; "b"];
  set ["a"; "c"];
  set ["b"; "c"];
  set ["a";"b"; "c"];
}
Run Code Online (Sandbox Code Playgroud)

由于结果是一个序列,我更喜欢它的顺序.我怎样才能在F#中以一种自我的方式做到这一点?

编辑:

这就是我将要使用的(基于BLUEPIXY的答案):

let powerset s =
    let rec loop n l =
        seq {
              match n, l with
              | 0, _  -> yield []
              | _, [] -> ()
              | n, x::xs -> yield! Seq.map (fun l -> x::l) (loop (n-1) xs)
                            yield! loop n xs
        }   
    let …
Run Code Online (Sandbox Code Playgroud)

f# lazy-evaluation powerset

4
推荐指数
1
解决办法
947
查看次数

标签 统计

f# ×1

lazy-evaluation ×1

powerset ×1