问题描述
有大量关于数据结构的文本,以及数据结构代码库.我知道纯函数式数据结构更容易推理.然而,我很难理解在实际代码中使用纯函数式数据结构(使用或不使用函数式编程语言)而不是命令式对应物的现实优势.有人可以提供一些真实世界的案例,其中纯函数式数据结构具有优势以及为什么?
There are large number of texts on data structures, and libraries of data structures code. I understand that purely functional data structure is easier to reason about. However I have trouble to understand the real world advantage of using purely functional data structure in pragmatic code (using functional programming language or not) over the imperative counterpart. Can somebody provide some real world cases where purely functional data structure has advantage and why?
类似我在programming_language中使用data_structure_name来做应用的例子,因为它可以做certain_thing.
Examples along the line like I use data_structure_name in programming_language to do application because it can do certain_thing.
谢谢.
PS:我所说的纯函数式数据结构与持久化数据结构不同.持久化数据结构是不会改变的数据结构??另一方面,纯函数数据结构是一种纯粹操作的数据结构.
PS: What I mean by purely functional data structure is not the same as persistent data structure. Persistent data structure is a data structure that doesn't change?? On other hand purely functional data structure is a data structure that operates purely.
推荐答案
纯函数式(又名持久或不可变)数据结构为您提供了几个优势:
Purely functional (aka persistent or immutable) data structures give you several advantages:
- 您永远不必锁定它们,这极大地提高了并发性.
- 它们可以共享结构,从而减少内存使用.例如,考虑 Haskell 中的列表 [1, 2, 3, 4] 和一些命令式语言(如 Java).要在 Haskell 中生成新列表,您只需创建新的
cons
(一对值和对下一个元素的引用)并将其连接到前一个列表.在 Java 中,您必须创建全新的列表,以免损坏之前的列表. - 您可以懒惰.
- 此外,如果您使用函数式风格,您可以避免考虑操作的时间和顺序,从而使您的程序更加声明性.
- 事实上,数据结构是不可变的,允许您做出更多假设,从而扩展语言能力.例如,Clojure 使用不变性这一事实在每个对象上正确提供 hashCode() 方法的实现,因此任何对象都可以用作地图中的键.
- 通过不可变数据和函数式风格,您还可以自由使用记忆化.
- you never have to lock them, which extremely improves concurrency.
- they can share structure, which reduces memory usage. For example, consider list [1, 2, 3, 4] in Haskell and some imperative language like Java. To produce new list in Haskell you only have to create new
cons
(pair of value and reference-to-next-element) and connect it to the previous list. In Java you have to create completely new list not to damage the previous one. - you can make persistent data structures lazy.
- also, if you use functional style, you can avoid thinking of time and sequence of operations, and so, make your programs more declarative.
- fact, that the data structure is immutable, allows you to make some more assumptions and so expand capabilities of language. For example, Clojure uses the fact of immutability to correctly provide implementations of hashCode() method on each object, so any object may be used as a key in a map.
- with immutable data and functional style you can also freely use memoization.
还有更多优势,总的来说,它是另一种模拟现实世界的方式.这个和其他一些SICP 的章节将使您更准确地了解具有不可变结构的编程及其优缺点.
There's much more advantages, in general, it is another way of modeling the real world. This and some other chapters from SICP will give you more accurate view of programming with immutable structures, its advantages and disadvantages.
这篇关于纯函数式数据结构有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!