I am a Ocaml beginner, and am not able to understand tail recursiveness or list iteration. How can we iterate through list is 2s and swap the pairs?
let rec swap = function| a :: (b :: _ as t) -> b::a::swap t | smaller -> smaller;;let newlist = swap [1;2;3;4];;List.iter print_int newlist;
For example, 1234, the swap function is swapping 1 and 2, and then the list head is still at 2, and its swapping 2 and 3 whereas it should be at 3 and swapping 3 and 4.