开发者

Prolog, recursive functions, and returning value by argument of a function

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜