为什么是函数式语言? [英] Why functional languages?

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

问题描述

我在这里看到很多关于函数式语言和其他东西的讨论.为什么要使用一种传统"语言?他们在哪些方面做得更好?他们不擅长什么?什么是理想的函数式编程应用程序?

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).

函数式编程的最大优点之一是无副作用函数的执行顺序并不重要.例如,在 Erlang 中,这用于以非常透明的方式启用并发.因为函数式语言中的函数的行为与数学函数非常相似,所以很容易将它们翻译成函数式语言.在某些情况下,这可以使代码更具可读性.

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.

传统上,函数式编程的一大缺点也是没有副作用.没有 IO 很难写出有用的软件,但是 IO 很难在函数中没有副作用的情况下实现.因此,大多数人从函数式编程中获得的收益从来没有比从单个输入计算单个输出更多.在 F# 或 Scala 等现代混合范式语言中,这更容易.

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.

许多现代语言都有函数式编程语言的元素.C# 3.0 有很多函数式编程特性,你也可以用 Python 进行函数式编程.我认为函数式编程流行的原因主要有两个原因:并发性正在成为普通编程中的一个真正问题,因为我们得到越来越多的多处理器计算机;并且语言变得越来越容易访问.

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 to be 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天全站免登陆