Lists conversion
I am newbie to prolog and I am trying to flatten a list which is like this:
X = [[[0,0,1],[1,0,2]],[[0,1,3],[0,2,2]],[[0,3,5],[0,4,4]],[[0,5,4],[0,6,5]]]
The desired output is:
X = [[0,0,1],[1,0,2],[0,1,3],[0,2,2],[0,3,5],[0,4,4],[0,5,4],[0,6,5]]
How can I do this?开发者_运维技巧
Use recursion :-)
myflatten([], []).
myflatten([[A, B]|T], [A|[B|FlattenedT]]) :- myflatten(T, FlattenedT).
And then query:
?- myflatten([[[0,0,1],[1,0,2]],[[0,1,3],[0,2,2]],[[0,3,5],[0,4,4]],[[0,5,4],[0,6,5]]], X).
精彩评论