projog

3.106. flatten(X,Y) - flattens a nested list.

Flattens the nested list represented by X and attempts to unify it with Y.

Examples

?- flatten([a,[[b]],[c]], X).
X = [a,b,c]

yes

?- flatten([a,b,c], X).
X = [a,b,c]

yes

?- flatten([[[[a]]],[],[],[]], X).
X = [a]

yes

?- flatten([a], X).
X = [a]

yes

?- flatten(a, X).
X = [a]

yes

?- flatten([[[[]]],[],[],[]], X).
X = []

yes

?- flatten([], X).
X = []

yes

?- flatten([a|b], X).
X = [a,b]

yes

?- flatten([a|[]], X).
X = [a]

yes

?- flatten([[a|b],[c,d|e],[f|[]],g|h], X).
X = [a,b,c,d,e,f,g,h]

yes

?- flatten([p([[a]]),[[[p(p(x))]],[p([a,b,c])]]], X).
X = [p([[a]]),p(p(x)),p([a,b,c])]

yes

?- flatten([a,b,c], [c,b,a]).

no

?- flatten([a,b,c], [a,[b],c]).

no

?- flatten([a,b,[c|X],d|Y], Z).
X = UNINSTANTIATED VARIABLE
Y = UNINSTANTIATED VARIABLE
Z = [a,b,c,X,d,Y]

yes