Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... , a programming language
Programming language
A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer.... is said to support first-class functions (or function literal) if it treats functions as first-class object
First-class object
In computing, a first-class object , in the context of a particular programming language, is an entity which can be used in programs without restriction .... s. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions. This concept doesn't cover any means external to the language and program (metaprogramming
Metaprogramming
Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at runtime that would otherwise be done at compile time.... ), such as invoking a compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program.... or an eval
Eval
In some programming languages, eval is a subroutine which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval.... function to create a new function.
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data.... style, in which (for instance) the use of higher-order function
Higher-order function
In mathematics and computer science, higher-order functions or functional are function s which do at least one of the following:*take one or more functions as an input... s is a standard practice.
Discussion
Ask a question about 'First-class function'
Start a new discussion about 'First-class function'
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... , a programming language
Programming language
A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer.... is said to support first-class functions (or function literal) if it treats functions as first-class object
First-class object
In computing, a first-class object , in the context of a particular programming language, is an entity which can be used in programs without restriction .... s. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions. This concept doesn't cover any means external to the language and program (metaprogramming
Metaprogramming
Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at runtime that would otherwise be done at compile time.... ), such as invoking a compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program.... or an eval
Eval
In some programming languages, eval is a subroutine which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval.... function to create a new function.
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data.... style, in which (for instance) the use of higher-order function
Higher-order function
In mathematics and computer science, higher-order functions or functional are function s which do at least one of the following:*take one or more functions as an input... s is a standard practice. A simple example of a higher-ordered function is the map
Map (higher-order function)
In many programming languages, map is the name of a higher-order function that applies a Procedural parameter to a sequence of elements and returns a sequence of results.... or mapcar 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 and returning them as results. Historically, these were termed the funarg problem
Funarg problem
Funarg is an abbreviation for "functional argument"; in computer science, the funarg problem relates to a difficulty in implementing function s as first-class objects in stack-oriented programming language implementations.... s, the name coming from "function argument".
Availability
Languages which are strongly associated with functional programming, such as Lisp
Lisp programming language
Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older.... , Scheme, ML
ML programming language
ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM.... , and Haskell
Haskell (programming language)
Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:... , all support first-class functions. Other languages which also support them include Perl
Perl
In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language.... , Python
Python (programming language)
Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive.... , PHP
PHP
PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in Standalone software Graphical user interface.... , Lua
Lua programming language
In computing, Lua is a lightweight languages, Reflection , imperative programming and functional programming programming language, designed as a scripting language with extension language semantics as a primary goal.... , Tcl/Tk
Tcl
Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration"?according to the author?with programmers devising their own languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands.... , ECMAScript
ECMAScript
ECMAScript is a scripting language, standardized by Ecma International in the ECMA-262 Specification . The language is widely used on the World Wide Web, and is often confused with JavaScript or JScript, the two major Programming language dialect from which ECMAScript was standardized.... (JavaScript
JavaScript
JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard.... , ActionScript
ActionScript
ActionScript is a scripting language based on ECMAScript. ActionScript is used primarily for the development of websites and software using the Adobe Flash Player platform , but is also used in some database applications , and in basic robotics, as with the Make Controller Kit.... ), Ruby, Io, Scala
Scala programming language
Scala is a multi-paradigm programming language programming language designed to integrate features of object-oriented programming and functional programming.... , and Nemerle
Nemerle
Nemerle is a high level language static typing programming language for the Microsoft .NET platform. It offers functional programming, object-oriented and imperative programming features.... .
Most modern, natively compiled programming languages (e.g. C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... , C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... , C#, and Pascal) support function pointers, which can be stored in data structures and passed as arguments to other functions. Nevertheless, they are not considered to support first-class functions since, in general, functions cannot be created dynamically during the execution of a program without using reflection, which has an enormous performance impact. This could be mitigated by caching in some cases. The closest analog would be a dynamically compiled function created by a just-in-time compiler, which is compiled as an array of machine language instructions in memory and then cast to a function pointer, such as in C#. However, this technique is specific to the underlying managed framework architecture and is therefore not portable. But if using windows, C# has a delegate type which is a type safe function pointer. The C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... programming language supports user-defined operators, including the '' operator, which allows first-class objects to be treated as functions. Those objects can be manipulated just like any other first-class object in C++, but such manipulations do not include changing the function itself at runtime. Additionally, real Lambdas (see Lambda Calculus
Lambda calculus
In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion.... ) have no language support in the last C++ standard (although they are included in the working draft of C++0x
C++0x
C++0x is the planned new Open standard for the C++. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003.... ). C# supports lambdas in many uses, including anonymous methods. C# Version 3 adds a lamdba expression lexer and parser which run at compile-time and then a lambda code-generation portion of the compiler which executes at run-time.
The D programming language, also known simply as D, is an Object-oriented programming, Imperative programming, Multi-paradigm programming language system programming language by Walter Bright of Digital Mars....
alias real delegate(real x) Delegate;
Delegate makeDerivative(Delegate f, real deltaX)
void main
Scheme
(define square (lambda (x) (* x x))) ;assigning a function literal to a variable
(define fns (list + sin square)) ;functions as elements of a list
(define (compose f g) (lambda (x) (f (g x)))) ;a function that takes two functions as arguments and returns a composite function
((compose sqrt square) -3) ;function as return value of another function, applied to the argument -3
the result is: 3
(define fns-squared (map (lambda (fn) (compose square fn)) fns)) ;compose the list of function with the square function -> list of functions
(map (lambda(fn) (fn 3)) fns-squared) ;apply each function to the argument 3
the result is the list: (9 0.01991485667481699 81)
Lisp
(lambda (a b) (* a b)) ; function literal
((lambda (a b) (* a b)) 4 5) ; function is passed 4 and 5
ALGOL 68 is an imperative programming computer programming programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously defined syntax and semantics....
PROC newton = (REAL x, error, PROC (REAL) REAL f, f prime) REAL:
# Newton's method
Newton's method
In numerical analysis, Newton's method is perhaps the best known method for finding successively better approximations to the zeroes of a Real number-valued function .... #
IF f(x) <= error
THEN x
ELSE newton (x - f(x) / f prime (x), error, f, f prime)
FI;
ECMAScript is a scripting language, standardized by Ecma International in the ECMA-262 Specification . The language is widely used on the World Wide Web, and is often confused with JavaScript or JScript, the two major Programming language dialect from which ECMAScript was standardized....
function(message) // function literal
SomeObject.SomeCallBack=function(message) // function literal assigned to callback
Note that the callee keyword in ECMAScript makes it possible to write recursive functions without naming them.
Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive.... creates functions by the def statement or the lambda expression. Although lambda expressions cannot create functions that use statements in their definition as they are confined to be just expressions, they are flexible enough to show that they create first class functions as well as if the more powerful def statement were used:
>>> #some built in functions and their inverses
>>> from math import sin, cos, acos, asin
>>> # Add a user defined function and its inverse
>>> cube = lambda x: x * x * x
>>> croot = lambda x: x ** (1/3.0)
>>> # First class functions allow run-time creation of functions from functions
>>> # return function compose(f,g)(x)
f(g(x))
>>> compose = lambda f1, f2: ( lambda x: f1(f2(x)) )
>>> # first class functions should be able to be members of collection types
>>> funclist = [sin, cos, cube]
>>> funclisti = [asin, acos, croot]
>>> # Apply functions from lists as easily as integers
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)]
[0.5, 0.49999999999999989, 0.5]
>>>
Javascript
Javascript supports both first class functions and lexical scope.
// f: function that takes a number and returns a number
// deltaX: small positive number
// returns a function that is an approximate derivative of f
function makeDerivative( f, deltaX )
var cos = makeDerivative( Math.sin, 0.000001);
// cos(0) ~> 1
// cos(pi/2) ~> 0
C# is a multi-paradigm programming language that encompasses Functional programming, Imperative programming, Generic programming, Object-oriented programming , and Component-based software engineering programming disciplines....
C# supports anonymous functions and lambda expressions.
using System;
class Program
In computing, an anonymous function is a function defined, and possibly called, without being bound to a name. In lambda calculus, all functions are anonymous....
In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
A function object, also called a functor, functional or functionoid, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function , usually with the same syntax....
In computing, a first-class object , in the context of a particular programming language, is an entity which can be used in programs without restriction ....