Using lists of lists usually hints at a bad representation. Several systems provide a flatten/2
library predicate over lists that may be useful, however, if you cannot avoid this representation. Assuming that the list of lists in your example have been flatted, you should be able to write a simple recursive predicate to walk over the list and process each element that satisfies your criteria. Something like:
process([]).
process([Element| Elements]) :-
( Element = obj2(Arg) ->
do_something(Arg)
; true
),
process(Elements).
Details, however, will depend on several factors that you don't specify. For example, what does it mean if processing of an obj2(_)
element fails? Failure of processing the list (as in the predicate sketched above)? Continue to the next element? Are the list elements independent or may share variables? Some Prolog systems also provide foreach/2
and forall/2
predicates that might be useful for a compact (but not necessarily more efficient) solution.