List of predicates in Prolog

2020-02-14 07:57发布

Is it possible to define a list, that consists of predicates and how do I call the predicates.

Also, is it possible to pass one predicate to another predicate (like passing atoms)?

Example:

pre1:- something.
pre2(Predicate1, List):-
    call(Predicate1),
    append([Predicate1], List, R),
    .....

标签: list prolog
1条回答
The star\"
2楼-- · 2020-02-14 08:33

You can't store predicates in a list, but you can store terms (or functors) and call terms as goals.

Here's a predicate that tests whether a term has the properties described by a list of functors:

has_properties([], _).
has_properties([P|Ps], X) :-
    Goal =.. [P, X],            % construct goal P(X)
    call(Goal),
    has_properties(Ps, X).

Usage:

% is 4 a number, an integer and a foo?
?- has_properties([number, integer, foo], 4).

The answer to this query will depend on your definition of foo/1, of course. See my explanation of =.. if needed.

Edit: as @false reports in the comments, it's not necessary to use =.., since Goal =.. [P, X], call(Goal) can be replaced by call(P, X) will have the same effect. It might still be worthwhile learning about =.., though, as you may encounter it in other people's code.

查看更多
登录 后发表回答