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

Reversing tuples inside a list with fold_left in Ocaml

$
0
0

Let lst be a List with tuples which I want to reverse each (The order of the tuples needs to be the same). For example:

[(a,b);(c,d)] -> [(b,a);(d,c)] 

I know that it can be done with map:

List.map (fun (a,b) -> (b,a)) [(1,2);(3,4)];;- : (int * int) list = [(2, 1); (4, 3)]

But I´m looking for a way to achieve it with List.fold_left. My approach is to match the list with [] as a accumulator to concatenate every reversed tuple together, but I keep getting type-Errors:

List.fold_left (fun (a,b) -> (b,a)) [] lst;;

Viewing all articles
Browse latest Browse all 591

Trending Articles