The functional programming paradigm was explicitly created to support a pure functional approach to problem solving. Functional programming is a form of declarative programming. In contrast, most mainstream languages, including object-oriented programming (OOP) languages such as C#, Visual Basic, C++, and Java –, were designed to primarily support imperative (procedural) programming.
With an imperative approach, a developer writes code that describes in exacting detail the steps that the computer must take to accomplish the goal. This is sometimes referred to as algorithmic programming. In contrast, a functional approach involves composing the problem as a set of functions to be executed. You define carefully the input to each function, and what each function returns. The following table describes some of the general differences between these two approaches.
Characteristic | Imperative approach | Functional approach |
|---|---|---|
Programmer focus | How to perform tasks (algorithms) and how to track changes in state. | What information is desired and what transformations are required. |
State changes | Important. | Non-existent. |
Order of execution | Important. | Low importance. |
Primary flow control | Loops, conditionals, and function (method) calls. | Function calls, including recursion. |
Primary manipulation unit | Instances of structures or classes. | Functions as first-class objects and data collections. |
Although most languages were designed to support a specific programming paradigm, many general languages are flexible enough to support multiple paradigms. For example, most languages that contain function pointers can be used to credibly support functional programming. Furthermore, in C# 3.0 and Visual Basic 9.0, explicit language extensions have been added to support functional programming, including lambda expressions and type inference. LINQ technology is a form of declarative, functional programming.
函数式编程简介
本文探讨了函数式编程范式及其与命令式编程的区别。重点介绍了函数式编程如何通过声明式方式解决问题,避免状态变化,强调数据转换而非指令执行流程。
1447

被折叠的 条评论
为什么被折叠?



