引用函数式数据结构时使用哪个术语:持久性还是不可变?

发布于 2024-08-27 03:38:17 字数 296 浏览 6 评论 0原文

在函数式编程的上下文中,哪个是正确的术语:持久性还是不可变?当我在 Google 上搜索“不可变数据结构”时,我得到了一篇关于“持久数据”的文章的 维基百科链接结构”甚至继续说道:

这样的数据结构是有效的 不可变

这让我更加困惑。函数式程序依赖于持久数据结构还是不可变数据结构?或者它们总是同一件事?

In the context of functional programming which is the correct term to use: persistent or immutable? When I Google "immutable data structures" I get a Wikipedia link to an article on "Persistent data structure" which even goes on to say:

such data structures are effectively
immutable

Which further confuses things for me. Do functional programs rely on persistent data structures or immutable data structures? Or are they always the same thing?

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

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

发布评论

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

评论(6

热情消退 2024-09-03 03:38:17

函数式数据结构的正确术语是不可变。 “持久”一词至少有三种使用方式:

  • 持久数据结构指的是这样的情况:您有一个旧的数据结构,您创建了一个新的数据结构,但您保留了一个指针到旧的。通常,旧的和新的共享许多状态——它们可能仅在恒定数量的堆对象或线性数量的堆数据结构上有所不同。这种持久性是具有不可变数据结构的结果,加上一种算法,该算法保留指向旧版本数据结构的指针,允许它们持久存在。

  • 持久变量是指其值在同一程序的多次调用中保持不变的变量。这可以通过语言功能或库来完成。

  • 持久编程语言是一种提供持久变量的语言。圣杯是正交持久性:程序员可以决定变量是否持久,独立于该变量的所有其他属性。目前,这种编程语言还处于遥远的研究阶段,但保留术语区别很有用。

我今天不想编辑维基百科:-(

The proper term for functional data structures is immutable. The teerm "persistent" is used in at least three ways:

  • A persistent data structure refers to the situation where you have an old data structure, you create a new one, but you keep a pointer to the old one. Typically the old one and new one share a lot of state—they may differ only by a constant number of heap objects or perhaps a linear number of heap data structures. This kind of persistence is a consequence of having immutable data structures, plus an algorithm that retains pointers to old versions of a data structure, allowing them to persist.

  • A persistent variable is one whose value persists across multiple invocations of the same program. This can be done with language features or libraries.

  • A persistent programming language is one that provides persistent variables. The holy grail is orthogonal persistence: a programmer can decide whether a variable is persistent, independent of all the other properties of that variable. At the moment, this kind of programming language is far-out research, but it's useful to preserve the terminological distinction.

I don't feel up to editing Wikipedia today :-(

抚笙 2024-09-03 03:38:17

持久数据结构在更改后仅保留其自身的先前版本。根据持久数据结构的类型...您可能能够也可能无法修改以前的版本。

不可变类型根本无法更改。

函数式语言主要依赖于不可变类型(也称为值)来存储数据(尽管您可以在某些语言中使用可变类型……但必须显式完成)。

A persistent data struture preserves only the previous version of itself after a change. Depending on the type of persistent data structure...you may or may not be able to modify previous versions.

An immutable type can not be changed at all.

Functional Languages primarily rely on Immutable types (also called values) for their data storage (even though you can use Mutable types in some...but it has to be done explicitly).

谢绝鈎搭 2024-09-03 03:38:17

该文章还说“在纯函数式程序中,所有数据都是不可变的”,这是事实。

在我看来,你真的不需要做出这种区分。如果您使用函数式语言或完全函数式风格进行编程(而不是在方便的情况下在命令式代码中使用函数式习惯用法),那么您可以只说“数据结构”。根据定义,它们将是不可变的和持久的。

如果您出于某种原因需要进行区分,那么“持久”可能更适合树和队列等动态结构,其中值似乎根据执行跟踪“更改”,而对于简单值对象来说“不可变”。

The article also says "in a purely functional program all data is immutable," which is true.

In my opinion you don't really need to make this distinction. If you're programming in a functional language or in a completely functional style -- as opposed to using functional idioms in imperative code where convenient -- then you can just say "data structure." By definition they will be immutable and persistent.

If you need to make the distinction for some reason, then "persistent" might be more appropriate for dynamic structures like trees and queues, where values appear to "change" based on execution traces, and "immutable" for simple value objects.

屋檐 2024-09-03 03:38:17

不可变通常意味着“不改变”。持久通常意味着“存储在永久存储介质上”。然而,你提到的维基百科文章似乎认为这两个词的意思非常相似(但不完全相同)。事实上它指出:

持久数据结构不是致力于持久存储的数据结构,例如磁盘;这是“持久”一词的不同且不相关的含义。

Immutable generally means "does not change". Persistent generally is taken to mean "stored on permanent storage medium". However, the Wikipedia article you mention seems to take the two words to mean very similar things (but not exact same). In fact it states:

A persistent data structure is not a data structure committed to persistent storage, such as a disk; this is a different and unrelated sense of the word "persistent."

思念满溢 2024-09-03 03:38:17

“不可变”的使用频率要高得多,因为“持久”被重载了(通常它意味着“存储在程序之外并比程序寿命更长”),甚至正确的定义也带有额外的语义包袱,而这些包袱通常与纯函数式编程的独特质量无关——事实上,不存在可变状态。 A = A,并且对于 A 的所有值始终如此。

"Immutable" is used far more often, as "persistent" is overloaded (normally it means "stored outside of and outliving the program") and even the correct definition carries additional semantic baggage that's often unrelated to the distinguishing quality of purely functional programming — the fact that there is no mutable state. A = A and always will for all values of A.

李白 2024-09-03 03:38:17

本文中,作者使用了“持久”一词”的意思是“尽管在幕后实现了突变,但观察上是不可变的”。在这种特殊情况下,突变被函数式而非纯粹的编程语言的模块系统隐藏。

In this article, the authors use the word "persistent" as meaning "observationally immutable, although implemented with mutations under the hood". In that particular case, the mutations are hidden by the module system of a functional, but not pure, programming language.

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