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



student wrote:
Joachim Schimpf wrote:
...
replace(In, OldTmpl, NewTmpl, Out) :-
functor(In, F, N),
functor(In1, F, N),
replace_args(N, In, OldTmpl, NewTmpl, In1),
( copy_term(OldTmpl-NewTmpl, Old-New), In1 = Old ->
Out = New
;
Out = In1
).

replace_args(I, In, OldTmpl, NewTmpl, Out) :-
( I > 0 ->
arg(I, In, InArg),
arg(I, Out, OutArg),
replace(InArg, OldTmpl, NewTmpl, OutArg),
I1 is I-1,
replace_args(I1, In, OldTmpl, NewTmpl, Out)
;
true
).


-- Joachim

Beautiful stuff, especially the concept of patterns with variables
that somehow end up not being instantiated, but I will have to find time
to bone up on 'functor/3', 'copy_term/3', 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.

- Joachim
.



Relevant Pages