Erlang 中可以定义循环列表吗?
erlang中可以定义循环列表吗? http://en.wikipedia.org/wiki/Linked_list
第一个问题是什么是循环erlang 中的列表意思是什么? 它是否有两个元素,一个元素是它自己,旁边是指向下一个元素的地址,存储在列表中?
如果是这样,我可以说有可能在 erlang 中定义循环列表。 但我需要澄清天气是我认为的 erlang 中的循环列表吗?
is it possible to define a circular list in erlang?
http://en.wikipedia.org/wiki/Linked_list
first question would be what exactly a circular list mean in erlang?
is it with two elements, one element its self and next to it address to the next element, stored in a list?
if so i can say there is a possibility of defining a circular list in erlang.
but i need clarification weather is it what i think a circular list is in erlang?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有内置的列表机制可以做到这一点。但是,您可以使用包含您访问过或未访问过的元素的元组来构建一个。
基本结构是一个包含两个列表的元组:
{Old, New}
。当您第一次从空列表开始时,它看起来像{[],[]}
。当您填充列表时,您将其填充到New
列表中:要在列表中移动,您要做的就是首先在
New
列表中查找,并将值放入旧的:那很好,它的工作原理就好像我们只是丢弃旧元素一样。当我们到达列表末尾时会发生什么?我们需要修复这个函数(还有 peek 函数):
如果列表中没有任何内容,它就会崩溃。如果您想通过特殊大小写,也可以返回“未定义”:
这样您就可以使用函数“next”、“peek”以及可能的“delete”(见下文)来执行正常操作。我们还可以添加一个“上一页”功能以允许向后浏览:
这应该涵盖大部分内容。
There is no built-in list mechanism to do it. However, you can build one using a tuple holding the elements you've visited or not.
The basic structure is a tuple with two lists:
{Old, New}
. When you first start with an empty list, it looks like{[],[]}
. When you fill the list, you fill it in theNew
list:To move within the list, what you do is first seek in the
New
list, and put the value in the old one:That's fine and it works as if we were just discarding old elements. What happens when we hit the end of the list though? We need to fix the function (and also the peek one):
If there's nothing in the list, it crashes. You could also return 'undefined' if you wanted to by special casing it:
This then lets you use the function 'next', 'peek' and possibly 'delete' (see below) to do normal stuff. We could also add a 'prev' function to allow backwards browsing:
And that should cover most of it.
看到erlang和erlang虚拟机只支持不可变数据,不可能构建循环列表。如果您要以某种“非法”方式自己构建一个,那么不确定内存管理是否可以正确处理它。
Seeing erlang, and the erlang virtual machine, only supports immutable data it is impossible to build a circular list. If you were to build one yourself in some "illegal" way then it is not certain that the memory management could handle it properly.
Erlang 中没有虚拟机支持的循环列表。如果你想要的话,你必须自己建造它们。
There are no circular lists in Erlang supported by the virtual machine. You have to build them yourself if you want one.
为什么是的,你可以;)
这里有一些蹩脚的代码来证明这一点
Why yes you can ;)
And here is some crappy code to prove it
如上所述,您必须自己实施它们。但是,由于您可以在 erlang 中以各种方式将数据与其他数据关联起来,因此没有什么可以阻止您这样做。
本质上,您只需要一个代表当前索引的东西和另一个代表指向下一个索引的指针。一种有趣的方法是为列表中的每个元素启动一个进程,通过其 PID 指向下一个(或上一个)进程(元素)。一个(或多个)特殊用途进程可能正在抓取那些其他“列表”进程。不太疯狂的方法可能会使用 ets 或 mnesia。
As pointed out above, you would have to implement them yourself. But as you can associate data to other data in various ways in erlang there is nothing stopping you from doing so.
Essentially you need just one thingie representing the current index and another one representing the pointer to the next index. One funny way would be starting a process for each element in the list pointing to the next(or previous) process(element) by its PID. One (or many) special purpose process(es) could be crawling those other "list"-processes. Less crazy aproaches might make use of ets or mnesia.