将复杂术语转换为列表列表,然后使用修改后的仿函数返回术语
Convert complex term to List of lists and then back to term with modified functor
您可以使用 =.. 来转换简单的术语。
?- x(a,b,c) =.. A.
A = [x, a, b, c].
复杂的术语呢:
x(a,b(c,d)) ==> [x,a,[b,c,d]]
x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]]
然后作为单独的任务,我想用更改后的函子重新生成条款:
x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d))
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]
您可以使用 =.. 来转换简单的术语。
?- x(a,b,c) =.. A.
A = [x, a, b, c].
复杂的术语呢:
x(a,b(c,d)) ==> [x,a,[b,c,d]]
x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]]
然后作为单独的任务,我想用更改后的函子重新生成条款:
x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d))
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]