Erlang 的 Prolog 术语

发布于 2024-09-08 12:11:33 字数 70 浏览 3 评论 0原文

假设您有序言术语“城市(伦敦,英格兰)”。即伦敦是英格兰的一个城市。

你如何在 Erlang 中表示模式匹配?

Let's say you have this prolog term "city(London, England)". ie London is a city in England.

How do you represent that in Erlang, pattern matching?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪失控 2024-09-15 12:11:33

Erlang 和 Prolog 之间没有可以完成的简单映射。除了语法和一些运算符之外,它们是完全不同的语言。你将无法从 Prolog 做太多事情,而且没有什么好方法可以完成你所要求的事情;他们都很糟糕。。尽管如此,以下是使用这些糟糕的方法可以做的事情。


在 Prolog 中,city(London, England) 创建了城市和国家之间的关系,而在 Erlang 中没有这样的声明性等价物。为了获得某种等价的东西,您需要在内存中手动存储关系(列表、ETS 表、树或字典等)。

如果您使用类似于 {Rel, [Items]} 的表示形式,则可以将当前示例设置为 {city, [london, england]}。如果你将它们全部存储在一个列表中,模式匹配可能只是

relation(X, [X|Rest]) -> true;
relation(X, [_|Rest]) -> relation(X, Rest);
relation(X, []) -> false.

或者更像是

main() ->
    Relations = [{london, england},
                 {montreal, canada},
                 {oxford, england}],
    same_country(Relations, london, oxford).

same_country(Relations, City1, City2) ->
    {_, Country1} = lists:keyfind(City1, 1, Relations),
    {_, Country2} = lists:keyfind(City2, 1, Relations),
    COuntry1 == Country2.

当然,这是低效的,你可能会使用 Erlang 中当前存在的数据结构之一。对于大多数用于键值存储的不透明数据结构(gb_trees、dict 等),您需要使用提供给您的函数来进行操作,因此不可能进行模式匹配。

下一个选择可能是使用 ETS,这是 Erlang 的内存数据库。它使用不同的匹配方法,名为 匹配规范 (或转换后的来自使用 ets:fun2ms/1 进行模式匹配的函数.)

以上所有内容并不是真正有用,因为它不会让您对关系进行任何真正有效的操作。为了获得最多的功能,您可能必须使用集合论,自己对数据进行建模并在 查询列表推导式sofs(集合的集合)模块。


我再次重申,确实没有什么好的方法可以将任何 Prolog 翻译成 Erlang。 Erlang 的早期版本是用 Prolog 编写的,但语义并不相同。如果你有兴趣,你可以看一下 Erlog​​,这是一个由 Erlang 编写的 Prolog罗伯特·维尔丁.

There is no simple mapping that can be done between Erlang and Prolog. Except for the syntax and some operators, they're completely distinct languages. You won't be able to do much from Prolog and there is no good way to do what you ask; they all suck. Nevertheless, here's what could be possible to do with the sucky methods.


Where city(London, England) creates a relationship between the city and the country in Prolog, there is no such declarative equivalent in Erlang. To get something somewhat equivalent, you would need to manually store relationships in memory (lists, ETS table, trees or dictionaries, etc).

If you use a representation a bit like {Rel, [Items]}, you could have your current example as {city, [london, england]}. If you store them all in a list, pattern matching could simply be

relation(X, [X|Rest]) -> true;
relation(X, [_|Rest]) -> relation(X, Rest);
relation(X, []) -> false.

or maybe something more like

main() ->
    Relations = [{london, england},
                 {montreal, canada},
                 {oxford, england}],
    same_country(Relations, london, oxford).

same_country(Relations, City1, City2) ->
    {_, Country1} = lists:keyfind(City1, 1, Relations),
    {_, Country2} = lists:keyfind(City2, 1, Relations),
    COuntry1 == Country2.

Of course this is inefficient and you're likely to use one of the data structures that currently exist in Erlang. In the case of most opaque data structures for key-value storage (gb_trees, dict, etc.), you need to use the functions given to you to play around and thus no pattern matching is possible.

The next choice could be to use ETS, an in-memory database for Erlang. It uses a different method for matching, named match specs (or converted from functions that use pattern matching with ets:fun2ms/1.)

All of the above isn't really useful because it won't let you do any really efficient operations on the relationship. To get the most functionality you'd probably have to use set theory, model the data yourself and operate on it with the help of Query List Comprehensions or the sofs (Sets Of Sets) module.


Again, I'll repeat there's really no good way to translate any Prolog to Erlang. Early versions of Erlang were written in Prolog, but the semantics just aren't the same. If you're interested, you could take a look at Erlog, a Prolog in and for Erlang written by Robert Virding.

萝莉病 2024-09-15 12:11:33
city("London", "England") -> true;
city(_, _) -> false.

或者

city("London") -> "England";
city(_) -> "".
city("London", "England") -> true;
city(_, _) -> false.

OR

city("London") -> "England";
city(_) -> "".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文