In which situations should I choose to use a functional programming language over a more verbose object oriented language like C++, C# or Java?
在哪种情况下,我应该选择使用函数式编程语言而不是像C ++,C#或Java这样更详细的面向对象语言?
I understand what functional programming is, what I don't really understand is for what types of problems is it a perfect solution?
我理解什么是函数式编程,我真正理解的是什么类型的问题是一个完美的解决方案?
8 个解决方案
#1
Functional languages are, in my opinion, good for mainly two things: Game AIs and mathematical computations. It's good in game AIs because of its nice list manipulations (at least in Lisp and Scheme), and for mathematical computations because of its syntax. Scheme, Lisp, and Haskell have a syntax that makes mathematical computations easy to read. The last thing I have to add is that functional languages are really fun languages. My Scheme course was one of the courses I had the most fun with.
在我看来,功能语言主要用于两件事:游戏AI和数学计算。它在游戏AI中很好,因为它有很好的列表操作(至少在Lisp和Scheme中),并且由于它的语法而用于数学计算。 Scheme,Lisp和Haskell的语法使数学计算易于阅读。我要补充的最后一件事是功能语言真的很有趣。我的计划课程是我最开心的课程之一。
#2
I've done some research into this myself and thought I might add CONCURRENCY to the list of reasons to use a functional language. See at some point in the near future processor speed will not be able to increase using the same cpu technology. The physics of the architecture won't allow it.
我自己做了一些研究,并认为我可以将CONCURRENCY添加到使用函数式语言的原因列表中。在不久的将来看到处理器速度在某些时候将无法使用相同的CPU技术增加。架构的物理特性不允许它。
So that is where concurrent processing comes in.
这就是并发处理的用武之地。
Unfortunately most OOP languages cannot take advantage of multiple processors at once because of the interdependencies between data.
不幸的是,由于数据之间的相互依赖性,大多数OOP语言不能同时利用多个处理器。
In pure functional programming languages the computer can run two (or many more) functions at once because those functions are not altering outside state information.
在纯函数式编程语言中,计算机可以同时运行两个(或更多)函数,因为这些函数不会改变外部状态信息。
Here is an excellent article on functional programming you may enjoy.
这是一篇关于您可能喜欢的函数式编程的优秀文章。
#3
A friend of mine quoted one of his college professors as saying something like the following:
我的一位朋友引用他的一位大学教授的话说如下:
Draw a grid with data types across the top and operations on those data types down the left side. If you slice the grid vertically, you're doing OO; if you slice the grid horizontally, you're doing FP.
在顶部绘制一个包含数据类型的网格,在左侧绘制对这些数据类型的操作。如果你垂直切割网格,你就是做OO;如果你水平切割网格,你就是在做FP。
My answer is that FP is a completely viable approach to programming with as wide a range of application as OO. As with any programming-language-choice discussion, I think the more important questions are:
我的答案是FP是一种完全可行的编程方法,其应用范围与OO一样广泛。与任何编程语言选择讨论一样,我认为更重要的问题是:
- Do you have the time to invest in learning a new notation and a new way of thinking?
- Which of the languages you're considering have sufficiently rich libraries that you won't get stuck reinventing some wheel?
您是否有时间投资学习新的符号和新的思维方式?
你正在考虑哪种语言有足够丰富的库,你不会被重新发现一些*?
As to the first question, if you are doing this primarily for the learning value, I'd suggest looking at Haskell, because of the wide range of support materials (books, articles, active community, etc.)
关于第一个问题,如果你这样做主要是为了学习价值,我建议看看Haskell,因为支持材料的范围很广(书籍,文章,活跃社区等)
As to the second question, Haskell has a nice range of libraries (although some are more mature than others), but Scala (a hybrid OO-functional language running on the JVM) has the advantages that you can more gradually transition into the functional style, and you have the full range of Java-accessible libraries available to you.
至于第二个问题,Haskell有一系列很好的库(尽管有些库比其他库更成熟),但Scala(在JVM上运行的混合OO函数语言)具有以下优点:您可以更加逐步地转换为函数式,您可以使用全系列的Java可访问库。
#4
Practically everything you can do in PP can be done in FP, and same thing in reverse. It's just another way to code something—another perspective on the problem and a different way to solve it.
实际上你在PP中可以做的一切都可以在FP中完成,反之亦然。这只是另一种编码方式 - 对问题的另一种观点以及解决问题的另一种方式。
However, because not many people use FP, the problem is more about the lack of good libraries, portability/maintainability (since the maintainer has a better chance to understand something written in C++ than Scheme), and the lack of documentation and community. I know there ARE some docs, however, compare that to C++, C# or Java and you will understand what I mean.
但是,由于没有多少人使用FP,问题更多的是缺乏良好的库,可移植性/可维护性(因为维护者有更好的机会理解用C ++编写的东西而不是Scheme),以及缺乏文档和社区。我知道有一些文档,但是,与C ++,C#或Java相比,你会理解我的意思。
However, if you really want to do FP, you can use this style even in C++ and C#. Of course, it won't be a 100% FP if you use the standard library. Unfortunately, I don't think C# is optimized to be used with functional programming and it will probably create lot of performance issues.
但是,如果你真的想做FP,你甚至可以在C ++和C#中使用这种风格。当然,如果您使用标准库,它将不是100%FP。不幸的是,我不认为C#被优化用于函数式编程,它可能会产生很多性能问题。
This is more a subjective post, what I think about FP. It is not a rigorous declaration.
这是一个主观的帖子,我对FP的看法。这不是一个严格的声明。
#5
Functional languages are good in a lot of situations. It depends on the libraries of a particular functional language.
功能语言在很多情况下都很好。它取决于特定功能语言的库。
How would you answer the question "When to use an object oriented programming language?"?
您如何回答“何时使用面向对象编程语言?”的问题?
#6
Although this is quite the subjective question, I'd just like to add that functional languages are used a lot to parse domain specific languages; the functional nature lends well to parsing grammars.
虽然这是一个非常主观的问题,但我想补充的是,函数式语言用于解析特定于域的语言;功能性很好地解析语法。
#7
In general, use the language in which it's easiest to express the solution to a problem. For functional programming, this is when the solution to a problem is easily expressed in terms of functions, hence the name. Generally it's good for mathematical operations, AI, pattern matching; in general anything that can be broken down into a set of rules that must be applied to get an answer. You can only really determine the "best" language to use after you've analyzed your problem sufficiently. This is where pseudo-code comes in handy. If you find yourself writing pseudo-code that looks like FP, use FP.
通常,使用最容易表达问题解决方案的语言。对于函数式编程,这就是当问题的解决方案很容易用函数表达时,因此就是名称。通常它对数学运算,AI,模式匹配有好处;一般而言,任何可以分解为一组必须应用于获得答案的规则的东西。在充分分析问题后,您才能真正确定要使用的“最佳”语言。这就是伪代码派上用场的地方。如果您发现自己编写的FP代码看起来像FP,请使用FP。
Of course, all complete programming languages are functionally equivalent, so it doesn't matter really which one you choose in terms of what problems you can solve. The main effects will be in terms of coding efficiency and accuracy, and ease of maintenance.
当然,所有完整的编程语言在功能上都是等价的,因此根据您可以解决的问题确切选择哪一种。主要效果将是编码效率和准确性,以及易于维护。
Note also that it's possible to mimic FP within OO languages through cleverly designed APIs. For instance, I've seen numerous Java libraries (JMock is one example) that use method chaining to simulate an FP DSL. Then you'll see constructs such as:
另请注意,可以通过设计巧妙的API在OO语言中模仿FP。例如,我见过很多使用方法链来模拟FP DSL的Java库(JMock就是一个例子)。然后你会看到如下构造:
logger.expects(once()).method("error") .with( and(stringContains(action),stringContains(cause)) );
logger.expects(once())。method(“error”)。with(和(stringContains(action),stringContains(cause)));
This essentially builds a function that is evaluated to determine whether some calling sequence on a mock object is correct. (example stolen from http://www.jmock.org/yoga.html)
这实际上构建了一个函数,该函数被评估以确定模拟对象上的某些调用序列是否正确。 (例子来自http://www.jmock.org/yoga.html)
Another FP-like syntax in otherwise OO languages is the use of closures, such as in Ruby.
在其他OO语言中,另一种类似FP的语法是使用闭包,例如在Ruby中。
#8
From what I've seen, it's more a matter of taste than functionality (no pun intended). There really isn't anything about either style of language that makes it inherently better or worse at specific tasks.
从我所看到的,它更多的是品味而不是功能(没有双关语意)。在任何一种语言风格中都没有任何东西可以使它在特定任务中本身更好或更差。
#1
Functional languages are, in my opinion, good for mainly two things: Game AIs and mathematical computations. It's good in game AIs because of its nice list manipulations (at least in Lisp and Scheme), and for mathematical computations because of its syntax. Scheme, Lisp, and Haskell have a syntax that makes mathematical computations easy to read. The last thing I have to add is that functional languages are really fun languages. My Scheme course was one of the courses I had the most fun with.
在我看来,功能语言主要用于两件事:游戏AI和数学计算。它在游戏AI中很好,因为它有很好的列表操作(至少在Lisp和Scheme中),并且由于它的语法而用于数学计算。 Scheme,Lisp和Haskell的语法使数学计算易于阅读。我要补充的最后一件事是功能语言真的很有趣。我的计划课程是我最开心的课程之一。
#2
I've done some research into this myself and thought I might add CONCURRENCY to the list of reasons to use a functional language. See at some point in the near future processor speed will not be able to increase using the same cpu technology. The physics of the architecture won't allow it.
我自己做了一些研究,并认为我可以将CONCURRENCY添加到使用函数式语言的原因列表中。在不久的将来看到处理器速度在某些时候将无法使用相同的CPU技术增加。架构的物理特性不允许它。
So that is where concurrent processing comes in.
这就是并发处理的用武之地。
Unfortunately most OOP languages cannot take advantage of multiple processors at once because of the interdependencies between data.
不幸的是,由于数据之间的相互依赖性,大多数OOP语言不能同时利用多个处理器。
In pure functional programming languages the computer can run two (or many more) functions at once because those functions are not altering outside state information.
在纯函数式编程语言中,计算机可以同时运行两个(或更多)函数,因为这些函数不会改变外部状态信息。
Here is an excellent article on functional programming you may enjoy.
这是一篇关于您可能喜欢的函数式编程的优秀文章。
#3
A friend of mine quoted one of his college professors as saying something like the following:
我的一位朋友引用他的一位大学教授的话说如下:
Draw a grid with data types across the top and operations on those data types down the left side. If you slice the grid vertically, you're doing OO; if you slice the grid horizontally, you're doing FP.
在顶部绘制一个包含数据类型的网格,在左侧绘制对这些数据类型的操作。如果你垂直切割网格,你就是做OO;如果你水平切割网格,你就是在做FP。
My answer is that FP is a completely viable approach to programming with as wide a range of application as OO. As with any programming-language-choice discussion, I think the more important questions are:
我的答案是FP是一种完全可行的编程方法,其应用范围与OO一样广泛。与任何编程语言选择讨论一样,我认为更重要的问题是:
- Do you have the time to invest in learning a new notation and a new way of thinking?
- Which of the languages you're considering have sufficiently rich libraries that you won't get stuck reinventing some wheel?
您是否有时间投资学习新的符号和新的思维方式?
你正在考虑哪种语言有足够丰富的库,你不会被重新发现一些*?
As to the first question, if you are doing this primarily for the learning value, I'd suggest looking at Haskell, because of the wide range of support materials (books, articles, active community, etc.)
关于第一个问题,如果你这样做主要是为了学习价值,我建议看看Haskell,因为支持材料的范围很广(书籍,文章,活跃社区等)
As to the second question, Haskell has a nice range of libraries (although some are more mature than others), but Scala (a hybrid OO-functional language running on the JVM) has the advantages that you can more gradually transition into the functional style, and you have the full range of Java-accessible libraries available to you.
至于第二个问题,Haskell有一系列很好的库(尽管有些库比其他库更成熟),但Scala(在JVM上运行的混合OO函数语言)具有以下优点:您可以更加逐步地转换为函数式,您可以使用全系列的Java可访问库。
#4
Practically everything you can do in PP can be done in FP, and same thing in reverse. It's just another way to code something—another perspective on the problem and a different way to solve it.
实际上你在PP中可以做的一切都可以在FP中完成,反之亦然。这只是另一种编码方式 - 对问题的另一种观点以及解决问题的另一种方式。
However, because not many people use FP, the problem is more about the lack of good libraries, portability/maintainability (since the maintainer has a better chance to understand something written in C++ than Scheme), and the lack of documentation and community. I know there ARE some docs, however, compare that to C++, C# or Java and you will understand what I mean.
但是,由于没有多少人使用FP,问题更多的是缺乏良好的库,可移植性/可维护性(因为维护者有更好的机会理解用C ++编写的东西而不是Scheme),以及缺乏文档和社区。我知道有一些文档,但是,与C ++,C#或Java相比,你会理解我的意思。
However, if you really want to do FP, you can use this style even in C++ and C#. Of course, it won't be a 100% FP if you use the standard library. Unfortunately, I don't think C# is optimized to be used with functional programming and it will probably create lot of performance issues.
但是,如果你真的想做FP,你甚至可以在C ++和C#中使用这种风格。当然,如果您使用标准库,它将不是100%FP。不幸的是,我不认为C#被优化用于函数式编程,它可能会产生很多性能问题。
This is more a subjective post, what I think about FP. It is not a rigorous declaration.
这是一个主观的帖子,我对FP的看法。这不是一个严格的声明。
#5
Functional languages are good in a lot of situations. It depends on the libraries of a particular functional language.
功能语言在很多情况下都很好。它取决于特定功能语言的库。
How would you answer the question "When to use an object oriented programming language?"?
您如何回答“何时使用面向对象编程语言?”的问题?
#6
Although this is quite the subjective question, I'd just like to add that functional languages are used a lot to parse domain specific languages; the functional nature lends well to parsing grammars.
虽然这是一个非常主观的问题,但我想补充的是,函数式语言用于解析特定于域的语言;功能性很好地解析语法。
#7
In general, use the language in which it's easiest to express the solution to a problem. For functional programming, this is when the solution to a problem is easily expressed in terms of functions, hence the name. Generally it's good for mathematical operations, AI, pattern matching; in general anything that can be broken down into a set of rules that must be applied to get an answer. You can only really determine the "best" language to use after you've analyzed your problem sufficiently. This is where pseudo-code comes in handy. If you find yourself writing pseudo-code that looks like FP, use FP.
通常,使用最容易表达问题解决方案的语言。对于函数式编程,这就是当问题的解决方案很容易用函数表达时,因此就是名称。通常它对数学运算,AI,模式匹配有好处;一般而言,任何可以分解为一组必须应用于获得答案的规则的东西。在充分分析问题后,您才能真正确定要使用的“最佳”语言。这就是伪代码派上用场的地方。如果您发现自己编写的FP代码看起来像FP,请使用FP。
Of course, all complete programming languages are functionally equivalent, so it doesn't matter really which one you choose in terms of what problems you can solve. The main effects will be in terms of coding efficiency and accuracy, and ease of maintenance.
当然,所有完整的编程语言在功能上都是等价的,因此根据您可以解决的问题确切选择哪一种。主要效果将是编码效率和准确性,以及易于维护。
Note also that it's possible to mimic FP within OO languages through cleverly designed APIs. For instance, I've seen numerous Java libraries (JMock is one example) that use method chaining to simulate an FP DSL. Then you'll see constructs such as:
另请注意,可以通过设计巧妙的API在OO语言中模仿FP。例如,我见过很多使用方法链来模拟FP DSL的Java库(JMock就是一个例子)。然后你会看到如下构造:
logger.expects(once()).method("error") .with( and(stringContains(action),stringContains(cause)) );
logger.expects(once())。method(“error”)。with(和(stringContains(action),stringContains(cause)));
This essentially builds a function that is evaluated to determine whether some calling sequence on a mock object is correct. (example stolen from http://www.jmock.org/yoga.html)
这实际上构建了一个函数,该函数被评估以确定模拟对象上的某些调用序列是否正确。 (例子来自http://www.jmock.org/yoga.html)
Another FP-like syntax in otherwise OO languages is the use of closures, such as in Ruby.
在其他OO语言中,另一种类似FP的语法是使用闭包,例如在Ruby中。
#8
From what I've seen, it's more a matter of taste than functionality (no pun intended). There really isn't anything about either style of language that makes it inherently better or worse at specific tasks.
从我所看到的,它更多的是品味而不是功能(没有双关语意)。在任何一种语言风格中都没有任何东西可以使它在特定任务中本身更好或更差。