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



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
.



Relevant Pages

  • Re: may Byrons cheap hemisphere favours, Blanche learns in view of dull, cruel lodges
    ... patterns, it'll somehow wonder the trace. ... You'll win vats. ...
    (rec.games.roguelike.nethack)
  • Re: Definition of Music
    ... > Somehow this meaning has warped into a much less well-defined ... Some adjectives (eg "beautiful") are essentially purely subjective. ...
    (rec.music.compose)
  • learning, or not
    ... How do jugglers learn new patterns? ... Reading to learn patterns does not seem to be popular. ... visual aids, including, and maybe even best, the robotic animations. ... trying to "burn" it into their minds somehow, trusting that somehow, ...
    (rec.juggling)
  • Re: List comprehensions and glob
    ... Robin Becker wrote: ... > I expected I would somehow be able to use list comprehensions to do ... for p in patterns) ...
    (comp.lang.python)
  • Re: Most Beautiful Patterns?
    ... I was just wondering what everybody thinks are some of the most beautiful ... patterns for an audience. ... ball cascade and shower ... ball cascade ...
    (rec.juggling)