为什么功能语言? [英] Why functional languages?

查看:141
本文介绍了为什么功能语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到很多关于函数式语言和东西的讨论。你为什么要用传统语言来使用它?他们做得更好些什么?他们更糟糕的是什么?什么是理想的函数式编程应用程序?

解决方案

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

执行无副作用函数的顺序并不重要。例如,在Erlang中,这用于以非常透明的方式启用并发。
因为函数式语言中的函数与数学函数非常相似,所以很容易将这些函数翻译成函数式语言。在某些情况下,这可以使代码更具可读性。传统上,函数式编程的一大缺点是缺乏副作用。在没有IO的情况下编写有用的软件是非常困难的,但IO很难在功能上没有副作用的情况下实现。所以大多数人从来没有比单一输入计算单个输出更多的功能编程。在现代混合范式语言(如F#或Scala)中,这更容易。



许多现代语言都包含函数式编程语言的元素。 C#3.0有很多功能性的编程功能,您也可以在Python中进行函数式编程。我认为功能性编程流行的原因主要是由于两个原因:并发在正常编程中遇到了一个实际问题,因为我们越来越多的多处理器计算机;并且语言越来越便于使用。


I see a lot of talk on here about functional languages and stuff. Why would you use one over a "traditional" language? What do they do better? What are they worse at? What's the ideal functional programming application?

解决方案

Functional languages use a different paradigm than imperative and object-oriented languages. They use side-effect-free functions as a basic building block in the language. This enables lots of things and makes a lot of things more difficult (or in most cases different from what people are used to).

One of the biggest advantages with functional programming is that the order of execution of side-effect-free functions is not important. For example, in Erlang this is used to enable concurrency in a very transparent way. And because functions in functional languages behave very similar to mathematical functions it's easy to translate those into functional languages. In some cases, this can make code more readable.

Traditionally, one of the big disadvantages of functional programming was also the lack of side effects. It's very difficult to write useful software without IO, but IO is hard to implement without side effects in functions. So most people never got more out of functional programming than calculating a single output from a single input. In modern mixed-paradigm languages like F# or Scala this is easier.

Lots of modern languages have elements from functional programming languages. C# 3.0 has a lot functional programming features and you can do functional programming in Python too. I think the reasons for the popularity of functional programming is mostly because of two reasons: Concurrency is getting a real problem in normal programming because we're getting more and more multiprocessor computers; and the languages are getting more accessible.

这篇关于为什么功能语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆