So we can easily find and replace an atom with another atom in Prolog by doing something like:
replace([],A,B,[]).
replace([H|T],A,B,[B|Result]) :-
H=A,
replace(T,A,B,Result),!.
replace([H|T],A,B,[H|Result]) :-
replace(T,A,B,Result).
I'm sure there are other ways to do this too.
However, I want to do something more complicated in logic in computing. How would you do something like replacing conjunctions like conj(x,y)
in a logical statement with just (x,y)? So it's like final and replace but not with atoms. So we could have something like reduce(conj(conj(x,y),z)).
that I would want reducing to ((x,y),z)
.
This is a simple example with only conjunctions but this is what I want to happen in the case of conjunctions. If anyone's interested, this is all about descriptive logic and the tableau method.
What I'm confused about it how you go about doing a find and replace when the input isn't actually a list; it's a structure. I don't see how you can solve this without using the standard [H|T]
trick with recursion and lists. Has anyone got any ideas?
Many thanks.