序言对列表

发布于 2024-11-25 16:02:26 字数 341 浏览 0 评论 0原文

我有一个对列表的输入:

[[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 技术交流群。

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

发布评论

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

评论(3

梦境 2024-12-02 16:02:27

您还可以使用模式匹配将每对分解为术语,并使用函子谓词检查这些术语的数量。

You can also use pattern matching to break each pair into terms, and use the functor predicate to check the arity of those terms.

就像说晚安 2024-12-02 16:02:26
run( [], Tail) :- Tail=[].
run( [ [First,Second] | Tail ], Output ) :- First =.. List1, Second =.. List2, 
length(List1,N1), length(List2, N2), N2 is N1, !, run(Tail, Tail2), 
append( [ [First | [Second]] ], Tail2, Output ). 
run( [H|T], Output ) :- run(T, Output).

第一条规则是基本情况。第二条规则检查第一对中的参数数量(如果相同)运行递归调用并将递归调用的输出附加到输出。因为如果 N2 等于 N1,则不会调用第三条规则。第三条规则丢弃不匹配的对并用列表的尾部调用自身。希望有帮助。

run( [], Tail) :- Tail=[].
run( [ [First,Second] | Tail ], Output ) :- First =.. List1, Second =.. List2, 
length(List1,N1), length(List2, N2), N2 is N1, !, run(Tail, Tail2), 
append( [ [First | [Second]] ], Tail2, Output ). 
run( [H|T], Output ) :- run(T, Output).

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.

屌丝范 2024-12-02 16:02:26

Prolog 提供了一个奇怪的中缀运算符 =.. ,称为“univ”,它在复合术语和以函子开头的列表之间进行转换,后跟该函子的参数。

因此,Prolog 查询如下所示:

?- abs_(p,X,Y,Z) =.. L.
L = [abs_,p,X,Y,Z]
yes

因此,我将在“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:

?- abs_(p,X,Y,Z) =.. L.
L = [abs_,p,X,Y,Z]
yes

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.

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