I am getting a hard time figuring out how to replace some leaves in a Prolog tree constant.
So e.g. substitute(a(b,c,d,f(c,d,e),c),X)
should replace all c
in the tree and the output should be like X=a(b,xyz,d,f(xyz,d,e),xyz)
.
I tried and got somewhat a shallow predicate
substitute(X,Y):- X =..A , myfun(A,Z), Y=..Z .
myfun([],[]).
myfun([c|T],[xyz|Z]):- myfun(T,Z), !.
myfun([H|T], [H|Z]):- myfun(T,Z).
So what this code achieves is, for input substitute(a(b,c,d,f(c,d,e),c),X)
it gives X=a(b,xyz,d,f(c,d,e),xyz)
.
But i want it to be deep. Any help is much appreciated.