Convert complex term to List of lists and then back to term with modified functor
问题 you can use =.. to convert simple terms. ?- x(a,b,c) =.. A. A = [x, a, b, c]. what about complex terms : x(a,b(c,d)) ==> [x,a,[b,c,d]] x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]] then as separate task i want to re-generate the terms with changed functor : x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d)) 回答1: deep_univ(X, Y) :- X =.. [H|Rest], ( Rest = [] -> Y = X ; maplist(deep_univ, Rest, ExpRest), Y=[H|ExpRest] ). rev_univ([H|Rest], Y) :- maplist(rev_univ, Rest, RestT), Y =.. [H|RestT]. rev_univ(H, H