Re: How to replace a term to the inside of an other term?



student wrote:
I would begin by deciding what I meant by the word "term" in this
context. Here is a handy way of looking at terms in Prolog:

/*

Term =.. [ P | TL ]
- --------------------------------
a =.. [ a | [] ]
[] =.. [ [] | [] ]
f(1,2,3) =.. [ f | [1,2,3] ]
[1,2,3] =.. [ '.' | [1,[2,3]] ]
- --------------------------------
*/
....

?- replace(f(1,f(2,f(3,[]))),f,g,T1).
T1 = g(1, g(2, g(3, []))) ;

This is a somewhat un-Prolog-ish view of things, because it
neglects the significance of the arity of a functor. Just
because you want to replace f/2 terms by g/2 terms doesn't
necessarily mean you want to do it for other arities as well.


student wrote:
?- replace(f(1,g(2,[2,4,5])),4,h(999),T1).
T1 = f(1, g(2, [2, h(999), 5])) ;
No

What about
?- replace(4, 4, h(999), T1).
type error in T1 =.. [h(999)]
Abort


-- Joachim
.