The question was to create a replace/4
predicate that would replace a certain element (X
) from the first list with another element (Y
) like x
and finally store it into the last argument, a new list. I know there is obviously something wrong with my base case (?), but I can't seem to figure it out. When I trace this code, it starts of normal, but after the first list is empty, it starts adding anonymous variables. Please have mercy, I'm new to Prolog.
replace([], _, _, []).
replace([H|T], X, Y, N):-
H = X,
append(N, [Y], NL),
replace(T, X, Y, NL).
replace([H|T], X, Y, N):-
H \= X,
append(N, [H], NL),
replace(T, X, Y, NL).