Content deleted Content added
Added {{unreferenced|date=January 2008}} to top |
Radagast83 (talk | contribs) from [[Function literal] |
||
Line 1:
{{unreferenced|date=January 2008}}
In [[computer science]], a [[programming language]] is said to support '''first-class functions''' (or '''function literal''') if it treats [[function (programming)|function]]s as [[first-class object]]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]]), such as invoking a [[compiler]] or an [[eval]] function to create a new function.
These features are a necessity for the [[functional programming]] style, in which (for instance) the use of [[higher-order function]]s is a standard practice. A simple example of a higher-ordered function is the ''[[Map (higher-order function)|map]]'' 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.
Line 12:
Most modern, natively compiled programming languages (e.g. [[C (programming language)|C]] and [[Pascal programming language|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. 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. However, this technique is specific to the underlying hardware architecture and is, therefore, neither general nor portable. The [[C++]] 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]]) have no language support in the last C++ standard (although there may be in [[C++0x]]).
== Examples ==
[[Lisp (programming language)|Lisp]]
(lambda (a b) (* a b)) ; function literal
((lambda (a b) (* a b)) 4 5) ; function is passed 4 and 5
[[ECMAScript]]
function(message) { print(message); } // function literal
SomeObject.SomeCallBack=function(message) { print(message); } // function literal assigned to callback
Note that the ''callee'' keyword in ECMAScript makes it possible to write recursive functions without naming them.
[[Python (programming language)|Python]]
Python does not have a (full) function literal, because lambda can only be followed an expression and not statements.
lambda x:x*x # function literal
map(lambda x:x*x,range(1:10)) # array of first ten square numbers
==See also==
|