Version 0 of lambda

Updated 2004-02-13 23:42:12

Richard Suchenwirth 2002-02-14 - Lambda is the name for the letter "L" in the Greek alphabet. Since Alonzo Church's "Lambda calculus", it is also the term for anonymous functions. Consider

 f(x) = x+1

is not much different from

 g(x) = x+1

except for the f/g difference, which are both arbitrary names. The common essence of the above functions, ignoring the name, is just

 (x) = x+1

or in other words, "given x, return x+1". In Tcl, this looks like

 lambda x {expr {$x+1}}

and it's easy to implement - see Lambda in Tcl.

To have lambdas directly, one change to Tcl would be:

  • If a command name is a list of length three, with lambda as first element, bind its arguments to the second element of the list, and then evaluate the third element in that scope.

As long as we don't have this, a simple way is to make up a name for a proc:

 proc lambda {argl body} {
    set name [info level 0]
    proc $name $argl $body
    set name
 }

where the lambda definition is returned as command name.

See also RPN again for "half-lambdas": only the body is the value of a function, arguments are popped from the stack, the result is pushed on the stack.


Caregory Concept | Arts and crafts of Tcl-Tk programming