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


当前回答

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

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#来编写临时脚本。

其他回答

函数式语言使用不同于命令式语言和面向对象语言的范式。他们使用无副作用函数作为语言的基本构建块。这使得很多事情成为可能,也让很多事情变得更加困难(或者在大多数情况下与人们习惯的不同)。

函数式编程的最大优点之一是无副作用函数的执行顺序并不重要。例如,在Erlang中,这用于以一种非常透明的方式启用并发。 因为函数式语言中的函数与数学函数的行为非常相似,所以很容易将它们翻译成函数式语言。在某些情况下,这可以使代码更具可读性。

传统上,函数式编程的一大缺点也是没有副作用。如果没有IO,很难编写有用的软件,但是如果函数中没有副作用,IO很难实现。因此,大多数人从函数式编程中得到的最多的就是从一个输入计算一个输出。在现代混合范式语言(如f#或Scala)中,这更容易。

许多现代语言都有函数式编程语言的元素。c# 3.0有很多函数式编程特性,你也可以在Python中进行函数式编程。我认为函数式编程流行的原因主要有两个原因:并发正在成为常规编程中的一个真正的问题,因为我们拥有越来越多的多处理器计算机;而且这些语言越来越容易使用。

一些想法:

The debate between FP and imperative programming (OO, structured, etc), has been raging since Lisp versus Fortran. I think you pose excellent questions but recognize that they are not especially new. Part of the hoopla over FP is that we seem to be recognizing that concurrency is very difficult, and that locks and other mechanisms in OO (e.g. Java) are just one solution. FP offers a refreshing sea change with ideas such as Actors and the power of stateless computing. To those wrestling with OO, the landscape seems highly appealing. Yes, schools teach FP. In fact, the University of Waterloo and others offer Scheme in first year classes (reference here). Regarding the average programmer, I'm sure that the same arguments were given against C++ back in the early 1990s. And look what happened. If businesses can gain an advantage via a technology, you can bet that people will receive training.

这并不是说这是板上钉钉的事,也不是说在3-5年内不会出现反弹(一如既往)。然而,朝着计划生育的趋势是有好处的,值得关注。

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

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

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

强烈推荐!

我要指出的是,你所说的关于函数式语言的一切,大约20年前,大多数人都在谈论面向对象语言。在那时候,OO是很常见的:

* The average corporate programmer, e.g. most of the people I work with, will not understand it and most work environments will not let you program in it
* It's not really taught at universities (or is it nowadays?)
* Most applications are simple enough to be solved in normal IMPERATIVE ways

改变必须来自某个地方。无论接受过早期技术培训的人是否认为变革没有必要,有意义的重要变革都会发生。尽管当时有很多人反对,但你认为向OO的转变是好的吗?

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

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