简体   繁体   English

在列表序言列表中反向列表

[英]Reverse lists in list of lists prolog

I want to reverse each list from a list of lists. 我想从列表列表中反转每个列表。 I have something which looks like: 我有一些看起来像:

[[a,b],[],[c,d,e],[],[],[f,g]]

What I want is to read through the list of lists and reverse each list and get this result: 我想要的是通读列表列表并反转每个列表并得到以下结果:

[[b,a],[],[e,d,c],[],[],[g,f]]

I already have a reverse function: 我已经有一个反向功能:

reverse(L, R) :- reverse(L, [], R).
reverse([], R, R).
reverse([H|T], A, R) :- reverse(T, [H|A], R).

How can I manage to read through the list? 我如何设法通读清单?

Will a recursive function like this one work ? 这样的递归函数会起作用吗?

reverseLL([H|T], L) :-
   reverse(H,NH),
   reverseLL([T],[NH|L]).

reverseLL([],L).
reverseInList([],[]).
reverseInList([H|Ts],[H1|R]):-
    reverse(H,H1),
    reverseInList(Ts,R).

This works by recursing over the list and using the library function 'reverse/2' to reverse each sublist. 这可以通过遍历列表并使用库函数“ reverse / 2”来反转每个子列表来实现。

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

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