first-class functions

In functional and semi-functional languages including Scheme, Javascript, Haskell, ML, functions are 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. commands themselves can not be passed as arguments to 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 langauges, functions are usually opaque objects, so introspection is not available. In Tcl, they are simply strings

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

See Also

functional programming
Playing Scheme
An exploratory implementation of first-class functions in Tcl