Inserting elements of one list into another list

Hi there, I am battling with one task that took my attention. I’m supposed to substitute one element from a list with all elements from another list.

For example, lets call the function substitute.
When run like this: #substitute [1;2;3;6;7] 3 [3;4;5]
The output should be [1;2;3;4;5;6;7]

So at the first occurance of number 3 it put all those elements from the second list.

the types should be 'a list → 'a → 'a list → 'a list

I tried with pattern matching but always end up having type errors or missmatch.

It would be better if you attach your best attempt…

But let foo xs n ys = List.concat_map (fun x -> if x=n then ys else [x]) xs should be quite close to the desired result.