Node:lambda, Next:, Previous:Graphing words in defuns, Up:Print Whole Graph



C.4.3 A lambda Expression: Useful Anonymity

lambda is the symbol for an anonymous function, a function without a name. Every time you use an anonymous function, you need to include its whole body.

Thus,

(lambda (arg) (/ arg 50))

is a function definition that says `return the value resulting from dividing whatever is passed to me as arg by 50'.

Earlier, for example, we had a function multiply-by-seven; it multiplied its argument by 7. This function is similar, except it divides its argument by 50; and, it has no name. The anonymous equivalent of multiply-by-seven is:

(lambda (number) (* 7 number))

(See The defun Special Form.)

If we want to multiply 3 by 7, we can write:

(multiply-by-seven 3)
 \_______________/ ^
         |         |
      function  argument


This expression returns 21.

Similarly, we can write:

((lambda (number) (* 7 number)) 3)
 \____________________________/ ^
               |                |
      anonymous function     argument


If we want to divide 100 by 50, we can write:

((lambda (arg) (/ arg 50)) 100)
 \______________________/  \_/
             |              |
    anonymous function   argument


This expression returns 2. The 100 is passed to the function, which divides that number by 50.

See Lambda Expressions, for more about lambda. Lisp and lambda expressions derive from the Lambda Calculus.