Replace occurrence of a list with another?
- From: brianbasquille@xxxxxxxxx
- Date: 24 Apr 2006 16:15:12 -0700
Hello all,
Need to replace all occurrences of an element in another list with
another? X being the element i wish to replace and Y being the element
to replace it with so when i issue:
?- replaceall(a,b,[a,b,c,d],L).
It should return L = [b, b, c, d] but i get two values back for L;
L = [b, b, c, d] ;
L = [a, b, c, d] ;
It seems to be returning both the new list and the old list.. why is
this? The predicate is below:
replaceall(X,Y,[H|T],[Y|List]) :-
X = H,
replaceall(X,Y,T,List).
replaceall(X,Y,[H|T],[H|List]) :-
replaceall(X,Y,T,List).
replaceall(_,_,[],[]).
Many thanks in advance!
PS - using SWI-Prolog by the way.
PPS - and how would i change this to just replace the first occurence?
.
- Follow-Ups:
- Re: Replace occurrence of a list with another?
- From: Bill Spight
- Re: Replace occurrence of a list with another?
- Prev by Date: accessing head of list
- Next by Date: Re: accessing head of list
- Previous by thread: accessing head of list
- Next by thread: Re: Replace occurrence of a list with another?
- Index(es):