“函数式编程”有明确的含义,但是“函数式语言”有明确的含义吗?
我非常清楚函数式编程技术和命令式编程技术之间的区别。但是现在有一种普遍的趋势是谈论“函数式语言”,这确实让我感到困惑。
当然,像 Haskell 这样的一些语言比 C 等其他语言更欢迎函数式编程。但即使是前者也可以进行 I/O(它只是将其保留在贫民窟中)。你可以用 C 语言编写函数式程序(这简直是太难了)。所以也许这只是程度问题。
尽管如此,即使从程度上来说,当有人称Scheme为“函数式语言”时,这意味着什么?我看到的大多数Scheme代码都是命令式的。如果您愿意的话,Scheme 是否可以让您轻松地以函数式风格进行编写? Lua 和 Python 也是如此。它们也是“函数式语言”吗?
我(真的)不想成为一名语言警察。如果这只是一种宽松的谈话方式,那也没关系。我只是想弄清楚它是否确实有一些我没有看到的明确含义(即使是程度问题)。
I understand very clearly the difference between functional and imperative programming techniques. But there's a widespread tendency to talk of "functional languages", and this really confuses me.
Of course some languages like Haskell are more hospitable to functional programming than other languages like C. But even the former does I/O (it just keeps it in a ghetto). And you can write functional programs in C (it's just absurdly harder). So maybe it's just a matter of degree.
Still, even as a matter of degree, what does it mean when someone calls Scheme a "functional language"? Most Scheme code I see is imperative. Is it just that Scheme makes it easy to write in a functional style if you want to? So too do Lua and Python. Are they "functional languages" too?
I'm (really) not trying to be a language cop. If this is just a loose way of talking, that's fine. I'm just trying to figure out whether it does have some definite meaning (even if it's a matter-of-degree meaning) that I'm not seeing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于以学习编程语言为生的人来说,“函数式编程语言”是一个约束力相当弱的术语。有一个强烈的共识:
相当少数人还保留术语“函数式语言”来表示以下语言:
就像 Agda、Clean、Coq 和 Haskell 等语言一样。
除此之外,什么被认为是函数式编程语言通常取决于意图,即设计者是否希望它被称为“函数式”。
Perl 和 Smalltalk 是支持一流函数的语言示例,但其设计者并不将它们称为函数式。 Objective Caml 是一种被称为函数式语言的示例,尽管它具有带有继承和一切的完整对象系统。
被称为“函数式”的语言往往具有如下功能(摘自函数式编程的定义点):
特定的编程语言越具有定制的语法和结构,以使上面列出的各种编程功能变得容易/轻松地使用快递&实施后,人们就越有可能将其标记为“函数式语言”。
Among people who study programming languages for a living, "functional programming language" is a pretty weakly bound term. There is a strong consensus that:
A significant minority also reserve the term "functional language" for languages which are:
as in languages like Agda, Clean, Coq, and Haskell.
Beyond that, what's considered a functional programming language is often a matter of intent, that is, whether is designers want it to be called "functional".
Perl and Smalltalk are examples of languages that support first-class functions but whose designers don't call them functional. Objective Caml is an example of a language that is called functional even though it has a full object system with inheritance and everything.
Languages that are called "functional" will tend to have features like the following (taken from Defining point of functional programming):
The more a particular programming language has syntax and constructs tailored to making the various programming features listed above easy/painless to express & implement, the more likely someone will label it a "functional language".
我想说,函数式语言是任何允许函数式编程而不会造成过度痛苦的语言。
I would say that a functional language is any language that allows functional programming without undue pain.
我喜欢@Randolpho 的回答。关于功能,我可能会在这里引用该列表:
函数式编程的定义点< /a>
即
特定的编程语言越具有定制的语法和构造,以使上面列出的各种 FP 功能能够轻松/轻松地表达和构建。实施后,人们就越有可能将其标记为“函数式语言”。
I like @Randolpho's answer. With regards to features, I might cite the list here:
Defining point of functional programming
namely
The more a particular programming language has syntax and constructs tailored to making the various FP features listed above easy/painless to express & implement, the more likely someone will label it a "functional language".
Jane Street 的 Brian Hurt 写了一篇非常好的文章不久前就此事。他得出的基本定义是,函数式编程语言是一种对 lambda 演算进行建模的语言。想想哪些语言被广泛认为是函数式的,您会发现这是一个非常实用的定义。
Lisp 是对 lambda 演算进行建模的原始尝试,因此它符合这个定义——尽管由于大多数实现并没有非常严格地遵循 lambda 演算的思想,因此它们通常被认为是混合范式或最多是弱函数。
这也是为什么很多人对像 Python 这样的语言被称为函数式语言感到愤怒。 Python 的一般哲学与 lambda 演算无关——它根本不鼓励这种思维方式——所以它不是一种函数式语言。这是一台具有一流功能的图灵机。是的,你可以用 Python 进行函数式编程,但是该语言的数学根源与函数式语言不同。 (顺便说一句,Guido van Rossum 本人同意这一点语言的描述。)
Jane Street's Brian Hurt wrote a very good article on this a while back. The basic definition he arrived at is that a functional programming language is a language that models the lambda calculus. Think about what languages are widely agreed to be functional and you'll see that this is a very practical definition.
Lisp was a primitive attempt to model the lambda calculus, so it fits this definition — though since most implementations don't stick very closely to the ideas of lambda calculus, they're generally considered to be mixed-paradigm or at best weakly functional.
This is also why a lot of people bristle at languages like Python being called functional. Python's general philosophy is unrelated to lambda calculus — it doesn't encourage this way of thinking at all — so it's not a functional language. It's a Turing machine with first-class functions. You can do functional-style programming in Python, yes, but the language does not have its roots in the same math that functional languages do. (Incidentally, Guido van Rossum himself agrees with this description of the language.)
一种促进函数式编程作为充分利用该平台功能的手段的语言(和平台)。
A language (and platform) that promotes Functional Programming as a means of fully leveraging the capabilities of the said platform.
这种语言使得创建有副作用的函数比创建没有副作用的函数困难得多。对于可变/不可变数据结构也同样如此。
A language that makes it a lot harder to create functions with side effects than without side effects. The same counts for mutable/immutable data structures.
我认为关于“OOP 语言”也可以提出同样的问题。毕竟,您可以用 C 语言编写面向对象的程序(而且这样做并不罕见)。但 C 没有任何内置语言结构来支持 OOP。您必须在没有编译器太多帮助的情况下“手动”执行 OOP。这就是为什么它通常不被视为 OOP 语言的原因。我认为这种区别也适用于“函数式语言”:例如,用 C++ 编写函数式代码并不罕见(想想像
std::count_if
或std 这样的 STL 函数: :变换
)。但 C++(目前)缺乏支持函数式编程的内置语言功能,例如 lambda。 (为了论证,让我们忽略 boost::lambda 。)因此,为了回答你的问题,我想说的是,尽管可以用这些语言中的每一种语言编写函数程序:
I think the same question can be asked about "OOP languages". After all, you can write object oriented programs in C (and it's not uncommon to do so). But C doesn't have any built-in language constructs to enable OOP. You have to do OOP "by hand" without much help from the compiler. That's why it's usually not considered an OOP language. I think this distinction can be applied to "functional languages", too: For example, it's not uncommon to write functional code in C++ (think about STL functions like
std::count_if
orstd::transform
). But C++ (for now) lacks built-in language features that enable functional programming, like lambdas. (Let's ignore boost::lambda for the sake of the argument.)So, to answer your question, I'd say although it's possible to write function programs in each of these languages:
您可以使用任何语言进行函数式编程。我尽可能地尝试。
Python、Linq都提倡函数式编程。
像 Haskell 这样的纯函数式语言要求您使用数学函数来完成所有计算,这些函数不会修改任何内容,只是返回值。
此外,函数式语言通常允许您编写高阶函数、将函数作为参数和/或返回类型的函数。
You can do functional style programming in any language. I try as much as possible.
Python, Linq all promote functional style programming.
A pure functional language like Haskell requires you to do all your computations using mathematical functions, functions that do not modify anything, they just return values.
In addition, functional languages typically allow you to write higher order functions, functions that take functions as arguments and/or return types.
Haskell 对于有副作用和没有副作用的函数有不同的类型。
作为 100% 函数式语言,这是一个相当好的区分属性,至少恕我直言。
Haskell for one have different types for functions with side-effects and those without.
That's a pretty good discriminating property for being a 100% functional language, at least IMHO.
我曾经写过一篇(相当长的)分析,解释为什么“函数式编程语言”这个术语毫无意义,它也试图解释为什么 Haskell 中的“函数”与 Lisp 或 Python 中的“函数”完全不同: http://blog.nihilarchitect.net/archives/289/on-function-programming/
像“map”或“filter”这样的东西在很大程度上也可以用 C 语言实现。
I wrote a (pretty long) analysis once on why the term 'functional programming language' is meaningless which also tries to explain why for instance 'functions' in Haskell are completely different from 'functions' in Lisp or Python: http://blog.nihilarchitect.net/archives/289/on-functional-programming/
Things like 'map' or 'filter' are for a large part also implementable in C for instance.