谁能告诉我如何在序言中访问列表的特定成员?举例来说,我需要访问传递到规则中的列表的第3或第4个元素?

最佳答案

带有SWI-Prolog的nth0(Ind, Lst, Elem)nth1(Ind, Lst, Elem)nth0第一个元素的索引为0。

例如,

nth0(3, [a, b, c, d, e], Elem). %Binds d to Elem
nth1(3, [a, b, c, d, e], Elem). %Binds c to Elem

nth0(Ind, [a, b, c, d, e], d).  %Binds 3 to Ind
nth0(3, [a, b, c, X, e], d).    %Binds d to X

nth0(3, [a, b, c, d, e], c).    %Fails.

关于list - Prolog,访问列表的特定成员?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12939425/

10-10 19:43