遍历两个列表的相同索引位置。

huangapple go评论81阅读模式
英文:

Iterate through two lists at same index

问题

你可以通过以下方式迭代两个具有相同索引的列表,以获取每个列表的一对元素:

?- X = [1, 2, 3], Y = [a, b, c], ...
ElemX = 1,
ElemY = a;

ElemX = 2,
ElemY = b;

ElemX = 3,
ElemY = c.
英文:

How can you iterate through two lists with the same index, so that you get a pair of each list:

?- X = [1, 2, 3], Y = [a, b, c], ...
ElemX = 1,
ElemY = a;

ElemX = 2,
ElemY = b;

ElemX = 3,
ElemY = c.

答案1

得分: 1

在两个列表同时循环:

member2(E1, [H1|T1], E2, [H2|T2]) :-
    member2_(T1, T2, H1, H2, E1, E2).

member2_(_, _, E1, E2, E1, E2).
member2_([H1|T1], [H2|T2], _, _, E1, E2) :-
    member2_(T1, T2, H1, H2, E1, E2).

在 swi-prolog 中的结果:

?- member2(E1, [1, 2, 3], E2, [a, b, c]).
E1 = 1,
E2 = a ;
E1 = 2,
E2 = b ;
E1 = 3,
E2 = c.
英文:

Loop through both lists simultaneously:

member2(E1, [H1|T1], E2, [H2|T2]) :-
    member2_(T1, T2, H1, H2, E1, E2).

member2_(_, _, E1, E2, E1, E2).
member2_([H1|T1], [H2|T2], _, _, E1, E2) :-
    member2_(T1, T2, H1, H2, E1, E2).

Result in swi-prolog:

?- member2(E1, [1, 2, 3], E2, [a, b, c]).
E1 = 1,
E2 = a ;
E1 = 2,
E2 = b ;
E1 = 3,
E2 = c.

答案2

得分: 1

你可以使用 pairs_keys_values/3 就像它是一个 zip() 函数一样:

?- X = [1, 2, 3],
   Y = [a, b, c],
   pairs_keys_values(Pairs, X, Y).

Pairs = [1-a, 2-b, 3-c].

然后可以通过这样迭代。

英文:

You can use pairs_keys_values/3 as if it was a zip() function:

?- X = [1, 2, 3],
   Y = [a, b, c],
   pairs_keys_values(Pairs, X, Y).

Pairs = [1-a, 2-b, 3-c].

And then iterate through that.

答案3

得分: 1

假设这个练习的目的是自己找出如何做而不是使用内置谓词,似乎这不应该比以下代码更复杂:

zip([X|_], [Y|_], X:Y).
zip([_|Xs], [_|Ys], X:Y) :- zip(Xs, Ys, X:Y).
英文:

Assuming the point of the exercise is to figure out how to do this on your own rather than using built-in predicates, it doesn't seem like it should be any more complicated than

zip( [X|_ ] , [Y|_ ] , X:Y ) .
zip( [_|Xs] , [_|Ys] , X:Y ) :- zip(Xs,Ys,X:Y) .

答案4

得分: 0

使用nth0(?Index, ?List, ?Elem)

_X = [1, 2, 3], _Y = [a, b, c], nth0(_Index, _X, ElemX), nth0(_Index, _Y, ElemY).
英文:

Use nth0(?Index, ?List, ?Elem):

_X = [1, 2, 3], _Y = [a, b, c], nth0(_Index, _X, ElemX), nth0(_Index, _Y, ElemY).

答案5

得分: 0

我已经在SWI Prolog上测试了所有三个答案,并添加了一个使用maplist/3的答案。代码如下:

member2(E1, [H1|T1], E2, [H2|T2]) :-
    member2_(T1, T2, H1, H2, E1, E2).

member2_(_, _, E1, E2, E1, E2).
member2_([H1|T1], [H2|T2], _, _, E1, E2) :-
    member2_(T1, T2, H1, H2, E1, E2).

pair(A,B,(A,B)).

test_member2(Size):-
    length(L0,Size),
    length(L1,Size),
    findall((A,B),member2(A, L0, B, L1),_).
   
test_pkv(Size):-
    length(L0,Size),
    length(L1,Size),
    findall(P,pairs_keys_values(P, L0, L1),_).

test_nth0(Size):-
    length(L0,Size),
    length(L1,Size),
    findall((X,Y),(nth0(Index, L0, X), nth0(Index, L1, Y)),_).

test_maplist(Size):-
    length(L0,Size),
    length(L1,Size),
    maplist(pair,L0,L1,_).

以下是测试结果:

?- time(test_member2(10000)).
20,014 inferences, 0.024 CPU in 0.024 seconds (100% CPU, 847596 Lips)

?- time(test_pkv(10000)).
10,016 inferences, 0.019 CPU in 0.019 seconds (100% CPU, 519172 Lips)

?- time(test_nth0(10000)).
60,013 inferences, 0.352 CPU in 0.352 seconds (100% CPU, 170474 Lips)

?- time(test_maplist(10000)).
20,006 inferences, 0.014 CPU in 0.014 seconds (100% CPU, 1462905 Lips)
英文:

I've tested all the three answers on SWI Prolog and added one which uses maplist/3.
The code is the following

member2(E1, [H1|T1], E2, [H2|T2]) :-
    member2_(T1, T2, H1, H2, E1, E2).

member2_(_, _, E1, E2, E1, E2).
member2_([H1|T1], [H2|T2], _, _, E1, E2) :-
    member2_(T1, T2, H1, H2, E1, E2).

pair(A,B,(A,B)).

test_member2(Size):-
    length(L0,Size),
    length(L1,Size),
    findall((A,B),member2(A, L0, B, L1),_).
   
test_pkv(Size):-
    length(L0,Size),
    length(L1,Size),
    findall(P,pairs_keys_values(P, L0, L1),_).

test_nth0(Size):-
    length(L0,Size),
    length(L1,Size),
	findall((X,Y),(nth0(Index, L0, X), nth0(Index, L1, Y)),_).

test_maplist(Size):-
    length(L0,Size),
    length(L1,Size),
    maplist(pair,L0,L1,_).

Here the results:

?- time(test_member2(10000)).
20,014 inferences, 0.024 CPU in 0.024 seconds (100% CPU, 847596 Lips)

?- time(test_pkv(10000)).
10,016 inferences, 0.019 CPU in 0.019 seconds (100% CPU, 519172 Lips)

?- time(test_nth0(10000)).
60,013 inferences, 0.352 CPU in 0.352 seconds (100% CPU, 170474 Lips)

?- time(test_maplist(10000)).
20,006 inferences, 0.014 CPU in 0.014 seconds (100% CPU, 1462905 Lips)

huangapple
  • 本文由 发表于 2023年6月26日 05:43:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定