list - Prolog,访问列表的特定成员?

标签 list prolog unification

谁能告诉我如何访问序言中列表的特定成员?比如说我需要访问传递给规则的列表的第三个或第四个元素?

最佳答案

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/

相关文章:

list - 大于 X 的第一个元素的索引(Prolog)

prolog - 在 Prolog 中比较两个原子

Prolog:算术表达式和常数的统一

prolog - 如何在所有参数模式的后继算术中实现阶乘序列?

prolog - PROLOG 中的填字游戏解算器

recursion - Prolog中的统一算法可以无限递归吗?

scheme - minikanren 中的特征结构统一

javascript - jQuery 按 <i> 将列表拆分并重组为新列表

javascript - 使用 jQuery 列表项值始终为 0

python - 我想按升序对所有字典进行排序,但每当我得到相同的值时,按降序对它们进行排序