First-class function

https://en.wikipedia.org/wiki/First-class_function

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.[2] In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type.[3] The term was coined by Christopher Strachey in the context of "functions as first-class citizens" in the mid-1960s.[4]

First-class functions are a necessity for the functional programming style, in which the use of higher-order functions is a standard practice. A simple example of a higher-ordered function is the map function, which takes, as its arguments, a function and a list, and returns the list formed by applying the function to each member of the list. For a language to support map, it must support passing a function as an argument.

There are certain implementation difficulties in passing functions as arguments or returning them as results, especially in the presence of non-local variables introduced in nested and anonymous functions. Historically, these were termed the funarg problems, the name coming from "function argument".[5] In early imperative languages these problems were avoided by either not supporting functions as result types (e.g. ALGOL 60Pascal) or omitting nested functions and thus non-local variables (e.g. C). The early functional language Lisp took the approach of dynamic scoping, where non-local variables refer to the closest definition of that variable at the point where the function is executed, instead of where it was defined. Proper support for lexically scoped first-class functions was introduced in Scheme and requires handling references to functions as closures instead of bare function pointers,[4] which in turn makes garbage collection a necessity.

Contents

[hide]

Concepts[edit]

In this section we compare how particular programming idioms are handled in a functional language with first-class functions (Haskell) compared to an imperative language where functions are second-class citizens (C).

Higher-order functions: passing functions as arguments[edit]

Further information: Higher-order function

In languages where functions are first-class citizens, functions can be passed as arguments to other functions in the same way as other values (a function taking another function as argument is called a higher-order function). In the language Haskell:

map :: (a -> b) -> [a] -> [b]
map f []     = []
map f (x:xs) = f x : map f xs

Languages where functions are not first-class often still allow one to write higher-order functions through the use of features such as function pointers or delegates. In the language C:

void map(int (*f)(int), int x[], size_t n) {
    for (int i = 0; i < n; i++)
        x[i] = f(x[i]);
}

When comparing the two samples, one should note that there are a number of differences between the two approaches that are not directly related to the support of first-class functions. The Haskell sample operates on lists, while the C sample operates on arrays. Both are the most natural compound data structures in the respective languages and making the C sample operate on linked lists would have made it unnecessarily complex. This also accounts for the fact that the C function needs an additional parameter (giving the size of the array.) The C function updates the array in-place, returning no value, whereas in Haskell data structures are persistent (a new list is returned while the old is left intact.) The Haskell sample uses recursion to traverse the list, while the C sample uses iteration. Again, this is the most natural way to express this function in both languages, but the Haskell sample could easily have been expressed in terms of a fold and the C sample in terms of recursion. Finally, the Haskell function has a polymorphic type, as this is not supported by C we have fixed all type variables to the type constant int.

Anonymous and nested functions[edit]

Further information: Anonymous function and Nested function

In languages supporting anonymous functions, we can pass such a function as an argument to a higher-order function:

main = map (\x -> 3 * x + 1) [1, 2, 3, 4, 5]

In a language which does not support anonymous functions, we have to bind it to a name instead:

int f(int x) {
    return 3 * x + 1;
}

int main() {
    int list[] = {1, 2, 3, 4, 5};
    map(f, list, 5);
}

Non-local variables and closures[edit]

Further information: Non-local variable and Closure (computer science)

Once we have anonymous or nested functions, it becomes natural for them to refer to variables outside of their body (called non-local variables):

main = let a = 3
           b = 1
        in map (\x -> a * x + b) [1, 2, 3, 4, 5]

If functions are represented with bare function pointers, it is no longer obvious how we should pass the value outside of the function body to it. We instead have to manually build a closure and one can at this point no longer speak of "first-class" functions.

typedef struct {
    int (*f)(int, int, int);
    int *a;
    int *b;
} closure_t;

void map(closure_t *closure, int x[], size_t n) {
    for (int i = 0; i < n; ++i)
        x[i] = (*closure->f)(*closure->a, *closure->b, x[i]);
}

int f(int a, int b, int x) {
    return a * x + b;
}

void main() {
    int l[] = {1, 2, 3, 4, 5};
    int a = 3;
    int b = 1;
    closure_t closure = {f, &a, &b};
    map(&closure, l, 5);
}

Also note that the map is now specialized to functions referring to two ints outside of their environment. This can be set up more generally, but requires more boilerplate code. If f would have been a nested function we would still have run into the same problem and this is the reason they are not supported in C.[6]

Higher-order functions: returning functions as results[edit]

When returning a function, we are in fact returning its closure. In the C example any local variables captured by the closure will go out of scope once we return from the function that builds the closure. Forcing the closure at a later point will result in undefined behaviour, possibly corrupting the stack. This is known as the upwards funarg problem.

Assigning functions to variables[edit]

Assigning functions to variables and storing them inside (global) datastructures potentially suffers from the same difficulties as returning functions.

f :: [[Integer] -> [Integer]]
f = let a = 3
        b = 1
     in [map (\x -> a * x + b), map (\x -> b * x + a)]

Equality of functions[edit]

Further information: Function equality

As one can test most literals and values for equality, it is natural to ask whether a programming language can support testing functions for equality. On further inspection, this question appears more difficult and one has to distinguish between several types of function equality:[7]

Extensional equality
Two functions f and g are considered extensionally equal if they agree on their outputs for all inputs (?xf(x) = g(x)). Under this definition of equality, for example, any two implementations of a stable sorting algorithm, such as insertion sort and merge sort, would be considered equal. Deciding on extensional equality is undecidablein general and even for functions with finite domains often intractable. For this reason no programming language implements function equality as extensional equality.
Intensional equality
Under intensional equality, two functions f and g are considered equal if they have the same "internal structure". This kind of equality could be implemented in interpreted languages by comparing the source code of the function bodies (such as in Interpreted Lisp 1.5) or the object code in compiled languages. Intensional equality implies extensional equality (under the assumption that the functions do not depend on the value of the program counter.)
Reference equality
Given the impracticality of implementing extensional and intensional equality, most languages supporting testing functions for equality use reference equality. All functions or closures are assigned a unique identifier (usually the address of the function body or the closure) and equality is decided based on equality of the identifier. Two separately defined, but otherwise identical function definitions will be considered unequal. Referential equality implies intensional and extensional equality. Referential equality breaks referential transparency and is therefore not supported in pure languages, such as Haskell.

Type theory[edit]

Main article: Function type

In type theory, the type of functions accepting values of type A and returning values of type B may be written as A → B or BA. In the Curry-Howard correspondencefunction types are related to logical implication; lambda abstraction corresponds to discharging hypothetical assumptions and function application corresponds to the modus ponens inference rule. Besides the usual case of programming functions, type theory also uses first-class functions to model associative arrays and similar data structures.

In category-theoretical accounts of programming, the availability of first-class functions corresponds to the closed category assumption. For instance, the simply typed lambda calculus corresponds to the internal language of cartesian closed categories.

Language support[edit]

Functional programming languages, such as SchemeMLHaskellF#, and Scala, all have first-class functions. When Lisp, one of the earliest functional languages, was designed, not all aspects of first-class functions were then properly understood, resulting in functions being dynamically scoped. The later Common Lisp dialect does have lexically scoped first-class functions.

Many scripting languages, including PerlPythonPHPLuaTcl/Tk, JavaScript and Io, have first-class functions.

For imperative languages, a distinction has to be made between Algol and its descendants such as Pascal, the traditional C family, and the modern garbage-collected variants. The Algol family has allowed nested functions and higher-order taking function as arguments, but not higher-order functions that return functions as results (except Algol 68, which allows this). The reason for this was that it was not known how to deal with non-local variables if a nested-function was returned as a result (and Algol 68 produces runtime errors in such cases).

The C family allowed both passing functions as arguments and returning them as results, but avoided any problems by not supporting nested functions. (The gcc compiler allows them as an extension.) As the usefulness of returning functions primarily lies in the ability to return nested functions that have captured non-local variables, instead of top-level functions, these languages are generally not considered to have first-class functions.

Modern imperative languages often support garbage-collection making the implementation of first-class functions feasible. First-class function have often only been supported in later revisions of the language, including C# 2.0 and Apple‘s Blocks extension to C, C++ and Objective-C. C++11 has added support for anonymous functions and closures to the language, but because of the non-garbage collected nature of the language, special care has to be taken for non-local variables in functions to be returned as results (see below).

Language Higher-order functions Nested functions Non-local variables Notes
Arguments Results Named Anonymous Closures Partial application
Algol family ALGOL 60 Yes No Yes No Downwards No Have function types.
ALGOL 68 Yes Yes[8] Yes Yes Downwards[9] No
Pascal Yes No Yes No Downwards No
Ada Yes No Yes No Downwards No
Oberon Yes Non-nested only Yes No Downwards No
Delphi Yes Yes Yes 2009 2009 No
C family C Yes Yes No No No No Has function pointers.
C++ Yes Yes C++11[10] C++11[11] C++11[11] C++11 Has function pointers, function objects. (Also, see below.)

Explicit partial application possible with std::bind.

C# Yes Yes Using anonymous 2.0 / 3.0 2.0 3.0 Has delegates (2.0) and lambda expressions (3.0).
Objective-C Yes Yes Using anonymous 2.0 + Blocks[12] 2.0 + Blocks No Has function pointers.
Java Partial Partial Using anonymous Java 8 Java 8 No Has anonymous inner classes.
Go Yes Yes Using anonymous Yes Yes Yes[13]
Limbo Yes Yes Yes Yes Yes No
Newsqueak Yes Yes Yes Yes Yes No
Rust Yes Yes Yes Yes Yes No
Functional languages Lisp Syntax Syntax Yes Yes Common Lisp No (see below)
Scheme Yes Yes Yes Yes Yes SRFI 26[14]  
Clojure Yes Yes Yes Yes Yes Yes  
ML Yes Yes Yes Yes Yes Yes  
Haskell Yes Yes Yes Yes Yes Yes  
Scala Yes Yes Yes Yes Yes Yes  
Scripting languages JavaScript Yes Yes Yes Yes Yes ECMAScript 5 Partial application possible with user-land code on ES3 [15]
Lua Yes Yes Yes Yes Yes Yes[16]  
PHP Yes Yes Using anonymous 5.3 5.3 No Partial application possible with user-land code.
Perl Yes Yes 6 Yes Yes 6[17]  
Python Yes Yes Yes Expressions only Yes 2.5[18] (see below)
Ruby Syntax Syntax Unscoped Yes Yes 1.9 (see below)
Other languages Fortran Yes Yes Yes No No No  
Io Yes Yes Yes Yes Yes No
Maple Yes Yes Yes Yes Yes No  
Mathematica Yes Yes Yes Yes Yes No  
MATLAB Yes Yes Yes Yes[19] Yes Yes Partial application possible by automatic generation of new functions.[20]
Smalltalk Yes Yes Yes Yes Yes Partial Partial application possible through library.
Swift Yes Yes Yes Yes Yes Yes  
C++
C++11 closures can capture non-local variables by reference (without extending their lifetime), by copy construction or by move construction (the variable lives as long as the closure does). The former potentially avoids an expensive copy and allows to modify the original variable but is unsafe in case the closure is returned (see dangling references). The second is safe if the closure is returned but requires a copy and cannot be used to modify the original variable (which might not exist any more at the time the closure is called). The latter is safe if the closure is returned and does not require a copy but cannot be used to modify the original variable either.
Java
Java 8 closures can only capture final or "effectively final" non-local variables. Java‘s function types are represented as Classes. Anonymous functions take the type inferred from the context. Method references are limited. For more details, see Anonymous Functions: Java Limitations
Lisp
Lexically scoped Lisp variants support closures. Dynamically scoped variants do not support closures or need a special construct to create closures.[21]
In Common Lisp, the identifier of a function in the function namespace cannot be used as a reference to a first-class value. The special operator function must be used to retrieve the function as a value: (function foo) evaluates to a function object. #‘foo exists as a shorthand notation. To apply such a function object, one must use the funcall function: (funcall #‘foo bar baz).
Python
Explicit partial application with functools.partial since version 2.5, and operator.methodcaller since version 2.6.
Ruby
The identifier of a regular "function" in Ruby (which is really a method) cannot be used as a value or passed. It must first be retrieved into a Method or Proc object to be used as first-class data. The syntax for calling such a function object differs from calling regular methods.
Nested method definitions do not actually nest the scope.
Explicit currying with [1].
时间: 2024-08-30 03:45:14

First-class function的相关文章

通过百度echarts实现数据图表展示功能

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa

帮同学做的大一大作业:《我的家乡—郑州》

---恢复内容开始--- 最近在上海上学的一个高中同学让我帮忙,帮她做她们的计算机课程大作业. 由于关系不错我也不好意思拒绝就帮忙做了,因为这个学期刚刚开始接触HTML5和css,所以制作过程中有很多不懂的,而且由于HTML5是选修课,一星期只有一节,所以做这个花费了比较多的时间,这个网站是我制作的第一个网站,比较有纪念意义,所以发在博客上,作为纪念. 通过去做这个作业,我了解到很多课上学不到的东西.因为没有美工,从头到尾,都是我一个人在臆想,刚开始的时候,根本无从下手,我去参考别人做的家乡网站

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

问题一: microsoft visual studio c++ 10.0 is required 安装scrapy时候出现需要vc c++ 10,有时安装其他也会有. 解决方法:安装vc 2010,安装过2017无效,安装过程也不一样. 问题二: 安装好,出现Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed? 解决办法: 1.pip install wheel 2. 到http://

[c++] Inline Function

The inline functions are a C++ enhancement feature to increase the execution time of a program. Compiler replace the definition at compile time instead of referring function defination at runtime. NOTE - This is a suggestion to compiler to make the f

js instanceof Object Function

Object.Function是javascript中顶级的两个对象,同时也属于两个顶级的构造器,function Object(){}.function Function(){}.Object.Function为两个独立的预先创建的两个对象.new Object创建一个具有Object特性的新的一个对象,new Function创建一个具有Function特性的一个新对象. Object是一个对象,包含__proto__.prototype属性. Object.__proto__ = func

different between method and function

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Arial; color: #242729 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Arial; color: #242729; background-color: #ffffff } span.s1 { background-color: #ffffff } span.s2 { } A method is on an o

qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method

最近在做一个网络音乐播放器时,由于出现qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method, 而不能播放网络歌曲. 上网搜了半天,都说要在电脑那里安装openssl,然后把C:\OpenSSL-Win64\bin下的libeay32.dll和ssleay32.dll拷贝到D:\Qt\Qt5.4.2\5.4\mingw491_32\bin, 然而并没什么卵用! 我的解决办法是: Qt的这个目

JavaScript中Function的拓展

Function 是什么东西,就是JavaScript中的顶级类,系统级别的类.我们平时写的函数方法例如下. function Animal() { } Animal就是Function的实例,但是在我们的逻辑中 Animal是类,是自定义类. Function是类,Animal是类也是实例,Animal是Function的实例,Animal是自定义类.这点大家一定要搞清楚. 我们在顶级类上定义一个method的方法,用于进行键值对的方式进行方法链式的设定, Function.prototype

第21讲: 偏函数(Partially applied function)实战详解

偏函数,也叫部分应用函数,就是调用的时候,只传入函数的部分参数.先举个例子就很容易明白了. object PartialAppliedFunction {   def main(args: Array[String]): Unit = {     val part_sum = sum(1,_:Int,3)     println(part_sum(2))   }      def sum(a:Int,b:Int,c:Int)=a+b+c } 我们定义了一个函数sum,共有3个参数. 我们又定义了