I am new to OCaml and trying to implement List.append as an exercise. This is what I have:
let rec append ab = match (List.rev a) with [] -> b | x:: xs -> append xs (x::b)
This seems to work until the argument a contains more than two elements. Example:
# append [1;2] [3;4] - : int list = [1; 2; 3; 4]
What's going on here? I checked, and List.rev [1;2;3] returns int list = [3; 2; 1] int list = [3; 2; 1] int list = [3; 2; 1] . I know that my implementation is naive and not (yet) lazy, but it seems like it should work.
source share