本文介绍了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似乎在ItemPredList中调用每个谓词,而fooItem上回溯. (将变量用作谓词是不可移植的; 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是正确的,可以用functor/3arg/3替换univ,如下所示:

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运算符?解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 18:28