惰性“n选择k”在 OCaml 中
作为枚举集合的更大问题的一部分,我需要编写一个 OCaml 函数“choose”,它接受一个列表并输出为由该列表的元素组成的所有可能的大小为 k 的序列的列表(不重复序列,这可以可以通过排列相互获得)。它们在最终列表中的顺序无关。
例如,
choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]]
有什么想法吗?
我想让整个事情变得懒惰,输出一个懒惰列表,但如果你有一个严格的解决方案,那也会非常有用。
As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant.
For example,
choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]]
Any ideas?
I would like to have the whole thing to be lazy, outputting a lazy list, but if you have a strict solution, that'll be very useful too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个严格且次优的版本。我希望这是清楚的。它通过假设输入列表中没有重复项并仅生成与原始列表中顺序相同的子列表来避免重复。
长度计算可以通过将
l
的长度作为choose
的参数传递来进行分解。这会使代码的可读性降低,但效率更高。对于惰性版本,在代码上撒上“lazy”和“Lazy.force”...
编辑:
从下面的注释中看来有必要使用
lazy_list_append
:Here is a strict and suboptimal version. I hope it is clear. It avoids duplicates by assuming there are no duplicates in the input list, and by generating only sublists that are in the same order as in the original list.
The length computation could be factored by passing
l
's length as an argument ofchoose
. That would make the code less readable but more efficient.For the lazy version, sprinkle "lazy" and "Lazy.force" on the code...
EDIT:
A
lazy_list_append
as appears necessary from the comments below:使用 Haskell 解决方案再次插入(使用惰性列表更容易,因为它们是内置的):
前两种情况来自 二项式系数,更具体地说:
n 选择 0 = 1
对于所有n
,包括n=0
(这就是为什么它首先处理0 select 0
的情况)。另一种是0选择k = 0
。第三个方程是组合的递归定义的精确翻译。不幸的是,当您将其应用于无限列表时,它会返回一个简单的解决方案:
编辑:
好的,所以我们真的想以有限的步骤完成每个组合。在上面的版本中,我们显然只使用了
++
左侧的表达式,它只生成从 1 开始的组合。我们可以通过定义一个有趣的列表压缩函数来解决这个问题,该函数通过以下方式构建列表交替选择每个参数列表的头部(第二个参数不严格很重要):并使用它而不是
++
:让我们看看:
所有
10 选择 3< /code> 组合就在那里!
Plugging in again with a Haskell solution (it's just easier to work with lazy lists since they are built-in):
The first two cases follow from the properties of binomial coefficients and more specifically:
n choose 0 = 1
for alln
includingn=0
(that's why it is first to handle the case0 choose 0
). The other one is0 choose k = 0
. The third equation is exact translation of the recursive definition of combinations.Unfortunately when you apply it to an infinite list it returns a trivial solution:
EDIT:
OK, so we really want to go trough each combination in a finite number of steps. With the above version we are obviously using only the expression to the left of
++
which generates only combinations starting with 1. We can work around this problem by defining an interesting list zipping function which builds a list by alternately picking the head of each of its argument lists (it's important to be non-strict in the second argument):and use it instead of
++
:lets see:
All
10 choose 3
combinations are there!只是为了完整起见,我将最终的代码放在这里,它将 Pascal 的严格代码与我的懒惰内容和所有其他 Pascal 的有用注释结合在一起。
定义了惰性列表类型,然后是两个辅助惰性函数(append 和 map),最后是我们要定义的函数“choose”。
评估“choose k ls n”的结果是列表 ls 的 k 个元素的所有选择的惰性列表,其中 ls 被认为最大为 n。请注意,正如 Pascal 所指出的,由于枚举发生的方式,函数 select 将不会覆盖无限列表的所有选择。
谢谢,这真的很有用!
最好的,
苏里卡托。
Just for the sake of completeness, I am putting here the final code which brings together the strict code from Pascal with my lazy stuff and all other Pascal's useful comments.
The lazy list type is defined, then two auxiliary lazy functions (append and map), and finally the function "choose" that we aim to define.
The result of evaluating "choose k ls n" is a lazy list of all choices of k elements of list ls, with ls considered up to size n. Note that, as pointed out by Pascal, because of the way the enumeration takes place, the function choose will not cover all choices of an infinite list.
Thanks, this was really useful!
Best,
Surikator.