Convert complex term to List of lists and then back to term with modified functor

落花浮王杯 提交于 2021-02-11 12:28:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!