Success: term_variables(atom, []). term_variables(Term, L). % gives L = [Term] term_variables(f(a,B,c), L). % gives L = [B] term_variables([X,Y,Z], L). % gives L = [Z,Y,X] term_variables([X,Y,X], L). % gives L = [Y,X] term_variables(s(X{a}), L). % gives L = [X{a}] Fail: term_variables(f(a,B,c), []).