简体   繁体   English

Prolog:从原子列表中返回事实列表

[英]Prolog: Return a list of facts from a list of atoms

I highly doubt it is possible, but I'm wondering is there is a way to transform a list of atoms into a list of fact.我非常怀疑这是可能的,但我想知道是否有一种方法可以将原子列表转换为事实列表。 It is assumed that there won't be repetition of atoms between two facts.假设两个事实之间不会有原子的重复。

More precisely, say I have the following list of facts:更准确地说,假设我有以下事实清单:

person(Mike, male, 33).
person(Frank, male, 24).
person(Julie, female, 25).

And I want to call我想打电话

listFacts( [Mike, Frank], L).

Which should return,哪个应该返回,

L = [person(Mike, male, 33), person(Frank, male, 24)].

You are currently using variables (the tokens starting with uppercase letters).您当前正在使用变量(以大写字母开头的标记)。 You must switch to constants (in this case, atoms): Mike -> mike您必须切换到常量(在本例中为原子): Mike -> mike

After that it's easy going, using setof/3 :之后很容易,使用setof/3

person(mike, male, 33).
person(frank, male, 24).
person(julie, female, 25).

listFacts( Names, List ) :-
   setof(person(Name,S,A), (member(Name,Names),person(Name,S,A)), List).  

Which means: Find answers for Name taken from Names for which a fact person(Name,S,A) exists and put a corresponding term person(Name,S,A) into a set (actually a list) called List .这意味着:从存在事实person(Name,S,A)Names中查找Name的答案,并将相应的术语person(Name,S,A)放入名为List的集合(实际上是一个列表)中。

And so:所以:

?- listFacts([mike,frank],F).
F = [person(frank, male, 24), person(mike, male, 33)].

The variation which is existentially qualified also works:存在限定的变体也有效:

listFacts( Names, List ) :-
   setof(person(Name,S,A), Name^(member(Name,Names),person(Name,S,A)), List). 

Properly, it should not as it is the same as:正确地,它不应该与以下内容相同:

listFacts( Names, List ) :-
   setof(person(_Name,S,A), subgoal(Names,S,A), List). 

subgoal(Names,S,A) :-
   member(Name,Names),
   person(Name,S,A).

and that gives us no info about the Name :这给我们没有关于Name的信息:

?- listFacts([mike,frank],F).
F = [person(_6478, male, 24), person(_6492, male, 33)].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM