Prolog GNU - Univ 运算符?它的解释 [英] Prolog GNU - Univ operator? Explanation of it

查看:26
本文介绍了Prolog GNU - Univ 运算符?它的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,univ 运算符.我不是很明白.

So the univ operator. I don't exactly understand it.

例如:

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.

如果没有 univ 运算符,你将如何重写它?

How would you rewrite this without the univ operator?

推荐答案

Univ (=..) 将一个术语分解为一个成分列表,或者从这样的列表构造一个术语.试试:

Univ (=..) breaks up a term into a list of constituents, or constructs a term from such a list. Try:

?- f(x,y) =.. L.
L = [f, x, y].

?- f(x,y,z) =.. [f|Args].
Args = [x, y, z].

?- Term =.. [g,x,y].
Term = g(x, y).

bar 似乎在 Item 上调用 PredList 中的每个谓词,而 foo 上回溯项目.(使用变量作为谓词是不可移植的;应该首选 call 谓词.)

bar seems to call each predicate in PredList on Item, with foo backtracking over the Items. (Using a variable as a predicate is not portable; the call predicate should be preferred.)

编辑:Kaarel说得对,univ可以替换成functor/3arg/3,如下:

Edit: Kaarel is right, univ can be replaced by functor/3 and arg/3, as follows:

bar([H|_],Item) :-
    functor(Goal,H,1),   % unifies Goal with H(_)
    arg(1,Goal,Item),    % unifies first argument of Goal with Item
    call(Goal).          % use this for portability

这篇关于Prolog GNU - Univ 运算符?它的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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