I have a list of substitutions between variables and values
they are presented in a list [[x,y],[1,2]] (meaning that the value of x equals 1, and the value of y equals 2).
I want to change the list to a list of pairs meaning [[x,1],[y,2]],
I tried to use append so that I will create a pair in each step of the recursion and append it to a new list but i have problem of doing so (mainly selecting the head and tails of each pair)
Asked
Active
Viewed 110 times
0
false
- 10,264
- 13
- 101
- 209
user3453625
- 67
- 4
-
Can you show your attempt(s) at a solution? Or just use [transpose/2](http://www.swi-prolog.org/pldoc/man?predicate=transpose/2). – lurker Sep 19 '14 at 16:25
-
1possible duplicate of [How to transpose a matrix in prolog](http://stackoverflow.com/questions/4280986/how-to-transpose-a-matrix-in-prolog) – lurker Sep 19 '14 at 16:29
-
@lurker: Hehe, how right you are! – false Sep 19 '14 at 16:35
1 Answers
0
your example it's a bit contrived, but transpose it's a bit overkill, specially if you're going to implement yourself. I think this should solve your need
'change an order of list of lists'([X_Y_Z, A_B_C], XA_YB_ZC) :-
findall([K, V], (nth1(I, X_Y_Z, K), nth1(I, A_B_C, V)), XA_YB_ZC).
CapelliC
- 59,646
- 5
- 47
- 90