0
我試圖找到列表的第一個和最後一個元素,使lastFirstElt(X,L)輸出列表L只包含第一個元素和最後一個元素。我能夠獲得第一和最後一個元素,我是想這個組合成一個只包含一個第一列表L和最後一個元素,但我有點不能讓它工作:序言:獲取列表中的某些元素
first(X, [X|_]). % to get first element
last(X, [X]).% last element if it is only one
last(X,[_|T]) :- last(X,T). % otherwise recurs to the last
lastFirstElt(X, L):- first(X,L), last(X,L). % Here, I was expecting the first and last element will be joined into List L