Sponsored links: Algebra eBooks
 

Help Index

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

The Maxima on-line user's manual

Algebra Calculator

Search:

Lambda Calculator

Lambda

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

debugmode(true);
B:matrix([1,2,-8,14],[0,1,-44/7,72/7], [0,0,1,-5/3]);
A:matrix([1,2,-8,14],[2,4,-7,13],[5,3,4,-2]);
A[1]: lambda([i, j], A[i]:A[i]/A[i,i], A[j]: A[j]-A[i]*A[j,i])(1,2);
A;

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) 

Lambda Example

Related Examples

lambda-let

:lisp (defvar next);

:lisp (setf (symbol-v...

:lisp (setf (symbol-f...

Calculate

lambda-second

lambda([x], second(x)...

Calculate

lambda-makelist-map-numer

li [3] (1/7), numer;

li [-2] (3), numer;

L : makelist (i / 10...

Calculate

lambda-numer

mu: 3e-4;

f: 63;

L: 0.68;

Calculate

lambda-length-let

:lisp (list-all-packa...

:lisp (search-package...

Calculate

lambda-solve-tex

solve(x^2 = 4,x);

tex(solve(dT + 9*(c*(...

Calculate

lambda-listofvars-load-sqrt-subst-trace

listofvars(sqrt(1+sqr...

load(sqdnst);

a: sqrt((sqrt(3-2*sqr...

Calculate

lambda-numer

mu: 3e-4;

f: 63;

L: 0.68;

Calculate

lambda-load-sin-sqrt

load(fourie);

fourintcos(r*((1-cos(...

Calculate

lambda-numer

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

f(49, 0.01)*f(49, 0.1...

Calculate