Prolog创建字典

时间:2019-05-21 15:21:36

标签: functional-programming prolog swi-prolog data-generation

我了解Prolog不是一种面向对象的语言,在阅读了几篇StackOverflow帖子后,我不清楚这是否可行,但我还是想问一遍:

如果Customer拥有且仅具有name(原子字符串)和age(整数),是否可以要求Prolog给出{{1}的一些示例}个字典,给出了可能的Customer列表和name范围?实际使用情况将对字典值产生广泛的限制。

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

age

给我类似的东西

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

1 个答案:

答案 0 :(得分:3)

在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}.

您可以在运行时从字典中添加和删除键值对。唯一的限制是键必须是原子项。

文档在这里:

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