Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 基于另一个列表中给定的索引交换元素_List_Prolog_Lambda_Meta Predicate - Fatal编程技术网

List 基于另一个列表中给定的索引交换元素

List 基于另一个列表中给定的索引交换元素,list,prolog,lambda,meta-predicate,List,Prolog,Lambda,Meta Predicate,你好,我的任务是交换列表中的元素,以及需要在另一个列表中交换的索引,因此fx: 如果我有: [3,1,2] as the list 以及: 那么它应该是这样的: [1,2] = 3 and 1 getting swapped [2,3] = 3 and 2 getting swapped 所以我最终的结果是输出=[1,2,3] 谓词指定如下: swap(C,Input,Output) swap( Input,[I|J], Input ) :- I = J. swap( Input,

你好,我的任务是交换列表中的元素,以及需要在另一个列表中交换的索引,因此fx:

如果我有:

[3,1,2] as the list
以及:

那么它应该是这样的:

[1,2] = 3 and 1 getting swapped
[2,3] = 3 and 2 getting swapped
所以我最终的结果是输出=[1,2,3]

谓词指定如下:

swap(C,Input,Output)
swap( Input,[I|J], Input ) :-
    I = J.
swap( Input, [I|J], Output ) :-
    swap( Input, [I|J], Output, _, _ ).
swap( Input, [I|J], Output ) :-
    swap( Input, J, I, Output, _, _ ).

swap( [E2|Ls], I,  0,  [E1|Ls], E1, E2 ):-!.
swap( [E1|Es], 0, J, [E2|Rs], E1, E2 ) :-
    N2 is J - 1,
    swap( Es, -1, N2, Rs, E1, E2 ),!.
swap( [E|Es], [I|J], [E|Rs], E1, E2 ) :-
    N1 is I - 1,
    N2 is J - 1,
    swap( Es, N1, N2, Rs, E1, E2 ). 
其中C是应该交换的元素的列表

输入是应该交换的列表

输出是交换的列表

我想要一些关于如何交换这些元素的建议,基于此,我已经研究了以下内容:

希望有人能帮我

编辑:

到目前为止,我已经尝试过这样的方法:

swap(C,Input,Output)
swap( Input,[I|J], Input ) :-
    I = J.
swap( Input, [I|J], Output ) :-
    swap( Input, [I|J], Output, _, _ ).
swap( Input, [I|J], Output ) :-
    swap( Input, J, I, Output, _, _ ).

swap( [E2|Ls], I,  0,  [E1|Ls], E1, E2 ):-!.
swap( [E1|Es], 0, J, [E2|Rs], E1, E2 ) :-
    N2 is J - 1,
    swap( Es, -1, N2, Rs, E1, E2 ),!.
swap( [E|Es], [I|J], [E|Rs], E1, E2 ) :-
    N1 is I - 1,
    N2 is J - 1,
    swap( Es, N1, N2, Rs, E1, E2 ). 

但我只能使用一个列表作为必须交换的内容的索引,如[1,2],我要寻找的是能够使用多个列表,如[[1,2],[2,3]等等。

使用列表交换/4

list_i_j_swapped(As,I,J,Cs) :-
 same_length(As,Cs),
 append(BeforeI,[AtI|PastI],As),
 append(BeforeI,[AtJ|PastI],Bs),
 append(BeforeJ,[AtJ|PastJ],Bs),
 append(BeforeJ,[AtI|PastJ],Cs),
 length(BeforeI,I),
 length(BeforeJ,J).

swap(List,[],List).
swap(List1,Swaps,ListSwapped):-
  Swaps =[[Index1,Index2]|T],
  list_i_j_swapped(List1,Index1,Index2,List2),
  swap(List2,T,ListSwapped).
问:


位置为零索引。

使用列表i\u j\u交换/4

list_i_j_swapped(As,I,J,Cs) :-
 same_length(As,Cs),
 append(BeforeI,[AtI|PastI],As),
 append(BeforeI,[AtJ|PastI],Bs),
 append(BeforeJ,[AtJ|PastJ],Bs),
 append(BeforeJ,[AtI|PastJ],Cs),
 length(BeforeI,I),
 length(BeforeJ,J).

swap(List,[],List).
swap(List1,Swaps,ListSwapped):-
  Swaps =[[Index1,Index2]|T],
  list_i_j_swapped(List1,Index1,Index2,List2),
  swap(List2,T,ListSwapped).
问:

位置为零索引。

此答案基于

这是+变体

这个答案是基于

这是+变体


请显示您迄今为止尝试过的内容。使用我现在尝试过的内容进行后期编辑。请显示您迄今为止尝试过的内容。使用我现在尝试过的内容进行后期编辑。sX:如果您选择,将基于foldl/4的变体作为单独的答案如何?我不确定foldl如何处理此问题?我只理解为一个列表的总和。我很想看看它对swap/3的作用。明白了吗?当然另外请注意,foldl/4也可以与s一起使用,如foldl//2.sX:如果您愿意,可以使用一个基于foldl/4的变体作为单独的答案吗?我不确定foldl如何处理这个问题?我只理解为一个列表的总和。我很想看看它对swap/3的作用。明白了吗?当然还要注意的是,foldl/4也可以与s一起用作foldl//2。
?- swap2([3,1,2], [[0,1],[1,2]], Xs).
  X = [1,2,3]
; false.