Mat*_*att 13 prolog backtracking operator-keyword meta-predicate
所以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).
Run Code Online (Sandbox Code Playgroud)
这是做什么的?这样可以查看另一个谓词是否为真.我不明白".."的作用.
如果没有univ运算符,你会如何重写它?
Fre*_*Foo 15
Univ(=..)将一个术语分解为一个成分列表,或者从这样的列表中构造一个术语.尝试:
?- 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).
Run Code Online (Sandbox Code Playgroud)
bar似乎把每个谓词都称为PredListon Item,并foo回溯Items.(使用变量作为谓词不可移植; call应首选谓词.)
编辑:Kaarel是正确的,大学可以通过更换functor/3和arg/3,如下所示:
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
Run Code Online (Sandbox Code Playgroud)