babsetta21
Programmer
Hi,
I have a problem that I have been trying to solve for the past week.
I have to implement swap (X,Y,List,NList) which binds Nlist with List but with its Xth and Yth elements swapped.
eg. swap (2,4,[a,b,c,d,e],Final) binds Final to [a,d,c,b,e].
Below is the code I managed to write but it is still not working well obviously due to the Q and Z predicates...
swap(1,2,[H1,H2|Rest],[H2,H1|Rest]).
swap(X,Y,[H|T],Nlist):-Q is X-1,
Z is Y-1,
swap(Q,Z,T,Nlist),
Nlist=[H,Nlist].
Any help is sincerely appreciated!!!
I have a problem that I have been trying to solve for the past week.
I have to implement swap (X,Y,List,NList) which binds Nlist with List but with its Xth and Yth elements swapped.
eg. swap (2,4,[a,b,c,d,e],Final) binds Final to [a,d,c,b,e].
Below is the code I managed to write but it is still not working well obviously due to the Q and Z predicates...
swap(1,2,[H1,H2|Rest],[H2,H1|Rest]).
swap(X,Y,[H|T],Nlist):-Q is X-1,
Z is Y-1,
swap(Q,Z,T,Nlist),
Nlist=[H,Nlist].
Any help is sincerely appreciated!!!