Prolog GNU - Univ operator? Explanation of it

前端 未结 2 506
逝去的感伤
逝去的感伤 2020-12-05 19:47

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         


        
2条回答
  •  一个人的身影
    2020-12-05 20:26

    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 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.)

    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
    

提交回复
热议问题