OCAML将此代码从组合更改为置换

OCAML将此代码从组合更改为置换,ocaml,combinations,permutation,Ocaml,Combinations,Permutation,这是获取int和list的代码,在给定不同对象数量的情况下,生成该列表的所有可能组合。例如,如果您为列表[1;2;3]插入int 2,它将为您提供[1;2]、[2;3]、[1;3]!我想让它做的是给我排列,而不是组合,顺序很重要!如何更改此代码来为我执行此操作 # let extract k list = let rec aux k acc emit = function | [] -> acc | h :: t -> if k = 1 then aux k (e

这是获取int和list的代码,在给定不同对象数量的情况下,生成该列表的所有可能组合。例如,如果您为列表[1;2;3]插入int 2,它将为您提供[1;2]、[2;3]、[1;3]!我想让它做的是给我排列,而不是组合,顺序很重要!如何更改此代码来为我执行此操作

# let extract k list =
  let rec aux k acc emit = function
  | [] -> acc
  | h :: t ->
    if k = 1 then aux k (emit [h] acc) emit t else
      let new_emit x = emit (h :: x) in
      aux k (aux (k-1) acc new_emit t) emit t
  in
  let emit x acc = x :: acc in
  aux k [] emit list;;

val extract : int -> 'a list -> 'a list list = <fun>
# 
#让我们提取k列表=
让rec aux k acc发射=功能
|[]->acc
|h::t->
如果k=1,则辅助k(发射[h]acc)发射t else
让new_emit x=emit(h::x)in
辅助k(辅助(k-1)acc新_发射t)发射t
在里面
让发射x acc=x::acc in
辅助k[]发射列表;;
val extract:int->'a list->'a list=
# 

此代码相当棘手,因为它依赖于重新定义
emit
函数来向发出的列表添加前缀。我认为你可以把函数写得更直接一点

尽管如此,尝试的一件事可能是让
new\u emit
(修改后的
emit
)添加
h
,不仅作为前缀,而且在列表中的每个可能位置添加
x
。我没试过这个,但听起来不错