I tried to write my own solution for this exercise by iterating through a list with a empty complst
list where all non duplicates are inserted into and then get returned.I know it is a over complicated approach after looking up the solution but would still like to understand why the pattern matching does not work as intended:
let compress list = let rec aux complst lst = match lst with | [] -> complst | a :: (b :: c) -> if a = b then aux complst (b::c) else aux (a::complst) (b::c) | x -> x in aux [] list;;val comp : 'a list -> 'a list = <fun>
Regardless of the input, the output is always a list with only the last element:
compress [1;1;2;2;3];;- : int list = [3] compress [1;2;3];;- : int list = [3]