Prolog GNU - Univ 运算符?其解释
所以univ操作员。我不太明白。
例如:
foo(PredList,[H|_]) :- bar(PredList,H).
foo(PredList,[_|T]) :- foo(PredList,T),!.
bar([H|_],Item) :- G =.. [H,Item],G.
bar([_|T],Item) :- bar(T,Item).
这是做什么的?这会检查另一个谓词是否为真。我不明白“..”的作用。
如果没有 univ 运算符,您将如何重写它?
So the univ operator. I don't exactly understand it.
For example this:
foo(PredList,[H|_]) :- bar(PredList,H).
foo(PredList,[_|T]) :- foo(PredList,T),!.
bar([H|_],Item) :- G =.. [H,Item],G.
bar([_|T],Item) :- bar(T,Item).
What is this doing? This looks to see if another predicate is true. I don't understand what the ".." does.
How would you rewrite this without the univ operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Univ (
=..
) 将术语分解为成分列表,或从这样的列表构造术语。尝试:bar
似乎在Item
上调用PredList
中的每个谓词,并使用foo
在Item 上回溯
。 (使用变量作为谓词是不可移植的;应首选call
谓词。)编辑:Kaarel 是对的,univ 可以替换为
functor/3
和arg/3
,如下:Univ (
=..
) breaks up a term into a list of constituents, or constructs a term from such a list. Try:bar
seems to call each predicate inPredList
onItem
, withfoo
backtracking over theItem
s. (Using a variable as a predicate is not portable; thecall
predicate should be preferred.)Edit: Kaarel is right, univ can be replaced by
functor/3
andarg/3
, as follows:我认为最合适的重写是:
call/n
尚未成为 ISO 核心标准的一部分,但它们可能会在不久的将来 (*)。许多 Prolog 系统已经支持它们。为什么
call/n
应该优于简单的(=..)/2
和functor/3
+arg 是有一个原因的/3
解决方案。call/n
解决方案能够处理闭包 (**)。通过简单的
(=..)/2
和functor/3
+arg/3
解决方案,我们可以调用bar/2< /code> 仅限第一个列表参数中的原子。例如:
有了闭包,我们就不再局限于原子,而且我们可能会节省一些编码工作。例如,我们可以直接执行以下操作:
最好的问候
(*)
技术勘误草案 2
http://www.complang.tuwien.ac.at/ulrich /iso-prolog/dtc2#call
(**)
谁发明了它?:
call/n
谓词http://www.complang.tuwien.ac.at/ulrich /Prolog-inedit/naish.html
The most appropriate rewrite in my opinion would be:
call/n
are not yet part of the ISO core standard, but they might become in the near future (*). A lot of Prolog systems already support them.There is one reason why
call/n
should be preferred over simple(=..)/2
andfunctor/3
+arg/3
solutions. Thecall/n
solution is capable to handle closures (**).With the simple
(=..)/2
andfunctor/3
+arg/3
solution one can invokebar/2
only with atoms in the first list argument. For example:With closures we are not restricted to atoms, and we might save some coding effort. For example we can do the following directly:
Best Regards
(*)
Draft Technical Corrigendum 2
http://www.complang.tuwien.ac.at/ulrich/iso-prolog/dtc2#call
(**)
Who Invented It?:
call/n
Predicateshttp://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/naish.html