Could someone propose better and/or more elegant implementation of this:
let each xs =
let rec each' acc left right =
match right with
| [] -> acc
| right -> let new_left = left @ [List.hd right]
let next = List.tl right
let result = (List.hd right), left @ next
each' (result::acc) new_left next
each' [] [] xs
It do that:
> each [1..3];; val it : (int * int list) list = [(3, [1; 2]); (2, [1; 3]); (1, [2; 3])]
This function could return the result in reverse direction, too. The idea is to get all elements as tuples with an element and list of rest elements.
The semantic is slightly different here, but from the example you give
Setmight be a good fit: