Subject: substitute

From: aikGuitarist <[email-masked]>
Date: 19 Nov 00 16:42:12 EST

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