From rhoekstr@wins.uva.nl  Fri Jan  7 12:35:00 2000
Received: from smtp2.a2000.nl (duck.a2000.nl [62.108.1.88])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id MAA03493
	for <prolog@swi.psy.uva.nl>; Fri, 7 Jan 2000 12:35:00 +0100 (MET)
Received: from node10e0e.a2000.nl ([24.132.14.14] helo=RHINO)
	by smtp2.a2000.nl with smtp (Exim 2.02 #4)
	id 126Xfc-0001WU-00; Fri, 7 Jan 2000 12:35:04 +0100
From: "Rinke Hoekstra" <rhoekstr@wins.uva.nl>
To: "Prolog Mailing List" <prolog@swi.psy.uva.nl>
Cc: <Pascal.Valliant@rz.hu-berlin.de>, <a-doug@microsoft.com>
Subject: RE: I am having trouble writing replace/4 
Date: Fri, 7 Jan 2000 12:42:15 +0100
Message-ID: <LFEJKFIDOJEOIHAFBNBFIEBBCCAA.rhoekstr@wins.uva.nl>
MIME-Version: 1.0
Content-Type: text/plain;
	charset="US-ASCII"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0)
In-Reply-To: <Pine.LNX.4.05.10001070910070.603-100000@torstrasse>
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600

It could be done a lot quicker:

replace_list([],[],List,List).
replace_list([HReplacant|TReplacant],[HReplacer|TReplacer],List,NewList):-
	replace(HReplacant,HReplacer,List,IList),
	replace_list(TReplacant,TReplacer,IList,NewList).

replace(_,_,[],[]).
replace(HReplacant,HReplacer,[HReplacant|Tail],[HReplacer|NewTail]):-
	replace(HReplacant,HReplacer,Tail,NewTail).
replace(HReplacant,HReplacer,[Head|Tail],[Head|NewTail]):-
	replace(HReplacant,HReplacer,Tail,NewTail).

This algorithm takes less steps (trace it to find out).

Regards,

	Rinke Hoekstra
	Amsterdam

-----Original Message-----
From: Pascal Vaillant [mailto:Pascal.Vaillant@rz.hu-berlin.de]
Sent: vrijdag 7 januari 2000 10:13
To: Douglas Miles
Cc: prolog@swi.psy.uva.nl
Subject: Re: I am having trouble writing replace/4



> 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




----------------
* To UNSUBSCRIBE, please use the HTML form at

    http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist

or send mail to prolog-request@swi.psy.uva.nl using the Subject:
"unsubscribe"
(without the quotes) and *no* message body.

** An ARCHIVE of this list is maintained at

    http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/


