functional-programming - Prolog 创建字典

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

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

如果一个Customer有并且只有一个name(原子字符串)和一个age(整数),是否可以询问Prolog给出一些 Customer 字典的示例,给定可能的 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}.

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

文档在这里:

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

关于functional-programming - Prolog 创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241542/

相关文章:

Javascript 与 ES6 的嵌套 for 循环

python 3 : how do I run a function over a list of lists of possible parameters and returning a dictionary of all results

prolog - 如何在 prolog 中打印所有数据库事实

prolog - 使用 DCG 解析 Prolog 变量

prolog - 如何使用SWI-Prolog的语义Web库查询RDF/OWL?

algorithm - 使用函数式编程 (F#) 将位置列表转换为二维位置数组

c# - 使用 Func<T,TResult> 作为参数的方法

序言 clpfd : creating arithmetic constraints from data

c++ - DSO 中的 SIGSEGV,混合 C/C++

prolog - 库(lambda)、柯里化(Currying)和 SWI Prolog 的问题