Как я уже упоминал в моих комментариях, решение должно быть рекурсивным и становится немного более сложным.
Вот возможное решение, в котором я оставил пустые места для вас, чтобы разобраться:
% We'll use an auxiliary predicate that handles a list of terms
term_args(Term, Args) :-
term_list_args([Term], Args).
% This predicate recursively handles a list of terms, each of which is or is not be compound
% ...so you have to check!
term_list_args([Term|Terms], Args) :-
( compound(Term)
-> Term =.. [_|TermArgs], % If this term is a compound term
___, % Recursively process the term's arguments (TermArgs)
___, % Recursively process the remaining terms (Terms)
___ % append the results of the two queries above to obtain Args
; % Otherwise, if Term is not compound
___, % Recursively process the remaining terms (Terms) - obtaining TermArgs
Args = [Term|TermArgs]
).
term_list_args([], []). % base case: An empty list of terms corresponds to an empty arg list
Попробуйте заполнить пробелы выше.Обратите внимание, что «рекурсивно» означает, что вы собираетесь снова что-то вызвать term_list_args/2
.