Write a function that replicates items in a list based on a second list specifying the number of times items are to be duplicated.
Write the function in Ocaml in a functional way of programming using lists.
If library functions - may be used only with O(1) computational complexity.
duplicate [5;6;7] [0;2;5;3];;
My code so far:
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));;
First of all, it does not compile, I have “syntax error”…
I do not feel sure about these “nested” functions.
I believe that complexity could be better.