序言对列表
我有一个对列表的输入:
[[abs_(p,X,Y,Z),abs_(f,X,Y,Z)],[abs_(p,X,Y,Z),abs_(l,Z,P)]]
我想检查一对是否具有相同数量的参数,在这种情况下是:
[abs_(p,X,Y,Z),abs_(f,X,Y,Z)]
在第二种情况下答案是否定的。
这只是一个示例,因为更一般地说,我想知道哪对具有相同数量的参数。输入的输出应该是:
[[abs_(p,X,Y,Z),abs_(f,X,Y,Z)]
我必须做什么?
I have an input of a list of pairs:
[[abs_(p,X,Y,Z),abs_(f,X,Y,Z)],[abs_(p,X,Y,Z),abs_(l,Z,P)]]
I want check if a pair have the same number of arguments, in this case yes:
[abs_(p,X,Y,Z),abs_(f,X,Y,Z)]
In the second case the answer is no.
This is just an example because more generally, I want to know which pair have the same number of arguments. The output for the input should be:
[[abs_(p,X,Y,Z),abs_(f,X,Y,Z)]
What do I have to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用模式匹配将每对分解为术语,并使用函子谓词检查这些术语的数量。
You can also use pattern matching to break each pair into terms, and use the functor predicate to check the arity of those terms.
第一条规则是基本情况。第二条规则检查第一对中的参数数量(如果相同)运行递归调用并将递归调用的输出附加到输出。因为如果 N2 等于 N1,则不会调用第三条规则。第三条规则丢弃不匹配的对并用列表的尾部调用自身。希望有帮助。
First rule is base case. Second rule checks the number of arguments in first pair if its same run the recursive call and append the output from recursive call with and this pair to Output. Because of cut if N2 equals N1 it doesn't call third rule. And third rule discard the unmatched pair and call itself with tail of the list. Hope it helps.
Prolog 提供了一个奇怪的中缀运算符 =.. ,称为“univ”,它在复合术语和以函子开头的列表之间进行转换,后跟该函子的参数。
因此,Prolog 查询如下所示:
因此,我将在“univ”运算符生成的列表上使用 length/2 来检查两个复合术语是否具有相同数量的参数。
Prolog provides a strange infix operator =.. called "univ" which converts between compound terms and a list that begins with the functor followed by the arguments to that functor.
Hence a Prolog query something like this:
So I would use length/2 on the lists produced by the "univ" operator to check that two compound terms have an equal number of arguments.