How to add to end of list in prolog

I am trying to add one item to the end of a list in prolog, but it keeps on failing.

insertAtEnd(X,[ ],[X]).
insertAtEnd(X,[H|T],[H|Z]) :- insertAtEnd(X,T,Z).    

letters([a,b,c]).

I do not understand why this below does not work.

insertAtEnd(d,letters(Stored),letters(Stored)). 

I am also attempting to store this list in the variable Stored throughout, but I am not sure if the above is correct way to proceed.


Prolog implements a relational computation model, and variables can only be instantiated, not assigned. Try

?- letters(Stored),
   insertAtEnd(d, Stored, Updated),
   write(Updated).

you can use append and put your item as second list

like this:

insertAtEnd(X,Y,Z) :- append(Y,[X],Z).

链接地址: http://www.djcxy.com/p/65492.html

上一篇: 使用Ajax进行Omniauth授权调用

下一篇: 如何在序言中添加到列表的结尾