我在这里看到很多关于函数式语言的讨论。为什么你要使用传统语言而不是传统语言呢?他们在哪些方面做得更好?他们更不擅长什么?理想的函数式编程应用程序是什么?
当前回答
实际上,在阅读了《黑客与画家》之后,我正在学习LISP,我相信我会从LISP中学到一些东西,这将使我更好地理解我所编程的其他东西。现在我不认为我会在我的日常生活中使用LISP,只是因为有人在1995年创建了一个网站,成为雅虎商店。所以无论如何这是一个双赢(如果它流行起来,我赢了,如果没有,我得到了更多关于如何编程和如何工作的观点)
现在…关于另一个有点相关的问题,我认为明年32核处理器到来后编程会发生很大变化吗?是的,我不知道它是否会是函数式编程,但是…我很确定会有一些不同的东西!
其他回答
一些想法:
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年内不会出现反弹(一如既往)。然而,朝着计划生育的趋势是有好处的,值得关注。
除了其他答案之外,用纯函数术语来描述解决方案可以迫使人们更好地理解问题。相反,用函数式的思维方式会培养出更好的解决问题的能力。
*要么是因为功能范式更好,要么是因为它可以提供额外的攻击角度。
函数式语言使用不同于命令式语言和面向对象语言的范式。他们使用无副作用函数作为语言的基本构建块。这使得很多事情成为可能,也让很多事情变得更加困难(或者在大多数情况下与人们习惯的不同)。
函数式编程的最大优点之一是无副作用函数的执行顺序并不重要。例如,在Erlang中,这用于以一种非常透明的方式启用并发。 因为函数式语言中的函数与数学函数的行为非常相似,所以很容易将它们翻译成函数式语言。在某些情况下,这可以使代码更具可读性。
传统上,函数式编程的一大缺点也是没有副作用。如果没有IO,很难编写有用的软件,但是如果函数中没有副作用,IO很难实现。因此,大多数人从函数式编程中得到的最多的就是从一个输入计算一个输出。在现代混合范式语言(如f#或Scala)中,这更容易。
许多现代语言都有函数式编程语言的元素。c# 3.0有很多函数式编程特性,你也可以在Python中进行函数式编程。我认为函数式编程流行的原因主要有两个原因:并发正在成为常规编程中的一个真正的问题,因为我们拥有越来越多的多处理器计算机;而且这些语言越来越容易使用。
函数式编程将很可能成为工程师和科学家用来解决他们所面临的问题的工具。它不会像早期的语言那样占领世界。然而,最难打败的产品是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。
我一直对“下一件大事”持怀疑态度。很多时候,下一个大事件纯粹是历史的偶然,无论技术好坏,它都在正确的时间出现在正确的地点。例如:c++, Tcl/Tk, Perl。所有的技术都是有缺陷的,都非常成功,因为它们被认为要么解决了当时的问题,要么与根深蒂固的标准几乎相同,或者两者兼而有之。函数式编程可能确实很棒,但这并不意味着它会被采用。
But I can tell you why people are excited about functional programming: many, many programmers have had a kind of "conversion experience" in which they discover that using a functional language makes them twice as productive (or maybe ten times as productive) while producing code that is more resilient to change and has fewer bugs. These people think of functional programming as a secret weapon; a good example of this mindset is Paul Graham's Beating the Averages. Oh, and his application? E-commerce web apps.
自2006年初以来,也有一些关于函数式编程和并行的讨论。因为像Simon Peyton Jones这样的人至少从1984年开始就一直在担心并行性,所以在函数式语言解决多核问题之前,我不会屏住呼吸。但它确实解释了目前一些额外的话题。
In general, American universities are doing a poor job teaching functional programming. There's a strong core of support for teaching intro programming using Scheme, and Haskell also enjoys some support there, but there's very little in the way of teaching advanced technique for functional programmer. I've taught such a course at Harvard and will do so again this spring at Tufts. Benjamin Pierce has taught such a course at Penn. I don't know if Paul Hudak has done anything at Yale. The European universities are doing a much better job; for example, functional programming is emphasized in important places in Denmark, the Netherlands, Sweden, and the UK. I have less of a sense of what's happening in Australasia.