Erlang 的 Prolog 术语
假设您有序言术语“城市(伦敦,英格兰)”。即伦敦是英格兰的一个城市。
你如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Erlang 和 Prolog 之间没有可以完成的简单映射。除了语法和一些运算符之外,它们是完全不同的语言。你将无法从 Prolog 做太多事情,而且没有什么好方法可以完成你所要求的事情;他们都很糟糕。。尽管如此,以下是使用这些糟糕的方法可以做的事情。
在 Prolog 中,
city(London, England)
创建了城市和国家之间的关系,而在 Erlang 中没有这样的声明性等价物。为了获得某种等价的东西,您需要在内存中手动存储关系(列表、ETS 表、树或字典等)。如果您使用类似于
{Rel, [Items]}
的表示形式,则可以将当前示例设置为{city, [london, england]}
。如果你将它们全部存储在一个列表中,模式匹配可能只是或者更像是
当然,这是低效的,你可能会使用 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 beor maybe something more like
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.
或者
OR