Re: How to replace a term to the inside of an other term?
- From: student <logic4sure@xxxxxxxxx>
- Date: Fri, 26 May 2006 15:01:27 GMT
Joachim Schimpf wrote:
Beautiful stuff, especially the concept of patterns with variables
that somehow end up not being instantiated, but I will have to
bone up on 'functor/3', 'copy_term/2', and 'arg/3' to understand it.
Oh, functor+arg vs =.. are just a matter of preference.
Apart from the line with the copy_term, your t_replace/4 from
your other posting does exactly the same as my code.
Well, well!
Let me see if I can make it work using 'copy_term/2'.
t_replace(T,A,B,T1) :-
T =.. [F|TL],
copy_term([A,B],[AA,BB]), /* jointly rename variables in A and B */
t_replaceL(TL,AA,BB,TL0),
T0 =.. [F|TL0],
( T0 = AA -> T1 = BB ; T1 = T0 ).
t_replaceL([],_,_,[]).
t_replaceL([T|TL],A,B,[T1|TL1]) :-
t_replace(T,A,B,T1),
t_replaceL(TL,A,B,TL1).
?- t_replace(3*3 + 4*4, X*X, twice(X), Out).
Out = twice(3)+twice(4)
?- t_replace(foo(1,foo(2,3)), foo(X,Y), bar(Y,X), Out).
Out = bar(bar(3,2),1)
?- t_replace(foo(bar(baz)), foo(bar(X)), foobar(X), Out).
Out = foobar(baz)
?- t_replace(4, 4, h(999), T1). /* using gprolog */
T1 = h(999)
?- t_replace(f(a),f,g(1),T1).
T1 = f(a)
?- t_replace([foo,f(f(f(1)))],1,bar,T1).
T1 = [foo,f(f(f(bar)))]
?- t_replace([foo,f(f(f(1)))],f(X),X,T1).
T1 = [foo,1]
By Jove, it works!
Thank you.
--
billh
.
- References:
- How to replace a term to the inside of an other term?
- From: Mika
- Re: How to replace a term to the inside of an other term?
- From: student
- Re: How to replace a term to the inside of an other term?
- From: Joachim Schimpf
- Re: How to replace a term to the inside of an other term?
- From: student
- Re: How to replace a term to the inside of an other term?
- From: Joachim Schimpf
- Re: How to replace a term to the inside of an other term?
- From: student
- Re: How to replace a term to the inside of an other term?
- From: Joachim Schimpf
- How to replace a term to the inside of an other term?
- Prev by Date: ! and ->
- Next by Date: Re: ! and ->
- Previous by thread: Re: How to replace a term to the inside of an other term?
- Next by thread: new to prolog
- Index(es):
Relevant Pages
|
|