与面向对象编程相比,函数式编程最重要的哲学/概念是什么?

发布于 2025-01-10 08:42:24 字数 1433 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

姜生凉生 2025-01-17 08:42:24

OP 中为 OOP 列出的大多数特性也适用于 FP - 或许除了继承之外。

但即便如此,一些功能优先语言(例如F#)支持继承,而一些面向对象语言没有继承

对于 OOP 的具体构成,没有单一的、公认的定义,对于 FP 来说也是如此。我在我的文章功能架构:定义,其中我还认为 FP 的定义特征是引用透明度

FP 的许多其他众所周知的特性(不变性、高阶函数、闭包等)直接或间接源于对引用透明度的强调。一些典型的 FP 陷阱(例如 monad)也可以说源自引用透明性,但主要是在某种意义上,它们成功地解决了引用透明性引起的一些问题(例如,在执行复杂计算时保持状态,这可以由 State monad 处理)。

尽管如此,对于 FP 仍存在不同的看法。只需将 Clojure哈斯克尔。这些语言都被它们的社区认为是功能性的,但它们仍然非常不同。 OP 中建议的许多特征(类型类、代数数据类型)对于 Haskell 来说是典型的,但在 Clojure 中并不是真正的特征,因为 Clojure 是动态类型的。

另一方面,Clojure 强调典型的类似 Lisp 的属性,例如 同像性 和宏,及其社区拥抱动态类型。 (但是,阅读本文时,请注意我对 Haskell 的了解比对 Clojure 的了解要多。)

Most of the characteristics listed for OOP in the OP apply to FP as well - perhaps with the exception of inheritance.

But even so, some functional-first languages (e.g. F#) support inheritance, and some object-oriented languages don't have inheritance.

There's no single, accepted definition of exactly what constitutes OOP, and the same is true for FP. I discuss this in my article Functional architecture: a definition, where I also posit that the defining characteristic of FP is referential transparency.

Many of the other well-known traits of FP (immutability, higher-order functions, closures, etc.) stem directly or indirectly from the emphasis on referential transparency. Some typical FP trappings like monads could also be said to originate from referential transparency, but mostly in the sense that they are successful solutions to some of the problems that arise from referential transparency (e.g. hos to maintain state while performing a complex calculation, which can be addressed by the State monad).

Still, there are disparate takes on FP. Just compare Clojure to Haskell. These languages are both, by their communities, considered functional, and still, they are very different. Many of the characteristics suggested in the OP (type classes, algebraic data types) are typical for Haskell, while not really a thing in Clojure, because Clojure is dynamically typed.

Clojure, on the other hand, emphasises typical Lisp-like properties like homoiconicity and macros, and its community embrace dynamic typing. (Read this, however, with the caveat that I know Haskell much better than I know Clojure.)

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