本文承接自上一篇《iOS的block与链式编程思想》
函数式编程
首先我们需要明白什么是函数式编程么?先说一点比较枯燥的概念,具体详细的定义可以详见维基百科,摘抄如下一段:
In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.
我是这样理解的:
在计算机科学中,函数式编程是一种编程范型,是一种构建程序的数据结构和元素的方法。它将计算机运算视为数学上的函数计算,并且避免使用程序状态以及易变对象。
维基百科还提到函数编程语言最重要的基础是λ演算(lambda calculus)。λ演算中最关键的要素就是函数被当作变量处理,能够参与运算。
概念说的有些云里雾里的,为了刨根问底,百科中还介绍了函数式编程的几个特性:
First-class and higher-order functions:这样拆成两部分:First-class functions 和 higher-order functions,点进去分别的定义如下。
First-class functions:
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.[1] Some programming language theorists require support for anonymous functions (function literals) as well.
函数为“第一等公民”。这意味着这个语言支持函数作为其他函数的参数,也可以作为函数的返回值,并且可以被赋值给变量或者在数据结构中存储。一些编程语言也支持匿名函数或者函数字面量。
(感觉这明显说的就是block啊!)
Higher-order function
In mathematics and computer science, a higher-order function (also functional, functional form or functor) is a function that does at least one of the following:
takes one or more functions as arguments (i.e., procedural parameters),
returns a function as its result.
看到这里有点懵逼,这两个定义好相似啊。。不怪我们英语水平问题,确实很相似:
*Higher-order functions are closely related to first-class functions in that higher-order functions and first-class functions both allow functions as arguments and results of other functions. *
The distinction between the two is subtle: "higher-order" describes a mathematical concept of functions that operate on other functions, while "first-class" is a computer science term that describes programming language entities that have no restriction on their use (thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values).
区别就是高阶函数描述的是数学概念上的函数操作,而一级函数是计算机科学中的专业名词。。
未完待续。。最近有点忙