Prolog 序言:查找列表中元素的最后一个索引

Prolog 序言:查找列表中元素的最后一个索引,prolog,Prolog,假设我有一个列表[1,2,1,3,2,0,8,3,1],我想找到最后3个的索引,它是7,如何在prolog中执行它?类似于 last(List, Needle, Ret) :- last1(List, Needle, 0, -1, Ret). last1([H | T], N, Idx, Acc, Ret) :- Idx2 is Idx + 1, (H == N, !, last1(T, N, Idx2, Idx, Ret); last1(T, N, Idx2, Acc, Ret)). las

假设我有一个列表
[1,2,1,3,2,0,8,3,1]
,我想找到最后3个的索引,它是
7
,如何在prolog中执行它?

类似于

last(List, Needle, Ret) :- last1(List, Needle, 0, -1, Ret).

last1([H | T], N, Idx, Acc, Ret) :- Idx2 is Idx + 1, (H == N, !, last1(T, N, Idx2, Idx, Ret); last1(T, N, Idx2, Acc, Ret)).
last1([], _, _, Acc, Acc).
这将遍历整个列表,保持最后看到的针的索引。这是家庭作业吗