Prolog 中列表的自定义反转

发布于 2024-09-25 18:30:31 字数 651 浏览 6 评论 0原文

我正在尝试编写一个谓词,在 Prolog 中给出以下列表:

[[1,a,b],[2,c,d],[[3,e,f],[4,g,h],[5,i,j]],[6,k,l]]

将生成以下列表:

[[6,k,l],[[5,i,j],[4,g,h],[3,e,f]],[2,c,d],[1,a,b]]

正如您所看到的,我想保留最低级别的元素顺序,以生成顺序为 1, a, b 而不是 b、a、1。

我还想保留列表的深度,即最初嵌套的列表按原样返回,但以相反的顺序返回。

我已经设法使用以下代码实现了所需的顺序,但深度丢失了,即列表不再正确嵌套:

accRev([F,S,T],A,R) :- F \= [_|_], S \= [_|_], T \= [_|_], 
                                 accRev([],[[F,S,T]|A],R).
accRev([H|T],A,R) :- accRev(H,[],R1), accRev(T,[],R2), append(R2,R1,R).
accRev([],A,A).
rev(A,B) :- accRev(A,[],B).

我将不胜感激帮助纠正代码以保留列表的正确嵌套。谢谢!

I am trying to write a predicate that given the following list in Prolog:

[[1,a,b],[2,c,d],[[3,e,f],[4,g,h],[5,i,j]],[6,k,l]]

will produce the following list:

[[6,k,l],[[5,i,j],[4,g,h],[3,e,f]],[2,c,d],[1,a,b]]

As you can see I would like to preserve the order of the elements at the lowest level, to produce elements in the order 1, a, b and NOT b, a, 1.

I would also like to preserve the depth of the lists, that is, lists that are originally nested are returned as such, but in reverse order.

I have managed to achieve the desired order with the following code, but the depth is lost, i.e. lists are no longer nested correctly:

accRev([F,S,T],A,R) :- F \= [_|_], S \= [_|_], T \= [_|_], 
                                 accRev([],[[F,S,T]|A],R).
accRev([H|T],A,R) :- accRev(H,[],R1), accRev(T,[],R2), append(R2,R1,R).
accRev([],A,A).
rev(A,B) :- accRev(A,[],B).

I would appreciate help in correcting the code to preserve the correct nesting of lists. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-10-02 18:30:31

两个建议。首先,这是一个 (rev_lists/2),它使用了一堆 SWI-PROLOG 内置函数:

rev_lists(L, RL) :-
    forall(member(M, L), is_list(M)), !,
    maplist(rev_lists, L, L0),
    reverse(L0, RL).
rev_lists(L, L).

这个通过测试列表 L 的所有元素本身是否是列表来工作(M);如果是这样,它将递归地将自身(通过maplist)应用于所有单独的子列表,否则它将返回相同的列表。这样就达到了需要的效果了。

其次,这里又是 rev_lists/2,但编写方式使其不依赖于除 member/2(这是常见的)之外的内置函数:

rev_lists(L, RL) :-
    reversible_list(L), !,
    rev_lists(L, [], RL). 
rev_lists(L, L).

rev_lists([], Acc, Acc).
rev_lists([L|Ls], Acc, R) :-
    (   rev_lists(L, RL), !
    ;   RL = L
    ),
    rev_lists(Ls, [RL|Acc], R).

reversible_list(L) :-
    is_a_list(L),
    \+ (
        member(M, L),
        \+ is_a_list(M)
    ).

is_a_list([]).
is_a_list([_|_]).

它基本上是相同的策略,但使用累加器在每个级别构建反向列表,前提是它们仅由列表组成;否则,返回相同的列表。

Two suggestions. First, here's one (rev_lists/2) which uses a bunch of SWI-PROLOG built-ins:

rev_lists(L, RL) :-
    forall(member(M, L), is_list(M)), !,
    maplist(rev_lists, L, L0),
    reverse(L0, RL).
rev_lists(L, L).

This one works by testing if all elements of a list L are themselves lists (M); if so, it will recursively apply itself (via maplist) over all individual sub-lists, else it will return the same list. This has the required effect.

Secondly, here's rev_lists/2 again, but written such that it doesn't rely on built-ins except member/2 (which is common):

rev_lists(L, RL) :-
    reversible_list(L), !,
    rev_lists(L, [], RL). 
rev_lists(L, L).

rev_lists([], Acc, Acc).
rev_lists([L|Ls], Acc, R) :-
    (   rev_lists(L, RL), !
    ;   RL = L
    ),
    rev_lists(Ls, [RL|Acc], R).

reversible_list(L) :-
    is_a_list(L),
    \+ (
        member(M, L),
        \+ is_a_list(M)
    ).

is_a_list([]).
is_a_list([_|_]).

It's basically the same strategy, but uses an accumulator to build up reverse lists at each level, iff they are comprised exclusively of lists; otherwise, the same list is returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文