我在这里看到很多关于函数式语言的讨论。为什么你要使用传统语言而不是传统语言呢?他们在哪些方面做得更好?他们更不擅长什么?理想的函数式编程应用程序是什么?


当前回答

你最近有关注编程语言的发展吗?所有主流编程语言的每一个新版本似乎都从函数式编程中借用了越来越多的特性。

Closures, anonymous functions, passing and returning functions as values used to be exotic features known only to Lisp and ML hackers. But gradually, C#, Delphi, Python, Perl, Javascript, have added support for closures. Its not possible for any up-and-coming language to be taken seriously without closures. Several languages, notably Python, C#, and Ruby have native support for list comprehensions and list generators. ML pioneered generic programming in 1973, but support for generics ("parametric polymorphism") has only become an industry standard in the last 5 years or so. If I remember correctly, Fortran supported generics in 2003, followed by Java 2004, C# in 2005, Delphi in 2008. (I know C++ has supported templates since 1979, but 90% of discussions on C++'s STL start with "here there be demons".)

是什么让这些功能吸引程序员?这应该是显而易见的:它帮助程序员编写更短的代码。如果想要保持竞争力,未来所有的语言都将至少支持闭包。在这方面,函数式编程已经成为主流。

大多数应用程序都很简单 可以用正常的面向对象方法解决

谁说不能用函数式编程来处理简单的事情?并不是每个函数程序都需要是编译器、定理证明器或大型并行通信交换机。除了更复杂的项目外,我还经常使用f#来编写临时脚本。

其他回答

I don't know whether it will catch on or not, but from my investigations, a functional language is almost certainly worth learning, and will make you a better programmer. Just understanding referential transparency makes a lot of design decisions so much easier- and the resulting programs much easier to reason about. Basically, if you run into a problem, then it tends to only be a problem with the output of a single function, rather than a problem with an inconsistant state, which could have been caused by any of the hundreds of classes/methods/functions in an imparative language with side effects.

FP的无状态本质更自然地映射到web的无状态本质,因此函数式语言更容易让自己更优雅,更RESTFUL的web应用程序。与JAVA和. net框架形成鲜明对比的是,它们需要在本质上无状态的功能平台(如web)上使用VIEWSTATE和SESSION键来维护应用程序状态,并维护有状态命令语言的抽象(有时相当容易泄漏)。

而且,应用程序越无状态,就越容易进行并行处理。如果你的网站很受欢迎,这对网络来说非常重要。向站点添加更多硬件以获得更好的性能并不总是那么简单。

实际上,在阅读了《黑客与画家》之后,我正在学习LISP,我相信我会从LISP中学到一些东西,这将使我更好地理解我所编程的其他东西。现在我不认为我会在我的日常生活中使用LISP,只是因为有人在1995年创建了一个网站,成为雅虎商店。所以无论如何这是一个双赢(如果它流行起来,我赢了,如果没有,我得到了更多关于如何编程和如何工作的观点)

现在…关于另一个有点相关的问题,我认为明年32核处理器到来后编程会发生很大变化吗?是的,我不知道它是否会是函数式编程,但是…我很确定会有一些不同的东西!

我的观点是,既然微软已经把它推向了主流,它就会流行起来。对我来说,它很有吸引力,因为它能为我们做什么,因为它是一个新的挑战,因为它为未来提供了工作机会。

一旦掌握了它,它将成为进一步帮助我们提高编程效率的另一个工具。

为什么函数式编程很重要

Slava Akhmechet写了一篇很棒的文章,叫做《函数式编程》(顺便说一下,正是这篇文章让我开始接触FP)。在FP带来的好处中,他非常规地强调了以下几点(我认为这有助于软件工程师的吸引力):

单元测试 调试 并发性 热码部署 机器辅助证明与优化

然后继续讨论FP中更多传统讨论的方面的优点,如高阶函数、咖喱、惰性求值、优化、抽象控制结构(尽管没有讨论单子)、无限数据结构、严格性、延续、模式匹配、闭包等。

强烈推荐!