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



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Joachim Schimpf wrote:
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.


And it doesn't necessarily mean I don't want to do it, either.

If you wish to restrict the replacement of occurrences a functor that
may occur with different arities in the same term to occurrences that
have some particular arity then you must specify which one it is.


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


I don't seem to be able to reproduce that result.

Here is the my program in full:

replace(T,A,B,T1) :-
T =.. [P|TL],
( P = A -> P1 = B ; P1 = P ),
replaceL(TL,A,B,TL1),
T1 =.. [P1|TL1].

replaceL([],_,_,[]).
replaceL([T|TL],A,B,[T1|TL1]) :-
replace(T,A,B,T1),
replaceL(TL,A,B,TL1).

With this program and SWI-Prolog Version 5.4.6
I get

?- replace(4, 4, h(999), T1).
T1 = h(999) ;
No

- --
billh

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEdSGumuz1B9Tdo+wRArc8AKCOwwv25aZhKdjOEM3YyA/EdvxtQwCfdvFf
+bSqgKga+N5WAt/MIZWQuDg=
=7d+n
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: subject line
    ... occurrences of a pattern, change the next 50 occurrences of a different ... In today's multi-core, multi-GHz ... personal computers something like this would not be too noticeable time ... Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org ...
    (comp.lang.ruby)
  • Re: calendar-hack.el for xemacs
    ... You need GnuPG to verify this message ... Replace all occurrences of `overlay-put' with `set-extent-property' ... Another solution, if you have the fsf-compat XEmacs package, ... Comment: Eicq - The SXEmacs ICQ Client ...
    (comp.emacs.xemacs)
  • Re: subject line
    ... occurrences of a pattern, change the next 50 occurrences of a different ... personal computers something like this would not be too noticeable time ... Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org ...
    (comp.lang.ruby)