(纯)函数式编程与“经典算法”对立吗?
经典算法书籍(TAOCP、CLR)(以及不那么经典的书籍,例如 fxtbook)充满了命令式算法。这对于其实现很大程度上基于数组的算法最为明显,例如组合生成(其中算法中同时使用数组索引和数组值)或并查找算法。
这些算法的最坏情况复杂度分析取决于数组访问的 O(1)。如果将数组替换为类似数组的持久结构(例如 Clojure 所做的),则数组访问不再是 O(1),并且这些算法的复杂性分析不再有效。
这让我想到以下问题:纯函数式编程与经典算法文献不兼容吗?
The classic algorithm books (TAOCP, CLR) (and not so classic ones, such as the fxtbook)are full of imperative algorithms. This is most obvious with algorithms whose implementation is heavily based on arrays, such as combinatorial generation (where both array index and array value are used in the algorithm) or the union-find algorithm.
The worst-case complexity analysis of these algorithms depends on array accesses being O(1). If you replace arrays with array-ish persistent structures, such as Clojure does, the array accesses are no longer O(1), and the complexity analysis of those algorithms is no longer valid.
Which brings me to the following questions: is pure functional programming incompatible with the classical algorithms literature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简而言之,只要算法完成后没有可以观察到的效果(除了返回的结果),那么它就是纯粹的。即使您执行破坏性数组更新或突变等操作,这一点也成立。
如果你有一个像这样的算法:
假设我们上面的伪代码是词法范围的,只要首先复制数组参数并且返回的数组是一个全新的东西,这个算法就代表一个纯函数(在这种情况下,Haskell函数
fmap (const 0)
可能会起作用)。书中展示的大多数“命令式”算法实际上都是纯函数,并且使用 ST 之类的东西在纯函数设置中以这种方式编写它们是完全可以的。我建议您查看 Mercury 或 Disciple Disciplined Compiler,以了解在破坏中仍然蓬勃发展的纯语言。
The short answer is that, so long as the algorithm does not have effects that can be observed after it finishes (other than what it returns), then it is pure. This holds even when you do things like destructive array updates or mutation.
If you had an algorithm like say:
Assuming our pseudocode above is lexically scoped, so long as the array parameter is first copied over and the returned array is a wholly new thing, this algorithm represents a pure function (in this case, the Haskell function
fmap (const 0)
would probably work). Most "imperative" algorithms shown in books are really pure functions, and it is perfectly fine to write them that way in a purely functional setting using something like ST.I would recommend looking at Mercury or the Disciple Disciplined Compiler to see pure languages that still thrive on destruction.
您可能对此相关问题感兴趣:纯函数式编程的效率。
You may be interested in this related question: Efficiency of purely functional programming.
它不是。但确实,人们可以在许多书籍中看到看起来只能在命令式语言中使用的算法。主要原因是纯函数式编程长期以来仅限于学术用途。然后,这些算法的作者强烈依赖命令式功能成为主流。现在,考虑两种广泛使用的算法:快速排序和合并排序。快速排序比归并排序更加“势在必行”;它的优势之一就是到位。合并排序(在某种程度上)比快速排序更“纯粹”,因为它需要复制并保持其数据持久性。实际上很多算法都可以用纯函数式编程来实现,而不会损失太多效率。例如,著名的 Dragon Book 中的许多算法都是如此。
It is not. But it is true that one can see in many book algorithms that look like they are only usable in imperative languages. The main reason is that pure functional programming was restrained to academic use for a long time. Then, the authors of these algorithms strongly relied on imperative features to be in the mainstream. Now, consider two widely spread algorithms: quick sort and merge sort. Quick sort is more "imperative" than merge sort; one of its advantage is to be in place. Merge sort is more "pure" than quick sort (in some way) since it needs to copy and keep its data persistent. Actually many algorithm can be implemented in pure functional programming without losing too much efficiency. This is true for many algorithms in the famous Dragon Book for example.
在数据结构方面,Chris Okasaki 进行了大量研究,将经典数据结构采用到纯功能设置中,因为许多标准数据结构在使用破坏性更新时不再起作用。他的书《纯函数式数据结构》展示了一些结构(如二项堆和红/黑树)如何在函数设置中很好地实现,而其他基本结构(如数组和队列)必须使用更复杂的概念来实现。
如果您有兴趣研究核心算法的这一分支,他的书将是一个很好的起点。
With respect to data structures, Chris Okasaki has done substantial research into adopting classic data structures into a purely functional setting, as many of the standard data structures no longer work when using destructive updates. His book "Purely Functional Data Structures" shows how some structures, like binomial heaps and red/black trees, can be implemented quite well in a functional setting, while other basic structures like arrays and queues must be implemented with more elaborate concepts.
If you're interested in pursuing this branch of the core algorithms, his book would be an excellent starting point.