“函数式编程”有明确的含义,但是“函数式语言”有明确的含义吗?

发布于 2024-08-22 06:18:53 字数 410 浏览 3 评论 0原文

我非常清楚函数式编程技术和命令式编程技术之间的区别。但是现在有一种普遍的趋势是谈论“函数式语言”,这确实让我感到困惑。

当然,像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

一念一轮回 2024-08-29 06:18:53

对于以学习编程语言为生的人来说,“函数式编程语言”是一个约束力相当弱的术语。有一个强烈的共识:

相当少数人还保留术语“函数式语言”来表示以下语言:

就像 Agda、Clean、Coq 和 Haskell 等语言一样。

除此之外,什么被认为是函数式编程语言通常取决于意图,即设计者是否希望它被称为“函数式”。

Perl 和 Smalltalk 是支持一流函数的语言示例,但其设计者并不将它们称为函数式。 Objective Caml 是一种被称为函数式语言的示例,尽管它具有带有继承和一切的完整对象系统。

被称为“函数式”的语言往往具有如下功能(摘自函数式编程的定义点):

  • 匿名函数(lambda 表达式)
  • 递归(由于纯粹性而更加突出)
  • 编程表达式而不是语句(再次强调,出于纯粹性)
  • 闭包
  • 柯里化 / 偏应用
  • 惰性评估
  • 代数数据类型模式匹配
  • 参数多态性(又名泛型

特定的编程语言越具有定制的语法和结构,以使上面列出的各种编程功能变得容易/轻松地使用快递&实施后,人们就越有可能将其标记为“函数式语言”。

Among people who study programming languages for a living, "functional programming language" is a pretty weakly bound term. There is a strong consensus that:

  • Any language that calls itself functional must support first-class, nested functions with lexical scoping rules.

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

  • Anonymous functions (lambda expressions)
  • Recursion (more prominent as a result of purity)
  • Programming with expressions rather than statements (again, from purity)
  • Closures
  • Currying / partial application
  • Lazy evaluation
  • Algebraic data types and pattern matching
  • Parametric polymorphism (a.k.a. generics)

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

另类 2024-08-29 06:18:53

我想说,函数式语言是任何允许函数式编程而不会造成过度痛苦的语言。

I would say that a functional language is any language that allows functional programming without undue pain.

凌乱心跳 2024-08-29 06:18:53

我喜欢@Randolpho 的回答。关于功能,我可能会在这里引用该列表:

函数式编程的定义点< /a>

  • 纯度(又名不变性、避免副作用、引用透明度
  • 高阶函数(例如,将函数作为参数传递,将其作为结果返回,将匿名函数动态定义为 lambda 表达式)
  • 惰性(又名非严格评估,最有用/与纯度结合使用时可用)
  • 代数数据类型模式匹配
  • 闭包
  • 柯里化/部分应用
  • 参数多态性(又名泛型
  • 递归(由于纯粹性而更加突出)
  • 使用表达式而不是语句进行编程(再次,出于纯粹性)

特定的编程语言越具有定制的语法和构造,以使上面列出的各种 FP 功能能够轻松/轻松地表达和构建。实施后,人们就越有可能将其标记为“函数式语言”。

I like @Randolpho's answer. With regards to features, I might cite the list here:

Defining point of functional programming

namely

  • Purity (a.k.a. immutability, eschewing side-effects, referential transparency)
  • Higher-order functions (e.g. pass a function as a parameter, return it as a result, define anonymous function on the fly as a lambda expression)
  • Laziness (a.k.a. non-strict evaluation, most useful/usable when coupled with purity)
  • Algebraic data types and pattern matching
  • Closures
  • Currying / partial application
  • Parametric polymorphism (a.k.a. generics)
  • Recursion (more prominent as a result of purity)
  • Programming with expressions rather than statements (again, from purity)

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

墨小墨 2024-08-29 06:18:53

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

野鹿林 2024-08-29 06:18:53

一种促进函数式编程作为充分利用该平台功能的手段的语言(和平台)。

A language (and platform) that promotes Functional Programming as a means of fully leveraging the capabilities of the said platform.

不爱素颜 2024-08-29 06:18:53

这种语言使得创建有副作用的函数比创建没有副作用的函数困难得多。对于可变/不可变数据结构也同样如此。

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.

绅士风度i 2024-08-29 06:18:53

我认为关于“OOP 语言”也可以提出同样的问题。毕竟,您可以用 C 语言编写面向对象的程序(而且这样做并不罕见)。但 C 没有任何内置语言结构来支持 OOP。您必须在没有编译器太多帮助的情况下“手动”执行 OOP。这就是为什么它通常不被视为 OOP 语言的原因。我认为这种区别也适用于“函数式语言”:例如,用 C++ 编写函数式代码并不罕见(想想像 std::count_ifstd 这样的 STL 函数: :变换)。但 C++(目前)缺乏支持函数式编程的内置语言功能,例如 lambda。 (为了论证,让我们忽略 boost::lambda 。)

因此,为了回答你的问题,我想说的是,尽管可以用这些语言中的每一种语言编写函数程序:

  • C 不是函数式语言(没有内置函数)函数式语言结构)
  • Scheme、Python 和类似语言都有函数式结构,因此它们是函数式语言。但它们也具有命令式和 OOP 结构,因此它们通常被称为“多范式”语言。

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 or std::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:

  • C is not a functional language (no built-in functional language constructs)
  • Scheme, Python and friends have functional constructs, so they're functional languages. But they also have imperative and OOP constructs, so they're usually referred to as "multi-paradigm" languages.
落花浅忆 2024-08-29 06:18:53

您可以使用任何语言进行函数式编程。我尽可能地尝试。

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.

不必在意 2024-08-29 06:18:53

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.

↘紸啶 2024-08-29 06:18:53

我曾经写过一篇(相当长的)分析,解释为什么“函数式编程语言”这个术语毫无意义,它也试图解释为什么 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文