如何在OCAML中产生n数量相等大小的赞助者?
我的目标是将split n l
函数采用n
和一个作为参数的列表,然后尝试将该列表拆分为n
编号相等的列表。
我功能的签名是:val split:int-> '清单 - > '列表列表=< fun>
该列表上的执行:[0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20]
将给出:
[[0; 1; 2; 3; 4; 5; 6]; [7; 8; 9; 10; 11; 12; 13]; [14; 15; 16; 17; 18; 19; 20]];
我试图用我与之合作的OCAML书以及我发现的功能来激发自己的
let take l n =
if n < 0 then
raise (Invalid_argument "take")
else
let rec take_inner r l n =
if n = 0 then
List.rev r
else
match l with
| [] -> raise (Invalid_argument "take")
| h :: t -> take_inner (h :: r) t (n - 1) in
take_inner [] l n
let drop l n =
let rec drop_inner n l =
match l with
| [] -> raise (Invalid_argument "drop")
| _ :: t ->
if n = 1 then
t
else
drop_inner (n - 1) t in
if n < 0 then
raise (Invalid_argument "drop")
else if n = 0 then
l
else
drop_inner n l
let rec split n l =
try take l n :: split n (drop l n)
with _ -> (
match l with
| [] -> []
| _ -> [l])
灵感列出了n个大小的赞助者,而不是n个sublists。
我只是看不到如何一起调整所有内容以了解我想要的东西。
请问吗?谢谢。
My goal here is to have a split n l
function that takes a N
and a list as argument and try to split that list into N
number of equal sized lists.
The signature of my function would be: val split : int -> 'a list -> 'a list list = <fun>
The execution on that list: [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20]
would give, for example:
[[0; 1; 2; 3; 4; 5; 6]; [7; 8; 9; 10; 11; 12; 13]; [14; 15; 16; 17; 18; 19; 20]];
I have tried to inspire myself with an OCaml book I'm working with and functions I've found such as
let take l n =
if n < 0 then
raise (Invalid_argument "take")
else
let rec take_inner r l n =
if n = 0 then
List.rev r
else
match l with
| [] -> raise (Invalid_argument "take")
| h :: t -> take_inner (h :: r) t (n - 1) in
take_inner [] l n
let drop l n =
let rec drop_inner n l =
match l with
| [] -> raise (Invalid_argument "drop")
| _ :: t ->
if n = 1 then
t
else
drop_inner (n - 1) t in
if n < 0 then
raise (Invalid_argument "drop")
else if n = 0 then
l
else
drop_inner n l
let rec split n l =
try take l n :: split n (drop l n)
with _ -> (
match l with
| [] -> []
| _ -> [l])
I feel like I'm close to what I'd like to achieve even though this version gives a list of N sized sublists, and not N sublists.
I just don't see how to tweak everything together to get to what I want.
Any advices please? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要将初始列表拆分为
n
同等大小的列表,则首先需要确定每个列表的大小。为此,您需要计算原始列表的大小并将其除以n
。如果均匀分裂,那么这是每个列表的长度,您已经知道如何生成给定长度的列表。如果它不均匀分裂,则可以生成错误或将最后一个列表缩短,具体取决于您从功能中所需的内容。If you want to split the initial list into
n
equally sized lists, then you first need to determine the size of each list. For that, you need to compute the size of the original list and divide it byn
. If it divides evenly, then this is the length of each list, and you already know how to generate lists of the given length. If it doesn't divide evenly, then you can either generate an error or leave the last list shorter, depending on what you want from your function.