简体   繁体   English

反转 Prolog 中每隔一个列表的列表

[英]Reverse every second list of lists in Prolog

I have a list containing lists and I want to reverse every second list in it.我有一个包含列表的列表,我想反转其中的第二个列表。 I tried something but if I have odd number of elements in the list the last list element is lost... So the best solution would be to put the odd lists first and the even lists second till every second list is reversed.我尝试了一些东西,但是如果我在列表中有奇数个元素,最后一个列表元素就会丢失......所以最好的解决方案是将奇数列表放在第一位,然后将偶数列表放在第二位,直到每隔一个列表被反转。

I can't use any libraries.我不能使用任何库。 I need to do it recursively or split them and append them again.我需要递归执行或拆分它们并再次附加它们。 The best thing I made so far was to reverse only the first even list and append the first odd and even list in a new list.到目前为止,我所做的最好的事情是仅反转第一个偶数列表并将第一个奇数和偶数列表附加到新列表中。

I tried to do this:我试图这样做:

reverselist(List, [List]).
reverselist([X,Y|Rest], [SnakeList|Rest2]):-
    append(X, [], Odd),
    reverse(Y, EvenList),
    append(Odd, EvenList, SnakeList),
    reverselist(Rest, Rest2).

And this:和这个:

 reverselist(List1, List2).
 reverselist([H|Ts], [Odd|R]):-
    not(0 is H mod 2),
    append(H, [], Odd),
    reverselist(Ts, R).
 reverselist([H|Ts], [Even|R]):-
    0 is H mod 2,
    reverse(H, Even),
    reverselist(Ts, R).

Sample query:示例查询:

?- reverselist([[a,b,c],[d,a,b],[c,d,o],[b,c,d],[e,e,d]], List).

I want the result to be:我希望结果是:

List = [ [a,b,c],[b,a,d],[c,d,o],[d,c,b],[e,e,d] ].

You can also write mutual recursion:您还可以编写相互递归:

reverselist([],[]).
reverselist([H|T],[H|T1]):-reverselist2(T,T1).

reverselist2([],[]).
reverselist2([H|T],[H1|T1]):-reverse(H,H1), reverselist(T,T1).

We need to create another predicate with one more argument to keep track of odd or even position:我们需要创建另一个带有更多参数的谓词来跟踪奇数或偶数位置:

reverselist(InList,OutList):- reverselist(InList,OutList, 0).

reverselist([],[],_). %base case
%case of even position
reverselist([H|T],[H|T1], 0):- reverselist(T,T1,1).
%case of odd position
reverselist([H|T],[H1|T1], 1):- reverse(H1,H), reverselist(T,T1,0).

You were pretty close with your first variant.您与第一个变体非常接近。

Instead of your而不是你的

reverselist(List, [List]).
reverselist([X,Y|Rest], [SnakeList|Rest2]):-
  append(X, [], Odd),
  reverse(Y, EvenList),
  append(Odd, EvenList, SnakeList),
  reverselist(Rest, Rest2).

just tweak it as只需将其调整为

reverselist([],   []).               % additional clause
reverselist([List], [List]).
reverselist([X,Y|Rest], [X,EvenList|Rest2]):-
  reverse(     Y,          EvenList),
  reverselist(   Rest,              Rest2).

All three clauses are mutually exclusive and together they are exhaustive, ie they cover every possibility.所有这三个条款都是相互排斥的,它们一起是详尽无遗的,即它们涵盖了所有可能性。

I believe this definition to be the most immediate and close representation of your problem.我相信这个定义是您问题的最直接和最接近的表示。 In Prolog, to formulate the problem means to have the solution for it.在 Prolog 中,制定问题意味着有解决方案。

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

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