Concept of function

Function - mapping between sets

  In the mathematics mapping which unambiguously displays one value on others is named as function. For example, record


puts in correspondence to each unit x from set of definition a unique unit y from a function range f. This correspondence also can be written as follows:

  f(x) → y

  Let's say, that function f from argument x matters y=f(x).
  Function can have any quantity of arguments, including them can not be absolutely. We will bring examples of functions:

abs(-3) → 3 number absolute value
+(2,3) → 5 the sum
union((a,b),(c,b)) → (a,b,c) association of sets
finnish(John) → false language definition
children(Adam,Eve) → (Kain,Avel) set of children

Type of arguments and functions

  Generally function can set mapping from several sets in a range. For the map of types of values of function and its arguments we will borrow one more denotation accepted in the mathematician. Let f - function from two arguments:

  f(x,y) → z  mapping of pairs units x, y in set z

  It is possible to designate types of arguments x and y , and also value type z as follows:

  f: A × B → C

  Here sign × between A and B designates a direct product of sets. The brought record means, that the type of the first argument of function f is A (i.e. it belongs to set A ), type of the second argument - B and type of value of function - C. Function f puts in correspondence to the arranged steams derivated from units of sets A and B , a unit from set C.
  Let's bring types of values and arguments of functions from the previous example:

+number × numbernumber
unionset × setset
finnishwordlogic value
childrenthe person × the personset of people

Definition and function call

  Function definition in language Lisp allows to set any computable mapping. For example, the function calculating the sum of squares, it is possible to define by means of addition and multiplication:

  arguments: x,y
  value: x*x+y*y

  Here x and y the variables representing any numbers.
  Function call means start, or application of definition of function to concrete values of arguments. For example, we will apply function thesumofsquares to arguments x=2 and y=3:

  thesumofsquares(2,3) → 13

The uniform infix notation

  In the mathematics and usual programming languages function call is written, as a rule, in so-called prefix record (notation) in which the function name faces to the brackets surrounding arguments:

  f(x), g(x,y), h(x,g(y,z)) , etc.

  In arithmetic expressions infix notation in which the function name or operations is allocated between arguments is used:

  x+y, x-y, x*(y+z) , etc.

  In the given implementation of language Lisp the infix form of record at which the object, the function name and arguments are written in brackets is accepted.

  (x F), (x G y), (x H (y G z)) , etc.

  Arithmetic operations are in the same way written. The arithmetic expressions brought above in lisp infix notation look so:

  (x + y), (x - y), (x * (y + z)) , etc.

  At first this way of record will seem inconvenient, but it rather fast becomes habitual and even it is pleasant as its advantages become obvious.
  Lisp contains as well the resources necessary for the definition of a way of record and the extension of language in a desirable direction. It is possible to use a traditional way of record or to think up to itself a new way. As all is made of lists conversion from one notation in another is simple enough. Some Lisp-systems allow to use without additional programming arithmetic operations without excessive brackets.
  The uniform and simple structure of function call is convenient both for the programmer, and for calculating value of expressions of interpreter Lispa. Calculating values of functions, it is not necessary to carry out difficult parse of expressions as at once on the first character of current expression the system already knows, with what structure deals and as with it to access.

Analogy between Lisp and a natural language

  In the mathematics one-letter characters are usually used. As it is not enough characters, began to use special characters, punctuation marks, indexes, the Greek characters and other ways of record. In Lisp (as well as in many other things programming languages) it is possible to name objects as their natural names. And as in usual language function call looks approximately the same as in Lisp at usage as names of functions and arguments of the ordinary words, turning out expressions start to remind natural language phrases. For example:

  (swedish translate (I know Swedish))

  Record of function call does not use such number of technical details and agreements as it is usually accepted in programming languages. If not to take into consideration used for selection of structures of a bracket and artificial association of several words from the point of view of record there is no special difference between Lisp and a natural language.

Dialogue with interpreter Lisp

  Lisp reminds a natural language and that it is usually used in dialogue (online) and in an interpretive mode. Interpreter Lispa functions as follows. When the user ends input of any function call or other expression the interpreter calculates and produces value of this expression. If we wish to calculate, for example, expression (2 + 3) we will enter it into the computer and at once we will receive result:

>(2 + 3)  ; the user enters expression
5  ; the interpreter calculates and produces result

  The character >, shown before entered expression, is a so-called prompt with which help the interpreter allows to know, that it has fulfilled calculation of the previous expression and new expression waits.

>(3 - 2)  ; subtraction
>  ; the interpreter produces the prompt on the screen and waits for input of the next expression

Hierarchy of calls

  The construction of entered function can include, in turn, functional subexpressions. Then arguments of calculated function replacing on the new calculations which are carrying on to definition of values of these arguments. Such by calculation which values as, for example constants, it is possible to define directly are reduced eventually to calculation of the expressions. We will give an example:

  ((1 + 2) * (3 + 4))

  The first task (1 + 2) is calculated at first and the result defines the object (in this case number), then the second unit of the list is interpreted as function defined for numbers, then this function calculates in a parallel way all remained arguments and is applied directly. If these arguments are nested function calls or other computable form before its calculation values of its arguments etc. are defined.
  At last, having omitted on the lowermost level, it is possible to start to substitute the values given by nested calculations as values of arguments of the previous level. Last value of all expression comes back. As well as in the mathematics, in Lisp the expressions bracketed first of all are calculated:

>((1 + 2) * (3 + 4))  ;(1+2)*(3+4)
>((1 + 2) * (1 * (2 + 3)))  ;(1+2)*(1*(2+3))

' locks expression calculation

  In some cases it is not necessary to calculate value of expression, and expression is necessary. Us, for example, can not interest value of functional call (2 + 3) , equal 5 , and we wish to handle the form (2 + 3) as the list. In such cases of expression which to the interpreter should not be calculated, are flagged in a special way. To prevent calculation of value of expression, it is necessary to put before this expression an apostrophe ' . In Lisp expression starting with an apostrophe corresponds to the conclusion in inverted commas in the ordinary text, and in both cases it means, that the citation needs to be used in that sort as it is. For example:

>'(2 + 3)
(2 + 3)

  Numbers should not be anticipated an apostrophe as the interpreter considers, that the number and its value coincide. However basically apostrophe usage before number is not forbidden. Before other constants (nil, true and false), as well as before numbers, too it is not necessary to put an apostrophe as also they represent themselves.
  However we will notice, that in the brought examples on a function name place in functional call the apostrophe was not used.

>317.0  ; the apostrophe is not necessary
>('2 + 3)
  ; value true is true
  ; the apostrophe is not necessary
  ; nil represents itself

  The presented interpreter does not admit usage as function of calculated expression.