Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 528

deleting duplicates tail recursively in OCaml

$
0
0

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]

Viewing all articles
Browse latest Browse all 528

Trending Articles