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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12939425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:54:37  来源:igfitidea点击:

Prolog, access specific member of list?

listprologunification

提问by David Carpenter

Can anyone tell me how to access a specific member of a list in prolog? Say for example I need to access the 3rd or 4th element of a list passed into a rule?

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

回答by joel76

nth0(Ind, Lst, Elem)or nth1(Ind, Lst, Elem)with SWI-Prolog, nth0the first element has the index 0.

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

For example,

例如,

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.

回答by CapelliC

When the indexes you need to access are so small, you could use pattern matching. Say we need the third element or fourth:

当您需要访问的索引非常小时,您可以使用模式匹配。假设我们需要第三个或第四个元素:

third([_,_,E|_], E).
fourth([_,_,_,E|_], E).

This could be more useful if used 'inline', when the list carries info with positional relevance. For instance

如果使用“内联”,当列表携带具有位置相关性的信息时,这可能更有用。例如

your_rule([_,_,E|Rest], Accum, Result) :-
   Sum is Accum + E,
   your_rule(Rest, Sum, Result).
...

回答by Nicholas Carey

A prolog list is a classic list. Access is not direct. You have to iterate over to to find what you need.

序言列表是一个经典的列表。访问不是直接的。您必须迭代以找到您需要的内容。

You can get the nth element this way:

您可以通过以下方式获取第 n 个元素:

foo( [X1,X2,X3,X4,...,XN|Xs] ) :- ...

where [code]X[/code]nis the nthelement of the list. Impractical for nlarger than a small value. This is roughly analogous to a C/C++ pointer expression:

其中 [code]X[/code] n是列表的第 n 个元素。对于大于小值的n 是不切实际的。这大致类似于 C/C++ 指针表达式:

LLNode *nthElement = root->next->...->next ;

Otherwise, you have to iterate over the list to find the desired element, using a built-in predicate or a home-brew predicate, something like:

否则,您必须遍历列表以找到所需的元素,使用内置谓词或自制谓词,例如:

foo(Xs) :- nth_element(Xs,9,X) , ...

foo(Xs) :- nth_element(Xs,9,X) , ...

nth_element(Xs,N,X) :- nth_element(Xs,0,N,X) .

nth_element(Xs,N,X) :- nth_element(Xs,0,N,X) 。

nth_element([X|Xs],N,N,X) :- !. nth_element([_|Xs],T,N,X) :- T1 is T+1 , nth_element(Xs,T1,N,X).

nth_element([X|Xs],N,N,X) :- !. nth_element([_|Xs],T,N,X) :- T1 是 T+1 , nth_element(Xs,T1,N,X)。

回答by Anderson Green

Using the funclibrary for SWI-Prolog, it is possible to write list comprehensions in a more concise way:

使用funcSWI-Prolog 库,可以以更简洁的方式编写列表推导式:

:- use_module(library(func)).

nth0((Index, List), Result) :-
    nth0(Index,List,Result).

Now, you can access two elements of the list and add them together like this:

现在,您可以访问列表的两个元素并将它们添加在一起,如下所示:

example :-
    List = [1,5,12,9],
    Y is (nth0 $ (0, List)) + (nth0 $(3,List)), %add the 1st and 4th elements of this list
    writeln(Y). %prints 10