# Replicate items from list basing on the second list

**URL:** <https://discuss.ocaml.org/t/replicate-items-from-list-basing-on-the-second-list/4641>\
**Category:** Learning\
**Created:** [November 6, 2019, 8:30pm UTC](https://discuss.ocaml.org/t/replicate-items-from-list-basing-on-the-second-list/4641 "2019-11-06T20:30:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Simpson](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/roger_simpson/32/1847_2.png) [@Roger\_Simpson](https://discuss.ocaml.org/u/Roger_Simpson)\
**Post date:** [November 6, 2019, 8:30pm UTC](https://discuss.ocaml.org/t/replicate-items-from-list-basing-on-the-second-list/4641/1 "2019-11-06T20:30:32Z")

</div>

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.

_For example:_

```auto
duplicate [5;6;7] [0;2;5;3];;

```

_returns:_

```auto
[6;6;7;7;7;7;7]

```

My code so far:

```auto
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.

---

<div class="post-metadata">

**Author:** ![cloudyhug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cloudyhug/32/2399_2.png) [@cloudyhug](https://discuss.ocaml.org/u/cloudyhug)\
**Post date:** [November 6, 2019, 8:56pm UTC](https://discuss.ocaml.org/t/replicate-items-from-list-basing-on-the-second-list/4641/2 "2019-11-06T20:56:37Z")

</div>

Hi. The issue with your nested functions is that you forgot the `in` keyword. When defining nested functions, you need to show to the compiler that you are still writing the body of the `duplicate` function, and not defining `read` or `print` as brand new functions. Here is a toy example :

```ocaml
let dbl_and_dup x =
  let dbl x = x * 2 in
  let dup x = (x, x) in
  dup (dbl x)

```

As you can see, `dbl` and `dup` are nested functions, and thus are invisible from outside the `dbl_and_dup` function.

Here is an stdlib version of what you asked, for two lists of same length :

```ocaml
let dup l1 l2 =
  List.combine l1 l2
  |> List.map (fun (e, n) -> List.init n (fun _ -> e))
  |> List.concat

```

A little additional work is required for the function to accept two lists of different lengths. And if you do not want to use library functions, you can define them yourself. Hope it helps !

---

<div class="post-metadata">

**Author:** ![Roger\_Simpson](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/roger_simpson/32/1847_2.png) [@Roger\_Simpson](https://discuss.ocaml.org/u/Roger_Simpson)\
**Post date:** [November 6, 2019, 9:50pm UTC](https://discuss.ocaml.org/t/replicate-items-from-list-basing-on-the-second-list/4641/3 "2019-11-06T21:50:11Z")

</div>

Not sure, but your stdlib version returns error: _Unbound value List.init_

---

<div class="post-metadata">

**Author:** ![jfeser](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jfeser/32/2093_2.png) [@jfeser](https://discuss.ocaml.org/u/jfeser)\
**Post date:** [November 6, 2019, 10:35pm UTC](https://discuss.ocaml.org/t/replicate-items-from-list-basing-on-the-second-list/4641/4 "2019-11-06T22:35:04Z")

</div>

You might have an old stdlib. List.init was added in 4.06.
