我在prolog中有这个问题需要解决,这就是目标
a) ?- replace_var_args(father(yannis,X), [anna], NewTerm). NewTerm =
father(yannis,anna),
b) ?- replace_var_args(f(a,X,g(b), Y,h(a,Z)), [b,c], NewTerm). NewTerm =
f(a,b,g(b),c,h(a,Z)),
c) ?- replace_var_args(f(a,X,f(b),Y,h(a,Z)), [b,c,d], NewTerm). NewTerm =
f(a,b,g(b),c,h(a,d)).
d) ?- replace_var_args(t(X, Y, Z,a,f(Z,b)), [1,2,3], NewTerm). NewTerm =
t(1,2,3,a,f(Z,b)).
I have created this
replace_var_args1(Old,Term,New):-atomic(Term),Term=Old.
replace_var_args1(Old,Term,Term):-var(Term).
replace_var_args1(Old,Term,Term):-nonvar(Term),atomic(Term),Term\==Old.
replace_var_args1(Old,Term,Term1):- \+ atomic(Term),functor(Term,F,N),functor(Term1,F,N),replace_var_args2(N,Old,Term,Term1).
replace_var_args(Old,Term,New):- replace_var_args1(Old,Term,New).
但它没有用......
答案 0 :(得分:1)
如果你被允许使用term_variables并且不介意缺乏优雅:
replace_var_args(Old,LARGS,New):-
term_variables(Old,LIST),
unify_var(LIST,LARGS),
New = Old.
unify_var([],_):-!.
unify_var(_,[]):-!.
unify_var([A|T],[A|Z]):- unify_var(T,Z).
比需要更明确(即' New = Old'而不是通过和使用相同的变量)和切割,但它适用于所有示例。