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


当前回答

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

OO的方式并不总是“正常的”。这个十年的标准是上个十年的边缘化概念。 函数式编程是数学。Paul Graham谈Lisp(用函数式编程取代Lisp):

简单解释一下 20世纪50年代的语言并没有过时 不是技术而是数学,而且 数学不会过时。正确的 Lisp不是20世纪50年代的东西 硬件,但是,比如说,快速排序 算法,是在 1960年,现在仍然是最快的 通用的排序。

其他回答

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键来维护应用程序状态,并维护有状态命令语言的抽象(有时相当容易泄漏)。

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

我不认为函数式编程方法“流行起来”有任何问题,因为它(作为一种编程风格)已经被使用了大约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.

我要指出的是,你所说的关于函数式语言的一切,大约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的转变是好的吗?

大学里并没有真正教授(或者现在有吗?)

我不知道现在的情况,但在90年代中期,我在计算机科学课程中学习了米兰达语言和Lisp语言。尽管从那以后没有使用纯函数语言,但它影响了我解决问题的方式。

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

在90年代中期的CS课程中,面向对象(使用Eiffel教授)的教学与函数式编程相当。两者在当时都是非主流的。面向对象现在可能是“正常”的,但过去并非如此。

我很有兴趣看看f#是否是将FP推向主流的东西。

函数式编程将很可能成为工程师和科学家用来解决他们所面临的问题的工具。它不会像早期的语言那样占领世界。然而,最难打败的产品是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。