Given a function that removes consecutive duplicates from a list
let rec compress l =
match l with
| [] -> []
| [x] -> [x]
| x :: (y :: _ as t) -> if x = y then compress t else x :: compress t;;
yields the correct result
compress ["a"; "a";"b";"c";"c"] ;;
- : string list = ["a"; "b"; "c"]
but if instead of x :: (y :: _ as t) I change to x :: (y :: t), then I get the incorrect result
compress ["a"; "a";"b";"c";"c"] ;;
- : string list = ["b"; "c"]
What is going on here ? I can't wrap my head around as to how the declaration _ as t changes the output of the function. Intuitively, I would think they are equivalent statements (:: _ as t and :: t), but seemingly they are not.
Could someone elaborate ?