谁能告诉我如何访问序言中列表的特定成员?比如说我需要访问传递给规则的列表的第三个或第四个元素?
最佳答案
nth0(Ind, Lst, Elem)
或 nth1(Ind, Lst, Elem)
使用 SWI-Prolog,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/