问题
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) :- \+ is_list(H).
?- T=x(a,b,c(d,e(f)), j), deep_univ(T, X), rev_univ(X, Y).
T = Y, Y = x(a, b, c(d, e(f)), j),
X = [x, a, b, [c, d, [e, f]], j]
来源:https://stackoverflow.com/questions/65678387/convert-complex-term-to-list-of-lists-and-then-back-to-term-with-modified-functo