Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 594

Trying to replicate the elements in a list n times in OCaml

$
0
0

I'm trying to write a function that would take an input like :

repeat 3 [1;2] ;;

and display something like:

[1;2;1;2;1;2] 

Now the code I have is:

let repeat ls n =  let rec helper acc n l =    if n = 0 then acc     else helper (l :: acc) (n-1) l   in  let rec helper2 acc = function    | [] -> acc    | h :: t -> helper2 (helper acc n h) t    in   helper2 [] (List.rev ls);;

which gives me an output of:

[1;1;1;2;2;2] 

for the same input. What can I do to fix this?


Viewing all articles
Browse latest Browse all 594

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>