### The Maxima on-line user's manual

Algebra Calculator

#### Lambda

Function: lambda ([<x_1>, ..., <x_m>], <expr_1>, ..., <expr_n>)

Function: lambda ([[<L>]], <expr_1>, ..., <expr_n>)

Function: lambda ([<x_1>, ..., <x_m>, [<L>]], <expr_1>, ..., <expr_n>) Defines and returns a lambda expression (that is, an anonymous function). The function may have required arguments <x_1>, ..., <x_m> and/or optional arguments <L>, which appear within the function body as a list. The return value of the function is <expr_n>. A lambda expression can be assigned to a variable and evaluated like an ordinary function. A lambda expression may appear in some contexts in which a function name is expected.

When the function is evaluated, unbound local variables <x_1>, ..., <x_m> are created. lambda may appear within block or another lambda; local variables are established each time another block or lambda is evaluated. Local variables appear to be global to any enclosed block or lambda. If a variable is not local, its value is the value most recently assigned in an enclosing block or lambda, if any, otherwise, it is the value of the variable in the global environment. This policy may coincide with the usual understanding of "dynamic scope".

After local variables are established, <expr_1> through <expr_n> are evaluated in turn. The special variable %%, representing the value of the preceding expression, is recognized. throw and catch may also appear in the list of expressions.

return cannot appear in a lambda expression unless enclosed by block, in which case return defines the return value of the block and not of the lambda expression, unless the block happens to be <expr_n>. Likewise, go cannot appear in a lambda expression unless enclosed by block.

lambda quotes its arguments; the quote-quote operator defeats quotation.

Examples:

* A lambda expression can be assigned to a variable and evaluated like an ordinary function.

(%i1) f: lambda ([x], x^2);
2
(%o1)                    lambda([x], x )
(%i2) f(a);
2
(%o2)                          a

* A lambda expression may appear in contexts in which a function evaluation is expected.

(%i3) lambda ([x], x^2) (a);
2
(%o3)                          a
(%i4) apply (lambda ([x], x^2), [a]);
2
(%o4)                          a
(%i5) map (lambda ([x], x^2), [a, b, c, d, e]);
2   2   2   2   2
(%o5)                 [a , b , c , d , e ]

* Argument variables are local variables. Other variables appear to be global variables. Global variables are evaluated at the time the lambda expression is evaluated, unless some special evaluation is forced by some means, such as .

(%i6) a: %pi\$
(%i7) b: %e\$
(%i8) g: lambda ([a], a*b);
(%o8)                   lambda([a], a b)
(%i9) b: %gamma\$
(%i10) g(1/2);
%gamma
(%o10)                       ------
2
(%i11) g2: lambda ([a], a*b);
(%o11)                lambda([a], a %gamma)
(%i12) b: %e\$
(%i13) g2(1/2);
%gamma
(%o13)                       ------
2

* Lambda expressions may be nested. Local variables within the outer lambda expression appear to be global to the inner expression unless masked by local variables of the same names.

(%i14) h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1
(%o14)    lambda([a, b], h2 : lambda([a], a b), h2(-))
2
(%i15) h(%pi, %gamma);
%gamma
(%o15)                       ------
2

* Since lambda quotes its arguments, lambda expression i below does not define a "multiply by a" function. Such a function can be defined via buildq, as in lambda expression i2 below.

(%i16) i: lambda ([a], lambda ([x], a*x));
(%o16)            lambda([a], lambda([x], a x))
(%i17) i(1/2);
(%o17)                  lambda([x], a x)
(%i18) i2: lambda([a], buildq([a: a], lambda([x], a*x)));
(%o18)    lambda([a], buildq([a : a], lambda([x], a x)))
(%i19) i2(1/2);
x
(%o19)                   lambda([x], -)
2
(%i20) i2(1/2)(%pi);
%pi
(%o20)                         ---
2

* A lambda expression may take a variable number of arguments, which are indicated by [<L>] as the sole or final argument. The arguments appear within the function body as a list.

(%i1) f : lambda ([aa, bb, [cc]], aa * cc + bb);
(%o1)          lambda([aa, bb, [cc]], aa cc + bb)
(%i2) f (foo, %i, 17, 29, 256);
(%o2)       [17 foo + %i, 29 foo + %i, 256 foo + %i]
(%i3) g : lambda ([[aa]], apply ("+", aa));
(%o3)             lambda([[aa]], apply(+, aa))
(%i4) g (17, 29, x, y, z, %e);
(%o4)                  z + y + x + %e + 46

There are also some inexact matches for lambda. Try ?? lambda to see them.

(%o1)                                true
(%i2)

### Related Examples

##### lambda-log-numer-sqrt

d:0.052716937029514;

g:9.81;

h:30;

Calculate

##### lambda

f: lambda([x], x^2);

f(2);

Calculate

##### lambda-ratsimp-sqrt-xthru

v:mu/sqrt(lambda)+a*l...

A:mu^2*pi^2/2+mu^2*v^...

xthru(A);

Calculate

##### lambda-makelist-map-setify

expr:n;

makelist(''expr, n, 1...

map(lambda([n],2^n/n)...

Calculate

##### lambda

f: lambda (2,3)+x=x*2;

Calculate

##### lambda-numer

f(t, lambda) := 3*%e^...

f(69, 0.01)*f(69, 0.0...

Calculate

log10(x):=log(x)/log(...

BW:20*1000*1000;

k:1.3806488*(10^-23);

Calculate

d:0.05;

p:400000;

g:9.81;

Calculate

##### lambda-map

mratcoeff(f,x,n) := (...

f:3*x^3*y+%e*y^2+%pi*...

mratcoeff(f,[x,y,z],[...

Calculate

##### lambda-print

a: 5;

f: lambda([x],x+a);

f(10);

Calculate