函数式、结构化和过程式编程之间有什么区别?

发布于 2024-10-07 10:57:47 字数 34 浏览 6 评论 0原文

我读过一些关于所有主题的文章,但我不太明白其中的区别。

I have read some articles on all subjects, but I don't quite get the differences.

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

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

发布评论

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

评论(3

伴我老 2024-10-14 10:57:47

结构化编程是一个古老的术语,我认为它包含函数式编程、过程式编程以及其他很多内容。它基本上意味着使用显式的控制流结构,而不是直接从一条指令跳转到另一条指令。

从这个意义上说,函数式编程和过程式编程都是结构化范例。函数式编程也是声明式编程——赋予代码的结构与其含义相对应——程序是改变世界状态的函数。过程式编程是您所认为的任何 C 语言或其后代(包括 Java 和 C++ 等面向对象语言)中的“典型”编程。程序是一系列指令,要串行执行,并沿途调用子过程。

或者,正如众所周知的那样:

函数式编程就像
向某人描述您的问题
数学家。命令式编程
就像给一个指令
白痴。

 --- arcus,Freenode 上的 #scheme

Structured programming is an old term that I think would encompass functional, procedural, and much else. It basically means using explicit control-flow structures rather than jumping about directly from instruction to instruction.

Functional and procedural programming are both, in that sense, structured paradigms. Functional programming is also declarative programming -- the structure given to your code corresponds to its meaning -- a program is a function that changes the state of the world. Procedural programming is what you'd consider "typical" programming in any C language or its descendants, including OO languages such as Java and C++. A program is a series of instructions, to be executed serially, and invoking subprocedures along the way.

Or, as it has been famously described:

Functional programming is like
describing your problem to a
mathematician. Imperative programming
is like giving instructions to an
idiot.

    --- arcus, #scheme on Freenode
撕心裂肺的伤痛 2024-10-14 10:57:47

函数式编程使用函数作为第一类元素。利用高阶函数(获取和/或返回函数);导致强大的构造和精心分解的代码。有些人还关注 FP 的纯度方面,也就是说,在给定相同的输入的情况下,函数应该始终返回相同的结果。我相信,这是 FP 的两个基本支柱。我还认为避免副作用本质上是从加载/存储机器级指令中抽象出一点。

结构化编程可以追溯到 Djikstra 的“Goto 被认为是有害的”论文。这意味着使用 if/then/else/elif 结构、do/while/until/for 循环等,而不是诉诸 goto。它本质上是从比较/分支机器级指令中抽象出来的。结构化编程与函数式编程和过程式编程都是正交的。

我认为过程式编程是指使用命令式“子例程”(而不是纯“函数”)进行编程,通常由一系列留下副作用的“语句”(而不是“表达式”)组成。

Functional programming is using functions as first-class elements. Making use of higher order functions (taking and/or returning functions); leading to powerful constructs and well factored code. Some people focus also on the purity aspect of FP which is to say that functions should always return the same result, given the same input. These, I believe, are the two basic pillars of FP. I also see avoiding side effects as essentially abstracting a bit away from the load/store machine level instructions.

Structured programming goes back to Djikstra's "Goto Considered Harmful" paper. It means using if/then/else/elif structures, do/while/until/for loops, etc. instead of resorting to goto. It's essentially abstracting a bit away from the compare/branch machine level instructions. Structured programming is orthogonal to both functional and procedural programming.

Procedural programming, I believe, refers to programming with imperative "subroutines" (as opposed to pure "functions") consisting generally of a series of "statements" (as opposed to "expressions") leaving behind side effects.

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