Note that this predicate is a generalisation of nonground/1 and nonground/2 which could be written as:
nonground(Term) :- nonground(1, Term, _). nonground(Term, Var) :- nonground(1, Term, [Var]).
Success: nonground(1, Term, L). % gives L = [Term] nonground(1, f(a,B,c), L). % gives L = [B] nonground(2, [X,Y,Z], L). % gives L = [Y,X] nonground(2, [X,Y,Z], L). % gives L = [Y,X] nonground(1, s(X{a}), L). % gives L = [X{a}] Fail: nonground(1, atom, L). nonground(2, f(a,B,c), L). nonground(2, [X,X,X], L).