From aikguitarist@usa.net  Sun Nov 19 22:42:14 2000
Received: from aw161.netaddress.usa.net (aw161.netaddress.usa.net [204.68.24.61])
	by swi.psy.uva.nl (8.9.3/8.9.3) with SMTP id WAA04612
	for <prolog@swi.psy.uva.nl>; Sun, 19 Nov 2000 22:42:13 +0100 (MET)
Received: (qmail 2365 invoked by uid 60001); 19 Nov 2000 21:42:12 -0000
Message-ID: <20001119214212.2364.qmail@aw161.netaddress.usa.net>
Received: from 204.68.24.61 by aw161 for [149.99.21.157] via web-mailer(34FM.0700.4.03) on Sun Nov 19 21:42:12 GMT 2000
Date: 19 Nov 00 16:42:12 EST
From: aikGuitarist <aikguitarist@usa.net>
To: prolog@swi.psy.uva.nl
Subject: substitute
X-Mailer: USANET web-mailer (34FM.0700.4.03)
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by swi.psy.uva.nl id WAA04612

Here is a program that I am writing

% substitute(List, Old, New, RevisedList) is true if the list RevisedList
% is the result of substituting New for all occurrences of Old in the 
% list List. 

substitute([], Old, New, []).
substitute([Old|Olds], Old, New, [New|News]):-substitute(Olds, Old, New,
News).
substitute([Temp|Olds], Old, New, [Temp,News]):-Old\=Temp,
substitute(Olds,Old, New, News).

which makes sense but when I run it with the following query see the answer I
get:

| ?- substitute([a, b, d, a], a, x, Some).
[ load module hilogsym ]
(0) Call: substitute([a,b,d,a],a,x,_94) ?
(1) Call: substitute([b,d,a],a,x,_153) ?
(2) Call: a \= b ?
(2) Exit: a \= b ?
(3) Call: substitute([d,a],a,x,_175) ?
(4) Call: a \= d ?
(4) Exit: a \= d ?
(5) Call: substitute([a],a,x,_197) ?
(6) Call: substitute([],a,x,_207) ?
(6) Exit: substitute([],a,x,[]) ?
(5) Exit: substitute([a],a,x,[x]) ?
(3) Exit: substitute([d,a],a,x,[d,[x]]) ?
(1) Exit: substitute([b,d,a],a,x,[b,[d,[x]]]) ?
(0) Exit: substitute([a,b,d,a],a,x,[x,b,[d,[x]]]) ?

Some = [x,b,[d,[x]]]

Actually it should just give me [x,b,d,x], but it throws in all those extra
brackets. How do I do that. 

Finally, I want to make it work so that it replaces occurences of Old with New
at all levels in the list. e.g. [a, b, [[a]], [a, b]] --> [x, b, [[x]], [x,b]]
...... How do i do that?

Thanx, 


____________________________________________________________________
Get free email and a permanent address at http://www.amexmail.com/?A=1

