简体   繁体   English

用列表的第N个值统一术语

[英]Unifying a term with the Nth value of a list

I'm having trouble implementing a Prolog predicate, vector_reference(L,N,V) , so that when given a list "L" and an index "N" it will unify V with the Nth value of the list. 我在实现Prolog谓词vector_reference(L,N,V)时遇到麻烦,因此当给定列表“ L”和索引“ N”时,它将使V与列表的第N个值统一。

The example print outs are below: 示例打印输出如下:

?- vector_reference([2,3,4], 3, V).
V = 4

?- vector_reference([2, [2,3],’hello’]),2,V).
V = [2,3]

Any help would be GREATLY appreciated. 任何帮助将不胜感激。

In the standard library, library(lists) (autoloaded, you don't need to do anything) there are two predicates: nth0/3 and nth1/3 that do exactly what you need. 在标准库中, library(lists) (自动加载,您无需执行任何操作)有两个谓词: nth0/3nth1/3可以完全满足您的需要。 For example: 例如:

?- nth1(2, [2,[2,3],hello], V).
V = [2, 3].

Btw, the SWI-Prolog webpage has a very useful search function. 顺便说一句,SWI-Prolog网页具有非常有用的搜索功能。 Typing nth in there would have given you the answer. 在其中输入nth会给您答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM