Prolog中的谓词列表 [英] List of predicates in Prolog

查看:252
本文介绍了Prolog中的谓词列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义一个由谓词组成的列表,以及如何调用谓词.

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

示例:

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

推荐答案

您不能在列表中存储谓词,但可以存储术语(或函子)并将其称为目标.

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

用法:

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

此查询的答案当然取决于您对foo/1的定义.如果需要,请参阅我的 =.. 的说明.

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

编辑:如同注释中的@false报告一样,不必使用=..,因为可以将Goal =.. [P, X], call(Goal)替换为call(P, X),其效果相同.不过,也许您仍然需要学习=..,因为您可能会在其他人的代码中遇到它.

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.

这篇关于Prolog中的谓词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆