【发布时间】:2020-06-06 08:45:03
【问题描述】:
我正在尝试编写一个函数,它接受一个二进制数列表并返回一个列表列表,如下所示:它必须遍历初始列表,并且可以添加 n 的嵌套列表一个,最多 n-1 个以零或单个零结尾的。例如(n = 3):
[1;1;1;1;0;0] -> [[1;1;1];[1;0];[0]]
[1;1;1] -> [[1;1;1]]
[1;0;0;1;1;0] -> [[1;0];[0];[1;1;0]]
我目前的代码是
let bitListSequence binList n =
let rec aux acc binList =
match binList with
| [] -> acc
| x::xs ->
let rec loop acc2 = function
| 1 -> if List.length acc2 < n-1 then loop acc2::1 xs
else aux acc2 xs
| 0 -> aux acc2::0 xs in
aux [] acc
我认为逻辑很好,但语法不正确。我正在尝试将嵌套列表累积到 acc2 中,直到满足条件,然后将其附加到 acc 并重复该过程直到 binList 为空。
【问题讨论】:
标签: functional-programming ocaml nested-loops