0

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))
Machavity
  • 30,841
  • 27
  • 92
  • 100
sten
  • 7,028
  • 9
  • 41
  • 63

1 Answers1

2
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] 
rajashekar
  • 3,460
  • 11
  • 27