How can I flatten a list in Prolog, for example:
from A = [["a","b"],["c"]]
to B = ["a", "b", "c"] ?
How can I flatten a list in Prolog, for example:
from A = [["a","b"],["c"]]
to B = ["a", "b", "c"] ?
A list can be flattened using maplist and append, in the following way.
A two-level list may be flattened as shown.
?- A=[["a","b"],["c"]],maplist(append,[A],[B]).
A = [["a", "b"], ["c"]],
B = ["a", "b", "c"].
A three-level may be flattened with two passes of the command.
?- A=[[["a","b"]],[["c"]]],maplist(append,[A],[B]),maplist(append,[B],[C]).
A = [[["a", "b"]], [["c"]]],
B = [["a", "b"], ["c"]],
C = ["a", "b", "c"].
Mixtures of two and three levels in a list results in an error from two passes of the command.
?- A=[[["a","b"]],[["c"]],["d"]],maplist(append,[A],[B]),maplist(append,[B],[C]).
false.
Mixtures of two and three levels in a list can only be partially flattened with one pass of the command.
?- A=[[["a","b"]],[["c"]],["d"]],maplist(append,[A],[B]).
A = [[["a", "b"]], [["c"]], ["d"]],
B = [["a", "b"], ["c"], "d"].