From pascal@rz.hu-berlin.de  Fri Jan  7 10:12:49 2000
Received: from suncom.rz.hu-berlin.de (suncom.rz.hu-berlin.de [141.20.1.31])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id KAA28352
	for <prolog@swi.psy.uva.nl>; Fri, 7 Jan 2000 10:12:49 +0100 (MET)
Received: from localhost.rz.hu-berlin.de (ppp196-160.rz.hu-berlin.de [141.20.196.160])
	by suncom.rz.hu-berlin.de (8.9.3/8.9.3) with ESMTP id KAA01933;
	Fri, 7 Jan 2000 10:12:49 +0100 (MET)
Received: (from pascal@localhost)
	by localhost (8.8.8/8.8.8/Debian/GNU) id KAA00606;
	Fri, 7 Jan 2000 10:12:56 +0100
Date: Fri, 7 Jan 2000 09:12:56 +0000 (GMT)
From: Pascal Vaillant <Pascal.Vaillant@rz.hu-berlin.de>
To: Douglas Miles <a-doug@microsoft.com>
cc: prolog@swi.psy.uva.nl
Subject: Re: I am having trouble writing replace/4 
In-Reply-To: <E713F2760348D211A9B600805F6FA1AB03559908@RED-MSG-09.itg-messaging.redmond.corp.microsoft.com>
Message-ID: <Pine.LNX.4.05.10001070910070.603-100000@torstrasse>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII


> replaceList([],[],NothingToDo,NothingToDo):-!.
> replaceList([HeadBefore|TailBefore],[HeadAfter|TailAfter],Start,End):-!,
>         replace(HeadBefore,HeadAfter,Start,Midde),
>         replaceList(TailBefore,TailAfter,Middle,End).
> 
> replace(_, _, [], []):-!.
> replace(A, B, [A|L], [B|R]) :- !,    replace(A, B, L, R).
> replace(A, B, [C|L], [C|R]) :-   replace(A, B, L, R).
> 
> here is my trouble..
> 
> ?- replaceList([a,b],[A,B],[1,2,3,a,b,c,x,y,z],O).
> A = b
> B = _G422
> O = [1, 2, 3, _G422, _G422, c, x, y, z]
> yes
> 
> I guess the real question is how can I write a replace only once..  or
> rewrite replaceList?
> 
> I want it to do this...
> 
> ?- replaceList([a,b],[A,B],[1,2,3,a,b,c,x,y,z],O).
> A = _G421
> B = _G422
> O = [1, 2, 3, _G421, _G422, c, x, y, z]
> yes
> 
> is copy_term/2 the key? I have tried a few tricks..
> or maybe numbervars can help?
> 


No, it's simply because your test (second clause of replace/4) involves
unification, and not only equivalence. Hence, after having replaced a
by A, your current list becomes [b, etc.], and you try, at that point, to
unify A with b (which it has no reason to refuse :-)

This works :

replaceList([],[],NothingToDo,NothingToDo).

replaceList([HeadBefore|TailBefore],[HeadAfter|TailAfter],Start,End) :-
  replace(HeadBefore,HeadAfter,Start,Middle),
  replaceList(TailBefore,TailAfter,Middle,End).


replace(_,_,[],[]).

replace(A,B,[C|L],[D|R]) :-
  ((A==C) *->
   D=B ;
   D=C),
  replace(A,B,L,R).


Tschuess,

Pascal Vaillant
Berlin



