Definition of functions

The lambda-expression represents parameterised calculation

  Definition of functions and their calculation in the Lisp is based on lambda-calculation Church, offering for this purpose an exact and simple formalism. The lambda-expression borrowed by the Lisp from a lambda-calculation is the important mechanism in practical programming.
  In a lambda-calculation of the Church function enters the name in a following kind:

  lambda(x1,x2,…,xn).fn

  In the Lisp the lambda-expression looks like:

  (LAMBDA (x1 x2 … xn) fn)

  The symbol LAMBDA means, that we deal with definition of function. Symbols xi are formal parameters of definition which call arguments in a body of function describing calculation fn . The list a part of the form formed of parameters, name a lambda-list.
  Body of function is the any form which value can calculate the interpreter of the Lisp, for example: a constant, the symbol connected with value or a composition from calls of functions. The function calculating the sum of squares of two numbers can be defined, for example, the following a lambda-expression:

  (lambda (x y) ((x * x) + (y * y))

  The formality of parameters means, that it is possible to replace them with any other symbols, and it will not be reflected in the calculations defined by function. It also disappears for a lambda-notation. With its help probably to distinguish concepts of definition and a call of function. For example, function LIST for two arguments can be defined any of two subsequent a lambda-expressions:

  (lambda (x y) (nil cons x (nil cons y nil)))
  (lambda (cat dog) (nil cons cat (nil cons dog nil)))

  Here value of a call of function CONS , being a body a lambda-expression, depends on values of communications (in other words, from values of variables).

The lambda-call corresponds to a call of function

  The lambda-expression is a definition of calculations and parameters of function in the pure state without actual parameters, or arguments. To apply such function to some arguments, it is necessary to put in a call of function a lambda-definition on a place of a name of function:

  (obj lambda-expression a1 a2 … an)

  Here ai - the forms setting actual parameters which are calculated as usually. For example, action of addition for numbers 2 and 3

>(2 + 3)
5

is possible to write down with use of a call a lambda-expression:

>(2  ; object
  (lambda (x) (this + x))  ; lambda-definition
  3)  ; argument
5  ; result

  The following call builds the list of arguments a and b :

>(nil (lambda (x y) (nil cons x (nil cons y nil)))
  'a 'b)
(a b)

  Such form of a call name a lambda-call.

Calculation a lambda-call, or a lambda-transformation

  Calculation a lambda-call, or application a lambda-expression to actual parameters, is made in two stages. First values of actual parameters are calculated and corresponding formal parameters contact the received values. This stage is called as linkage of parameters. At a following stage in view of new communications the form, being by a body a lambda-expression is calculated, and the received value comes back as value a lambda-call. To formal parameters after the termination of calculation those communications which at them, probably, were before calculation a lambda-call come back. All this process name a lambda-transformation.

Association a lambda-calls

  Lambda-calls it is possible to unite freely among themselves and other forms. Enclosed a lambda-calls it is possible to put both on a place of a body a lambda-expression, and on a place of actual parameters. For example, the lambda-expression contains a body in a following call enclosed a lambda-call:

>(nil (lambda (y)  ; at a lambda-call a body again a lambda-call
    (nil (lambda (x) (nil list y x)) 'internal)) 'external)
(external internal)

  In resulted below an example the lambda-call is argument of other call:

>(nil (lambda (x)  ; the lambda-call at which argument is new a lambda-call
    (nil list 'second x))
  (nil (lambda (y) (nil list y)) 'first))
(second (first))

  Pay attention, that the lambda-expression without arguments (actual parameters) represents only definition, but not the form which can be calculated. In itself it is not perceived by the interpreter:

>(lambda (x y) (nil cons x (nil cons y nil)))
Error:eval: Symbol lambda have no links

Lambda-expression - function without a name

  The lambda-expression is as cleanly abstract mechanism for definition and the description of the calculations, giving an exact formalism for parametrization of calculations by means of variables and the image of calculations, and the mechanism for linkage of formal and actual parameters for the period of performance of calculations. The lambda-expression is an anonymous function which vanishes immediately after calculation of value of the form. It is difficult for using again as it is impossible to cause by name though earlier expression was accessible as list object. However anonymous functions are used also, for example by transfer of function as argument of other function or at formation of function as a result of calculations, in other words, at synthesis of programs.

DEFMETHOD names the description of method

  The lambda-expression corresponds to definition of procedure used in another languages or function, and a lambda-call - to a call of procedure or function. Distinction consists that for the programmer the lambda-expression is only the mechanism, and it does not contain a name or something similar, allowing to refer to this expression from other calls. To write down calls of functions completely by means of a lambda-calls it is not reasonable, as very soon expressions in a call should be repeated, though different calls of one function differ only regarding actual parameters. The problem will be solved by the task of giving a name a lambda-expressions and uses in a call only a name.
  To name and define new function it is possible by means of function DEFMETHOD . Its action from the abstract point of view similarly task of a name of data ( SET and others). DEFMETHOD it is caused so:

  (classname DEFMETHOD name-function the-list-arguments body)

  That it is possible to imagine as reduction of record

  (classname DEFMETHOD name-function lambda-expression)

from which for convenience external brackets a lambda-expression and atom LAMBDA are excluded. For example:

  (nil defmethod list1 (lambda (x y) (nil cons x (nil cons y nil))))
  →
  (nil defmethod list1 (x y) (nil cons x (nil cons y nil)))

   DEFMETHOD connects a symbol with a lambda-expression, and the symbol starts to represent (to call) certain by it a lambda-expression of calculation. Value of this form is the lambda-expression of new function:

>(nil defmethod list1 (x y) (nil cons x (nil cons y nil)))  ; definition
(lambda (x y) (nil cons x (nil cons y nil)))
>(nil list1 'a 'b)
  ; call
(a b)

  Let's still more some examples:

>(nil defmethod lambdap (expression)  ; checks on a lambda-expression
  (nil eq (expression first) 'lambda))
(lambda (expression) (nil eq (expression first) (nil quote lambda)))
>(nil lambdap '(nil list1 'a 'b))
FALSE
>('number defmethod percent (part)
  ; calculates % of a part
  ((part / this) * 100))
(lambda (part) ((part / this) * 100)))
>(20 percent 4)
20

GETMETHOD gives out definition of a method

  Earlier the predicate BOUNDP , checking presence at a symbol of value has been considered. Accordingly function GETMETHOD returns definition of a method which can be perceived both in logic sense, and for a capture of its definition.

>(nil getmethod 'list1)
(lambda (x y) (nil cons x (nil cons y nil)))

  As definition of a method is set by the list, and he is always accessible to the program it is possible to investigate job of methods and even from time to time to modify it, changing definitions (for example, in problems of training). In the traditional programming languages assuming translation, it would be impossible. The symbol can simultaneously call some value and a method, and these opportunities do not stir each other. The position of a symbol in expression defines its interpretation:

>(nil setq list1 'a)
a
>(nil list1 list1 'b)
(a b)

  Symbols can in addition to value and definition to possess more generally and other properties certain by users, i.e. to possess the list of properties. Definition of a method and value of variables are only two various properties of variables which the programmer if necessary can to add or change by means of the list of properties.

Setting parameter in a lambda-list

  Considered earlier DEFMETHOD-form it is quite enough for studying the Lisp. However DEFMETHOD-form contains, besides the mechanism of keywords by means of which it is possible at desire to treat arguments of a call of a method differently.
  By means of a keyword in a lambda-list it is possible to allocate the parameter connected with a tail of the list of arguments of a changing length.
  Keywords begin with a sign &, and them write down before corresponding parameters in a lambda-list.

&RESTvariable quantity of arguments

  The argument specified after a keyword &REST, communicates with the list of the untied arguments specified in a call. Such functions can be passed variable quantity of arguments.
  For example:

>(nil defmethod fn (x &rest y)
  (nil list x y))
(lambda (x &rest y) (NIL list x y))
>(nil fn 'a)
(a nil)
>(nil fn 'a 'b 'c)
(a (b c))

Printing of functions in help managements

  The mechanism of keywords a lambda-list is applied not only to definition of functions, in directories and textbooks it use, explaining the use of arguments in descriptions of functions. For example, that fact, that function LIST has unlimited quantity of parameters enters the name in the form of

  (nil LIST &REST elements )

Functions calculate all arguments

  In the given realization there is no mechanism by means of which it would be possible to designate the parameters which are not demanding calculation. Blocking calculation of argument of a method and the form, such as quote, SETQ and DEFMETHOD , are defined through the mechanism of macroowls or macrooperators.