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.
函数式编程是一种声明式的编程范式,专注于定义需要什么信息以及需要进行何种转换,而非具体执行步骤。这种范式避免了状态变化的重要性,并侧重于将函数作为一等公民。
1466

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



