开发者

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜