Prolog 创建字典

Prolog Create Dictionaries

我知道 Prolog 不是一种面向对象的语言,在阅读了一些 Whosebug 帖子后,我不清楚这是否可行,但我想我还是要问一下:

如果一个Customer有并且只有一个name(原子串)和一个age(整数),是否可以要求Prolog给出一些[=的例子12=] 字典,给定可能的 name 列表和 age 范围?实际使用将对字典值进行广泛的限制。

例如,理想情况下我想要这样的东西

between(18, 60, Customer.age),
member(Customer.name, [jodie, tengyu, adiche, tomoyo, wolfgang]),
Customer = whatisthis{age: What, name: Wot}.

给我类似的东西

Customer = whatisthis{age: 24, name: tomoyo} ;
Customer = whatisthis{age: 55, name: tengyu} ;
...
...

在 SWI-Prolog 中,您确实有听写。这里:

?- between(2,3,X), Age is 20*X, member(Name, [tomoyo, tengyu]), Dict = customer{name:Name,
age:Age}.
X = 2,
Age = 40,
Name = tomoyo,
Dict = customer{age:40, name:tomoyo} ;
X = 2,
Age = 40,
Name = tengyu,
Dict = customer{age:40, name:tengyu} ;
X = 3,
Age = 60,
Name = tomoyo,
Dict = customer{age:60, name:tomoyo} ;
X = 3,
Age = 60,
Name = tengyu,
Dict = customer{age:60, name:tengyu}.

您可以在 run-time 的字典中添加和删除 key-value 对。唯一的限制是键必须是原子项。

文档在这里:

http://www.swi-prolog.org/pldoc/man?section=bidicts