Welcome to W3Courses
 Like Us on Facebook

Replace Function using Prolog Source Code

The following code replaces an element with a new element in the list

 CODE
 replace([],A,B,[]).
 replace([H|T],A,B,[B|Result]) :- H=A, replace(T,A,B,Result).
 replace([H|T],A,B,[H|Result]) :- replace(T,A,B,Result).


 RESULT
 ?- replace([a,b,a,c,a,d],a,w,R).
 R = [w, b, w, c, w, d]
 Yes

Share