(* I want to return a list of common elements shared between two lists. In particular, I want the cartesian product of two lists, such that: let x = [2; 4; 6; 8; 8; 10; 12] let y = [-7; -8; 2; 2; 3; 4; 4; 8; 8; 8;] join(x, y) = [2; 2; 4; 4; 8; 8; 8; 8; 8; 8] The following code is close to what I want: it returns distinct elements shared between two lists, such that join(x, y) = [2; 3; 4; 8; 9] -- Juliet *) let join x y = let rec loop = function | [], y | y, [] -> y | (x::xs as x'), (y::ys as y') -> if x = y then y :: loop(xs, ys) elif x > y then loop(x', ys) else loop(xs, y') loop(x, y)