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
Related Articles
- Appends Two Lists using Prolog Source Code
- Delete all occurrences of an Element from a List using Prolog Source Code
- Delete the Nth Element from the List using Prolog Source Code
- List Length Function using Prolog Source Code
- List Member Function using Prolog Source Code
- List Merge Function using Prolog Source Code
- MergeSort Function using Prolog Source Code
- Powersort Function using Prolog Source Code
