I'm trying to swap two elements in a list, not really sure why mines not working.
The correct implementation should do the following:
list_swap_val ([5;6;7;3],7,5) => [7;6;5;3]
list_swap_val ([5;6;3],7,5) => [7;6;3]
Code
let rec list_swap_val (b, u, v) = fold ((fun (a,y) -> if y == u
then u::a else if y == v
then v::a else y::a), [], b)
;;
Mine code currently does:
list_swap_val([0,1,2,3,4,5,6,7,8], 1, 3) => [8,7,6,3,2,5,4,1,0]
I know that it reverses the list because of the fold, but I can't seem to get it to reverse the correct elements.