The OCaml list is immutable, so there is no such thing as deleting and inserting elements in list operations.
What you can do is create a new list, reusing a specific part of the old list. For example, to create a list (1, 3)::xs'from (1, 2)::(2, 3)::xs', you are actually reusing xs'and creating a new list using the constructor cons.
And pattern matching is very convenient to use:
let rec transform xs =
match xs with
| [] | [_] -> xs
| (x, y1)::(y2, z)::xs' when y1 = y2 -> (x, z)::transform xs'
| (x, y1)::(y2, z)::xs' -> (x, y1)::transform ((y2, z)::xs')
source
share