I am trying to implement the following program in Ocaml:
Write a shuffle : α list → α list → α list
procedure, which will return a list with elements from 2 lists shuffled according to the following rules:
- all of the lists from 2 input lists are on the output lists
- elements from both lists are in the same order as previously
- elements of at least one the input lists are not next to each other on the output list
Example outputs of shuffle [5; 7; 0; 5] [3; 2; 8; 9; 3]
:
[5; 3; 2; 7; 8; 9; 0; 3; 5]
[3; 5; 2; 7; 8; 0; 9; 5; 3]
I know how to implement a simple shuffle function (taken from here):
let shuffle d =
let nd = List.map (fun c -> (Random.bits (), c)) d in
let sond = List.sort compare nd in
List.map snd sond
Can you please give me any tips on how should I go from there?