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

Pair swap elements in list using Ocaml

$
0
0

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.


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>