Question is simply... why this doesn't work?
replace([l|[r|[r|[l|R]]]], Result) :- append([f,f],R,Result).
replace([HEAD|TAIL], Result) :- replace(TAIL, Y), append(HEAD,Y,Result).
?- replace([l,r,r,l,r,r],X).
returns
X = [f, 开发者_StackOverflow社区f, r, r] .
but
?- replace([r,l,r,r,l,r,r],X).
gives
false
why not X = [r, f, f, r, r] .
?
please help.
PS. I am still learning English, however sometimes I am making stupid mistakes. I am doing my best, writing questions and answers in English, however I would be very glad if you will edit my post in case of any mistakes. I promise, your effort will not be wasted.
You need append([HEAD],Y,Result) instead of append(HEAD,Y,Result). HEAD is not a list by oitself.
精彩评论