Reputation: 109
I have this problem in prolog to solve , this are the goals
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).
but its not working....
Upvotes: 0
Views: 90
Reputation: 4336
If you're allowed to use term_variables and don't mind the lack of elegance:
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).
A lot more explicit than needed (i.e. 'New = Old' instead of using the same variable through and through) and with cuts, but it works for all of your examples.
Upvotes: 1