I need to define a predicate toAdd/3
such that if Xss
and Yss
are lists of lists then toAdd(X,Xss,Yss)
holds if Yss
can be obtained by adding the element X
to the end of every element in Xss
, e.g.
?- toAdd(g, [[e],[b,c,f],[k,h]], Yss).
Yss = [[e,g],[b,c,f,g],[k,h,g]]. % expected result
I know how to add an element to a list but lists of lists confuse me.
I wrote this code that adds to the end of one list, but not sublists.
add(X,[],[X]).
add(X,[A|L],[A|L1]) :-
add(X,L,L1).