编写一个函数,该函数根据第二个列表(指定要复制项目的次数)复制列表中的项目。
使用列表的功能编写方式在Ocaml中编写功能。
如果使用库函数-仅可用于O(1)计算复杂度。
例如:
duplicate [5;6;7] [0;2;5;3];;
返回:
[6;6;7;7;7;7;7]
到目前为止我已经发明的代码:
let duplicate (list1,list2)=
let rec read (list2,list1) =
if List.hd list2 = 0 then read (List.tl list2,List.tl list1) else print (List.hd list2,List.hd list1)
let rec print (acc,num) =
num :: (print (acc-1,num));;
- 首先,它无法编译,我有“语法错误” ...
- 我不确定这些“嵌套”功能。
- 我相信复杂性会更好。