F# 中的不可变 Trie 结构
我正在尝试使用 aho-corasick 算法来尝试使用 F# 来改进一点,并且我遇到了 Trie 实现的问题,它们都是可变的或者无法进行尾部调用优化。
我所看到的基本问题是,不可变数据结构必须“自下而上”构建,因为您无法更改它们指向的内容,因此您的选择要么使它们可变,要么在进行过程中找出节点(即构造中的递归)。
有没有办法制作一个不可变的 trie 数据结构,并在构造上进行尾调用优化?(并且不会因复制而损失效率。)
I am playing around with the aho-corasick algorithm to try and get a little better with F#, and I ran across a problem with the Trie implementations, they are all mutable or can't be tail call optimized.
The basic issue from what I can see is that immutable data structures have to be built "bottom up" since you can't change what they point to, so your options are either make them mutable, or find out the nodes as you go along(i.e. recurse in construction).
Is there any way of making an immutable trie data structure with tail call optimizations on the construction?(and not lose efficiency by copying.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尾部调用优化可以通过使用延续来消除。这是一个示例,其中键和值分别为
string
和int
如果您想坚持使用不可变结构,消除副本会有点困难
The tail call optimiation can be eliminated by using a continuation. Here's a sample where the key and value are
string
andint
respectivelyEliminating the copies is a bit more difficult if you want to stick with immutable structures
我在研究我的代码审查帖子时发现了这篇文章,我在其中实现了不可变的特里树。
它使用链接映射而不是二叉树来提高性能。
I came across this post while researching for my code review post where I've implemented at immutable trie.
It is performant using maps for links rather than binary trees.