Re: How to replace a term to the inside of an other term?
- From: student <logic4sure@xxxxxxxxx>
- Date: Thu, 25 May 2006 03:16:31 GMT
-----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-----
.
- Follow-Ups:
- 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: Joachim Schimpf
- Re: How to replace a term to the inside of an other term?
- 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
- How to replace a term to the inside of an other term?
- Prev by Date: Re: count of result sets
- Next by Date: Re: String is not string in SWI prolog , part/2.
- Previous by thread: Re: How to replace a term to the inside of an other term?
- Next by thread: Re: How to replace a term to the inside of an other term?
- Index(es):
Relevant Pages
|
|