first-class functions

Difference between version 7 and 8 - Previous - Next
In [functional programming%|%functional] and semi-functional [programming language%|%languages] including [Scheme], [Javascript], [Haskell], [ML], functions are '''[http://en.wikipedia.org/wiki/First-class_citizen%|%First Class Citizens]''', meaning that, like any other value, they can be passed as arguments to other functions.  By definition, they do not exist in languages such as [C], [Perl], and [Algol], where functions inhabit a different namespace than variables.  As with many things, [Tcl] is rather unique in this area.  [proc%|%commands] themselves can not be paussed as arguments toin other commands, but their names can, which provides something very similar to first-class functions.  Additionally, `[apply]` can be used to interpret a value as a function and evaluate it, providing the equivalent of [lambda] functions.

As a "first-class citizen", a function can be 

   * stored in variables
   * passed to functions
   * returned from functions
like any other value.  In other langauages, functions are usually [opaque objects], so [introspection] is not available.  In Tcl, they are simply [EIAS%|%strings]

First-class functions permit coding styles that are not available in languages without them ([C] is a good example):

   * [higher order functions], like [lmap] and other [fold]s
   * [static variables] become natural
   * [curry] becomes easy when functions are first class
   * [closures] become important with first-class functions



** See Also **

   [functional programming]:   
   [Playing Scheme]:   An exploratory implementation of first-class functions in Tcl.

<<categories>> Category Concept | Category Functional Programming