如何在OCAML中产生n数量相等大小的赞助者?

发布于 2025-02-07 06:32:55 字数 1288 浏览 3 评论 0原文

我的目标是将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2025-02-14 06:32:55

如果要将初始列表拆分为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 by n. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文