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


当前回答

我不认为函数式编程方法“流行起来”有任何问题,因为它(作为一种编程风格)已经被使用了大约40年。每当OO程序员编写有利于不可变对象的干净代码时,这些代码就是借用了函数概念。

然而,这些天来,强制函数式风格的语言正在获得大量的虚拟墨水,这些语言是否会在未来占据主导地位是一个悬而未决的问题。我自己的怀疑是混合的、多范式的语言,如Scala或OCaml 将很可能统治“纯粹的”函数语言,就像纯粹的OO语言(Smalltalk、Beta等)影响了主流编程一样,但还没有成为最广泛使用的表示法。

最后,我忍不住要指出,你对FP的评论与我几年前从过程程序员那里听到的评论高度相似:

(恕我直言,这是神话)“普通”程序员不理解它。 这并没有被广泛教授。 任何你能用它来写的程序,都能用现有的技术以另一种方式来写。

Just as graphical user interfaces and "code as a model of the business" were concepts that helped OO become more widely appreciated, I believe that increased use of immutability and simpler (massive) parallelism will help more programmers see the benefits that the functional approach offers. But as much as we've learned in the past 50 or so years that make up the entire history of digital computer programming, I think we still have much to learn. Twenty years from now, programmers will look back in amazement at the primitive nature of the tools we're currently using, including the now-popular OO and FP languages.

其他回答

函数式语言的一个关键特征是一类函数的概念。其思想是,您可以将函数作为参数传递给其他函数,并将它们作为值返回。

函数式编程包括编写不改变状态的代码。这样做的主要原因是,对函数的连续调用将产生相同的结果。您可以使用任何支持第一类函数的语言编写函数式代码,但有一些语言(如Haskell)不允许更改状态。事实上,你根本不应该产生任何副作用(比如打印文本)——这听起来可能完全没用。

相反,Haskell对IO: monads采用了不同的方法。这些对象包含解释器顶层要执行的所需IO操作。在其他任何层面上,它们都只是系统中的对象。

函数式编程有什么优点?函数式编程允许代码出现错误的可能性更小,因为每个组件都是完全隔离的。此外,使用递归和一级函数允许简单的正确性证明,这通常反映了代码的结构。

除了其他答案之外,用纯函数术语来描述解决方案可以迫使人们更好地理解问题。相反,用函数式的思维方式会培养出更好的解决问题的能力。

*要么是因为功能范式更好,要么是因为它可以提供额外的攻击角度。

微软正在Visual Studio上大力推广f#的下一个版本。它是一种像Scala一样的混合语言,并且可以很好地与。net框架的其余部分集成。我认为许多微软公司将使用它来加速高度并行的数据处理应用程序和功能的开发。

函数式编程将很可能成为工程师和科学家用来解决他们所面临的问题的工具。它不会像早期的语言那样占领世界。然而,最难打败的产品是Excel,如果我是一名工程师,需要做计算,Excel是很棒的。

However, F# is going to be another source and will likely fill design needs by the non-Computer Scientists. Let's face it, Computer Scientists have done a great job of creating a WHOLE new way of doing things. Object Oriented Programming is GREAT. But sometimes you just need a way to solve an equation, get a solution and graph it. That's it. Then a language like F# fills the bill. Or maybe you want to build a finite state machine, F# again could be one of the solutions, but then C could be a solution as well.

但是当涉及到并行处理时,Excel大放异彩,f#也会及时出现。但是要以友好的方式,F#= friendly。

一些想法:

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年内不会出现反弹(一如既往)。然而,朝着计划生育的趋势是有好处的,值得关注。