Of what use is 'lambda'???

joswig at corporate-world.lisp.de joswig at corporate-world.lisp.de
Sun Sep 24 20:03:48 EDT 2000


In article <39CE4DB7.64BEFC1D at alcyone.com>,
  Erik Max Francis <max at alcyone.com> wrote:
> This is probably the main application of lambdas; it's a case where you
> need a throwaway function to use as glue but it doesn't need a symbol
> and you probably won't use it again.

One other applications of LAMBDA is that you can return them
from functions and create closures:

? (defun make-one-arg-function (function)
    (lambda (arg)
      (funcall function arg arg)))
MAKE-ONE-ARG-FUNCTION

? (mapcar (make-one-arg-function #'*)
          '(1 2 3 4))
(1 4 9 16)

? (setf (symbol-function 'square)
        (make-one-arg-function #'*))
#<COMPILED-LEXICAL-CLOSURE #x889D226>

? (square 3)
9

? (mapcar #'square
          '(1 2 3 4))
(1 4 9 16)

? (setf (symbol-function 'double)
        (make-one-arg-function #'+))
#<COMPILED-LEXICAL-CLOSURE #x889DD2E>

? (double 3)
6

? (mapcar #'double '(1 2 3 4 5))
(2 4 6 8 10)




Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list