Prolog findall/3 [英] Prolog findall/3

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

问题描述

假设我有一个包含多个事实的谓词 pred.

Say I have a predicate pred containing several facts.

pred(a, b, c).
pred(a, d, f).
pred(x, y, z).

我可以使用 findall/3 来获取所有可以进行模式匹配的事实列表吗?

Can I use findall/3 to get a list of all facts which can be pattern matched?

例如,如果我有

pred(a, _, _)我想获得

[pred(a, b, c), pred(a, d, f)]

推荐答案

简单总结一下@mbratch在评论区说的话:

Just summing up what @mbratch said in the comment section:

是的,但您必须确保使用 named 变量或构造一个简单的辅助谓词来为您执行此操作:

Yes, but you have to make sure that you either use named variables or construct a simple helper predicate that does that for you:

命名变量:

findall(pred(a,X,Y),pred(a,X,Y),List).

辅助谓词:

special_findall(X,List):-findall(X,X,List).

?-special_findall(pred(a,_,_),List).
List = [pred(a, b, c), pred(a, d, f)].

请注意,这不起作用:

findall(pred(a,_,_),pred(a,_,_),List).

因为它相当于

 findall(pred(a,A,B),pred(a,C,D),List).

因此没有将Template的变量与Goal的变量统一起来.

And thus doesn't unify the Variables of Template with those of Goal.

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

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