Given a list of the format,
[item(a), item(b), item(c), other(d), other(e) ...]
where the number of items isn't fixed, nor is the number of others, but items always precede other, how do I split the list so that I can pass the items and the others into different predicates.
I've been trying to find ways to split the list based on the elements but can't figure out a way to do so.
I need to write a predicate that will take this list, and then pass the items to an itemPredicate
, and the others to an otherPredicate
.
If there's any other info I can provide please let me know.
A simple split predicate that separates item(_)
from other(_)
could work as follows:
split([], [], []).
split([item(A) | T], [item(A) | IT], OL) :- split(T, IT, OL).
split([other(A) | T], IL, [other(A) | OT]) :- split(T, IL, OT).
And use it like this:
?- split([item(1), other(2), other(3), item(4), other(5)], X, Y).
X = [item(1), item(4)],
Y = [other(2), other(3), other(5)].
It doesn't even require that item
s always precede other
s.