OCAML中使用FOLD的列表函数中按3个元素分组的说明 [英] Explanation for group by 3 elements from a list function using FOLD in OCAML
问题描述
我有一段代码可以执行以下操作:将n个元素的列表中的3个元素分组.主要功能称为group_by_3
.例如,执行group_by_3 [1;2;3;4;5;6;7]
将给我([1;2;3],[4;5;6],[7])
.
I have a piece of code that does the following: group 3 elements of a list of n elements. The main function is called group_by_3
. For example, executing group_by_3 [1;2;3;4;5;6;7]
will give me ([1;2;3],[4;5;6],[7])
.
let group_by_3 lst =
let accum = ( [], [], 0 )
in
let f (all_groups, current_group, size) x =
if size = 3
then ( (List.rev current_group) :: all_groups,
[x], 1 )
else ( all_groups, x::current_group, size+1)
in
let (groups, last, _) = List.fold_left f accum lst
in List.rev ( List.rev last :: groups)
我不太明白为什么这样做(在课堂上提供).
I don't really understand why this works (it is provided in class).
- 什么是all_groups,current_group,大小?
-
这是做什么用的?
- What are all_groups, current_group, size?
What does this do?
if size = 3
then ( (List.rev current_group) :: all_groups,
[x], 1 )
else ( all_groups, x::current_group, size+1)
谢谢!
推荐答案
什么是
all_groups
,current_group
,size
?
这是通过访问输入序列中的每个项目来产生分组所需的三个状态.这些单独的状态组合成3个元组,以形成折叠的单个状态.最终,我们只关心all_groups
,其他两个只是构造它的必要中间状态.
These are the three pieces of state required to produce the groupings by visiting each item in the input sequence. These individual states are combined in a 3-tuple to form a single state for the fold. Ultimately we'll only care about all_groups
, and the other two are just intermediate state necessary to construct that.
-
all_groups
:这是增加完成的分组的列表值.每当我们看到足以满足组大小的新元素时,我们就会创建一个新组并将其添加到此列表中. -
current_group
:这也是一个列表值,但更多的是用于建立分组直到到达size
的临时缓冲区.当它足够大时,它将被添加到all_groups
,并使用当前项目[x]
重置为新的组/列表. -
size
这只是一个计数器,用于跟踪current_group
中有多少个项目.
all_groups
: This is a list value that accretes completed groupings. Whenever we've seen enough new elements to satisfy the group size, we make a new group and add it to this list.current_group
: This is also a list value, but more of a temporary buffer to build up a grouping until it reachessize
. When it's big enough, it gets added toall_groups
and is reset to a new group/list with the current item[x]
.size
this is just a counter to track how many items are incurrent_group
.
这是做什么的?
What does this do?
if size = 3
只是确定我们是要继续积累元素还是要有足够的分组能力.
if size = 3
simply decides whether we want to keep accumulating elements or if we've got enough for a grouping.
then ( (List.rev current_group) :: all_groups, [x], 1 )
正在建立/返回新的累加器值,该值是all_groups
,current_group
和size
的三元组.由于 else 分支中列表的生成方式,因此必须使用List.rev
调用;将项目添加到列表的 front 最快,但这是输入序列的 reverse ,因此我们将它们反向. x
是当前项目,它将是新的,正在增长的组中的第一项. 1
当然是该新组的大小.
then ( (List.rev current_group) :: all_groups, [x], 1 )
is building/returning the new accumulator value, which is a 3-tuple of all_groups
, current_group
, and size
. The List.rev
call is necessary because of the way the list is being grown, in the else branch; it's fastest to add items to the front of a list, but this is the reverse of the input sequence thus we reverse them. x
is the current item, which will be the first item in the new, growing group. 1
is of course the size of that new group.
else ( all_groups, x::current_group, size+1)
将当前项目x
弹出到current_group
列表的前面,并增加size
计数器.
else ( all_groups, x::current_group, size+1)
is popping the current item x
onto the front of the current_group
list and incrementing the size
counter.
该部分下面的逻辑是处理所有不能整齐地归为三类的散乱项目.
Below that section is the logic that takes care of any straggler items that don't fit neatly into groupings of three.
这篇关于OCAML中使用FOLD的列表函数中按3个元素分组的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!