Prolog中的zip功能 [英] zip function in Prolog

查看:75
本文介绍了Prolog中的zip功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Prolog的新手,我的工作要求我们实现以下功能:

I'm new to Prolog and my assignment requires us to implement a function as described below:

写一个Prolog谓词zip(L1,L2,L3),如果列表L3是通过对列表L1L2的元素进行压缩(即混洗"或交织")获得的,则为true.更新:列表L1L2可以具有不同的长度.例如,完成后,您应该得到以下行为:

Write a Prolog predicate zip(L1,L2,L3) that is true if the list L3 is obtained by zipping (i.e. shuffling", or interleaving") the elements of the lists L1 and L2. Update: the lists L1 and L2 can have different lengths. For instance, when you are done, you should get the following behavior:

?- zip([1,2],[a,b],[1,a,2,b]).
true.
?- zip([1,2],[a,b],X).
X = [1, 2, a, b] ;
X = [1, 2, a, b] ;
X = [1, a, 2, b] ;
X = [1, a, b, 2] ;
X = [a, 1, 2, b] ;
X = [a, 1, b, 2] ;
X = [a, b, 1, 2] ;
X = [a, b, 1, 2] ;
false.
?- zip([1,2],[a,b],[1,2,a,b]).
true.
?- zip(X,[a,b],[1,a,2,b]).
X = [1,2]
true.
?- zip([1,2],X,[1,a,2,b]).
X = [a,b]
true.

我正在考虑创建一个包含来自L1L2的元素的列表,然后将该列表与L3进行比较.但是我不熟悉Prolog中的语法和循环.

I'm thinking of creating a list which contains elements from L1 and L2 and then compare the list with L3. But I'm not familiar with the syntax and loops in Prolog.

推荐答案

实际上,我对您有三个答案.您可能想要第三个.但是无论如何都要遍历其他人.

Actually, I have three answers to you. You probably want the third. But go through the others anyway.

我不确定您是否想要描述的关系.你是 也可以同时学习OCaml和Python,并且使用这些语言zip意味着 其他的东西.意思是这样的:

I am not that sure that you want the relation you describe. You are also learning OCaml and Python at the same time and in those languages zip means something else. It means something like:

zip([], [], []).
zip([], [_|_], []).
zip([_|_], [], []).
zip([X|Xs], [Y|Ys], [X-Y|XYs]) :-
   zip(Xs, Ys, XYs).

?- zip([1,2],[3,4],XYs).
XYs = [1-3,2-4].

请注意Prolog中的不同约定.虽然OCaml,Python以及Haskell也使用(X,Y)表示元组,但Prolog中经常使用的约定是(X-Y).此处的减号并不表示减法.这只是一个无法解释的术语.

Note the different convention in Prolog. While OCaml, Python but also Haskell use (X,Y) to denote a tuple, the frequent convention in Prolog is to use (X-Y). The minus sign here does not mean subtraction. It is just an uninterpreted term.

在Prolog中实现此功能的常用方法是使用maplist . maplist要求所有列表的长度都相同.

The common way to implement this in Prolog is to use maplist. maplist requires that all lists are of same length.

(编辑)以下是另一种解释. interlace/3shuffle/3之类的名称 在这里会很理想.最近,@ salva向我们展示了一个非常漂亮的解决方案 这.别忘了为其+1!让我只展示一些很酷的方式 可以使用它:

(Edit) Another interpretation is the following. A name like interlace/3 or shuffle/3 would be ideal here. Recently @salva has showed us a very beautiful solution to this. Don't forget to +1 it! Let me only show some cool ways how you can use it:

?- shuffle([1,2],[3,4],Zs).
Zs = [1,3,2,4].

您已经知道了.但是为什么我们需要同时给出两个列表[1,2][3,4]到Prolog?这不是一种简单的编程语言, 强迫您说出一切.如果您懒得键​​入复杂的列表或其他术语,只需输入一个变量,然后看Prolog如何弄清楚它.因此,我们将第二个列表替换为 变量.

This you already know. But why do we need to give both lists [1,2] and [3,4] to Prolog? This isn't a simple programming language which forces you to tell everything. If you are too lazy to type in a complex list or another term, just put a variable and see how Prolog figures it out. So, let's replace the second list by a variable.

?- shuffle([1,2],Ys,Zs).
Ys = [],
Zs = [1,2] ;
Ys = [_G607],
Zs = [1,_G607,2] ;
Ys = [_G607,_G616|_G617],
Zs = [1,_G607,2,_G616|_G617].

以这种方式我们问:YsZs看起来必须像shuffle/3那样真实吗?实际上,Ys有3个答案:

In this manner we ask: How do Ys and Zs have to look like such that shuffle/3 is true? In fact, there are 3 answers for Ys:

  • []是空列表.那么Zs[1,2].因此,这是一种解决方案.

  • [] being the empty list. Zs is then [1,2]. So this is one solution.

[_G607]是仅包含一个元素的列表. Zs[1,_G607,2]. _G607是一个自由变量.它可能有一个更好的名称,但要点是,此变量在YsZs两者都出现.这个答案说:符合该变量的所有 术语都是解决方案.因此,我们在一个答案中可以表达无限多个解决方案.

[_G607] being a list with exactly one element. Zs is the [1,_G607,2]. The _G607 is a free variable. It could have a nicer name, but the point is that this variable occurs both within Ys and within Zs. This answer says: All terms that fit into that variable are solutions. So we have here infinitely many solutions expressed within a single answer.

[_G607,_G616|_G617]表示包含至少两个元素的列表.

[_G607,_G616|_G617] meaning a list with at least two elements.

这是一个更酷的查询:

?- shuffle(Xs,Xs,Zs).
Xs = Zs, Zs = [] ;
Xs = [_G592],
Zs = [_G592,_G592] ;
Xs = [_G592,_G601],
Zs = [_G592,_G592,_G601,_G601] ;
Xs = [_G592,_G601,_G610],
Zs = [_G592,_G592,_G601,_G601,_G610,_G610]
...

看看现在Zs中相同变量的重复项是什么!

Look how there are now duplicates of the same variable in Zs!

也许这就是您真正想要的:

Maybe this is what you actually want:

intertwine([], [], []).
intertwine([E|Es], Fs, [E|Gs]) :-
   intertwine(Es, Fs, Gs).
intertwine(Es, [F|Fs], [F|Gs]) :-
   intertwine(Es, Fs, Gs).

在下面的查询中,我们询问可以交织在一起的列表,从而给出三个元素的列表!

In the following query we ask about the lists that can be intertwined to give a three element list as result!

?- length(Zs,3), intertwine(Xs,Ys,Zs).
Zs = Xs, Xs = [_G648,_G651,_G654],
Ys = [] ;
Zs = [_G648,_G651,_G654],
Xs = [_G648,_G651],
Ys = [_G654] ;
Zs = [_G648,_G651,_G654],
Xs = [_G648,_G654],
Ys = [_G651] ;
Zs = [_G648,_G651,_G654],
Xs = [_G648],
Ys = [_G651,_G654] ;
Zs = [_G648,_G651,_G654],
Xs = [_G651,_G654],
Ys = [_G648] ;
Zs = [_G648,_G651,_G654],
Xs = [_G651],
Ys = [_G648,_G654] ;
Zs = [_G648,_G651,_G654],
Xs = [_G654],
Ys = [_G648,_G651] ;
Zs = [_G648,_G651,_G654],
Xs = [],
Ys = [_G648,_G651,_G654].

这篇关于Prolog中的zip功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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