将列表1中的特定数字与列表2中的特定数字交换 [英] Swapping a specific number in list 1 with a specific number in list 2

查看:72
本文介绍了将列表1中的特定数字与列表2中的特定数字交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在复习一些Prolog.我很乐意提出一些随机问题,尝试解决并解决它们.不过,这是一件非常困难的事情,我不是要放弃我打算解决的问题的人.

I have been brushing up on some Prolog recently. I kind of enjoy just coming up with random problems to try and solve and then working them out. This one is quite tough though, and I'm not one to give up on a problem that I have set out to solve.

问题:我想创建一个谓词,该谓词将具有2个预定列表,2个要交换的数字,然后在交换完成后输出列表.

The problem: I want to make a predicate that will have 2 predetermined lists, 2 numbers to swap, and then output the lists after the swapping is done.

进一步的解释:我想通过清单1查找特定的唯一编号,并将其与清单2的特定唯一编号交换,从而使自己的工作更加困难,如果我有2清单... [7,2,7,8,5]和[1,2,3,8,7,9,8],然后给谓词2个数字(让我们说8和7),再给数字8和数字7将在列表中交换 IF和仅IF ,数字8在第一个列表中,数字7在第二个列表中. (它将忽略第二个列表中的8和第一个列表中的7).

Further Explanation: I made it a little harder on myself by wanting to find a specific unique number from list 1, and swapping this with a specific unique number from list 2 so that if I have 2 lists... [7,2,7,8,5], and [1,2,3,8,7,9,8], and then give the predicate 2 numbers(Lets just say 8 and 7), then the number 8 and the number 7 will be swapped between the lists IF AND ONLY IF the number 8 is in the first list and the number 7 is in the second list. (It would disregard an 8 in the second list and a 7 in the first list).

带有预期答案的示例查询:

Sample query with expected answer:

?- bothSwap([7,2,7,8,5],[1,2,3,8,7,9,8],8,7,X,Y).
X = [7,2,7,7,5], Y = [1,2,3,8,8,9,8].

我有点卡住了:

bothSwap([],L2,N1,N2,[],L2).
bothSwap(L1,[],N1,N2,L1,[]).
bothSwap([H1|T1],[H2|T2],N1,N2,X,Y) :- H1 == N1, H2 == N2, bothSwap(T1,T2,N1,N2,D1,D2), append(D1,[H2],X), append(D2,[H1],Y).
bothSwap([H1|T1],[H2|T2],N1,N2,X,Y) :- H1 == N1, H2 =\= N2, bothSwap([H1|T1],T2,N1,N2,D1,D2).
bothSwap([H1|T1],[H2|T2],N1,N2,X,Y) :- H1 =\= N1, H2 == N2, bothSwap(T1,[H2|T2],N1,N2,D1,D2).

有什么聪明的人愿意和我一起解决这个问题吗? :)

Any bright minds out there willing to tackle this problem with me? :)

推荐答案

让我们开始吧,交换意味着什么.

Let's start, what you mean by swapping.

swap(X0,X, S0,S) :-
   if_(X0 = S0, S = X, S = S0).

bothSwap0(Xs0, Ys0, X0,X, Xs,Ys) :-
   maplist(swap(X0,X), Xs0,Xs),
   maplist(swap(X,X0), Ys0,Ys).

if_( C_1, Then_0, Else_0) :-
   call(C_1, Truth),
   functor(Truth,_,0),  % safety check
   ( Truth == true -> Then_0 ; Truth == false, Else_0 ).

=(X, Y, R) :- X == Y, !, R = true.
=(X, Y, R) :- ?=(X, Y), !, R = false. % syntactically different
=(X, Y, R) :- X \= Y, !, R = false. % semantically different
=(X, Y, R) :- R == true, !, X = Y.
=(X, X, true).
=(X, Y, false) :-
   dif(X, Y).

现在您想要一个特定的条件-目前尚不清楚如何应用它.我看到两种解释:

Now you wanted a particular condition - it is not clear how to apply it. I see two interpretations:

bothSwap(Xs0, Ys0, X0,X, Xs,Ys) :-
   memberd(X0, Xs0),
   memberd(X, Ys0),
   maplist(swap(X0,X), Xs0,Xs),
   maplist(swap(X,X0), Ys0,Ys).

这表示bothSwap/6将在两个元素不在其各自的列表中出现时失败.

Which means that bothSwap/6 will fail should the two elements not occur in their respective list.

另一种解释可能是您希望列表保持不变.要表达这一点(以纯粹的单调方式):

Another interpretation might be that you want that otherwise the lists remain the same. To express this (in a pure monotonic fashion):

bothSwap(Xs0, Ys0, X0,X, Xs,Ys) :-
   if_( ( memberd_t(X0, Xs0), memberd_t(X, Ys0) ),
        ( maplist(swap(X0,X), Xs0,Xs), maplist(swap(X,X0), Ys0,Ys) ),
        ( Xs0 = Xs, Ys0 = Ys) ).

memberd_t(E, Xs, T) :-
   list_memberd(Xs, E, T).

list_memberd([], _, false).
list_memberd([X|Xs], E, T) :-
   if_(E = X, T = true, list_memberd(Xs, E, T) ).

','( A_1, B_1, T) :-
   if_( A_1, call(B_1, T), T = false ).

这篇关于将列表1中的特定数字与列表2中的特定数字交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆