A function is expressed by a lambda form which is merely a list whose first element is lambda. If a lambda form is defined for a symbol using defun, it can be referred as a global function name. Lambda form takes following syntax.
(lambda ({var}*
)}*]
) ((keyword var) [initform])}*
]
)}*])
{declaration}*
{form}*)
There is no function type such as EXPR, LEXPR, FEXPR, etc.: arguments to a function are always evaluated before its application, and the number of acceptable arguments is determined by lambda-list. Lambda-list specifies the sequence of parameters to the lambda form. Each of &optional, &rest, &key and &aux has special meaning in lambda-lists, and these symbols cannot be used as variable names. Supplied-p variables for &optional or &key parameters are not supported.
Since a lambda form is indistinguishable from normal list data, function special form must be used to inform the interpreter and compiler the form is intended to be a function. 1Function is also important to freeze the environment onto the function, so that all the lexical variables can be accessible in the function even the function is passed to another function of different lexical scope. The following program does not work either interpretedly nor after compiled, since sum from the let is invisible inside lambda form.
(let ((x '(1 2 3)) (sum 0)) (mapc '(lambda (x) (setq sum (+ sum x))) x))
To get the expected result, it should be written as follows:
(let ((x '(1 2 3)) (sum 0)) (mapc #'(lambda (x) (setq sum (+ sum x))) x ))
#' is the abbreviated notation of function, i.e. #'(lambda (x) x) is equivalent to (function (lambda (x) x)). Here is another example of what is called a funarg problem:
(defun mapvector (f v) (do ((i 0 (1+ i))) ((>= i (length v))) (funcall f (aref v i)))) (defun vector-sum (v) (let ((i 0)) (mapvector #'(lambda (x) (setq i (+ i x))) v) i)) (vector-sum #(1 2 3 4)) --> 10
EusLisp's closure cannot have indefinite extent: i.e. a closure can only survive as long as its outer extent is in effect. This means that a closure cannot be used for programming of ``generators". The following program does not work.
(proclaim '(special gen)) (let ((index 0)) (setq gen #'(lambda () (setq index (1+ index))))) (funcall gen)
However, the same purpose is accomplished by object oriented programming, because an object can hold its own static variables:
(defclass generator object (index)) (defmethod generator (:next () (setq index (1+ index))) (:init (&optional (start 0)) (setq index start) self)) (defvar gen (instance generator :init 0)) (send gen :next)k-okada 2013-05-21