问题描述
因此,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
在Item
上回溯. (将变量用作谓词是不可移植的; call
谓词应该是首选.)
bar
seems to call each predicate in PredList
on Item
, with foo
backtracking over the Item
s. (Using a variable as a predicate is not portable; the call
predicate should be preferred.)
编辑:Kaarel是正确的,可以用functor/3
和arg/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运算符?解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!