From the very beginning Lisp has been intended not for carrying out of operations with numbers, and for job with symbols and lists.
In Lisp for construction, analysis and the analysis of lists there are very simple base functions which in this language are primitives. In the certain sense they form system of axioms of language (algebra of processing of lists) to which symbolical calculations eventually are reduced.
Base functions of processing of lists can be compared to the basic actions in arithmetic calculations or in the theory of numbers.
Simplicity of base functions and their small number are characteristic features Lisp. Mathematical elegance of language is connected to it. Reasonably chosen primitives form, except for a beautiful formalism, as well a practical basis of programming.
Base functions of processing of symbolical expressions (atoms and lists) are:
FIRST, REST, CONS, ATOM and EQ
Functions by a principle of their use can be divided into functions of analysis, creation and check:
| USE | CALL | RESULT |
|---|---|---|
| Analysis: | (list FIRST) | s-expression |
| (list REST) | list | |
| Creation: | (NIL CONS first rest) | list |
| Check: | (NIL ATOM obj) | TRUE or FALSE |
| (NIL EQ x y) | TRUE or FALSE |
Function CONS and EQ has two arguments, for the others primitives - on one. As names of arguments and results of functions it is used names of the types describing arguments, on which it is determined (i.e. function and a kind of result returned by functions is meaningful). S-expression designates atom or the list.
Functions ATOM and EQ are base predicates. Predicates are functions which check performance of some condition and return as result FALSE or NIL in sense of logic denying, or other object in positive sense.
The first element of the list is called as a head, and the rest of the list, i.e. the list without its first element, called as a tail of the list. With the help of selectors FIRST and REST it is possible to allocate its head and a tail from the list.
Function FIRST returns the first element of the list, i.e. a head element of the list as value.
('(first second third) FIRST) → first ; the attention, is used '
Pay attention to that value of argument (first second third) is not calculated and operation FIRST is not applied to it, and the argument in such kind as it is is used. The situation differs, for example, from the following functional call:
>(4 * (3 + 2))
20
where it is necessary to increase 4 on value of argument (3+2) , i.e. on 5 , instead of on the list (3+2) . The call of function FIRST with argument (first second third) without an apostrophe would be interpreted as a call of function second with argument third , and the mistake would be received.
Function FIRST is meaningful only for the arguments being lists, and consequently, having a head:
FIRST: list → s-expression
For argument of atom the result of function FIRST is not determined, and thereof the result will be NIL . Head part of the empty list count for convenience NIL .
(NIL FIRST) → NIL ; head of the empty list - the empty list
('NIL FIRST) → NIL ; the sign ' can be omitted
('(NIL a) FIRST) → NIL ; head of the list NIL
Function REST is applicable to lists. Value of it will be a tail part, i.e. the list received from the initial list without a head element.
REST: list → list
('(a b c) REST) → (b c)
('(a (b c)) REST) → ((b c))
Function REST does not allocate the second element of the list, and takes all rest of the list, i.e. a tail. If the list will consist of one element, a tail will be the empty list () , i.e. NIL .
('(a) REST) → NIL
From reasons of convenience value of function REST from the empty list considers NIL :
(NIL REST) → NIL
('(1 2 3 4) FIRST) → 1
(('(1 2 3 4) REST) FIRST) → 2
(('(1 2 3 4) REST) REST) → (3 4)
('('(1 2 3 4) REST) REST) → (REST)
That it was possible to include the first argument of function CONS as the first element of value of the second argument of this function, the second argument should be the list. Value of function CONS always will be the list.
CONS: s-expression × list → list
Selectors FIRST and REST are the opposites for the function CONS . The list broken with the help of functions FIRST and REST on a head and a tail, it is possible to restore with the help of function CONS .
| ('(a b c) FIRST) | → a | |
| CONS → (a b c) | ||
| ('(a b c) REST) | → (b c) |
To carry out allowable actions with lists and to avoid erroneous situations, for us are necessary, except for getting and designing functions, means of the identification of expressions. The functions solving this task, in Lisp are called as predicates.
The predicate is a function which determines, whether the argument possesses the certain property and returns as value logic value "false", i.e. FALSE or NIL , or "true" which can be submitted by a symbol TRUE or any expression which is distinct from FALSE or NIL .
ATOM and EQ are base predicates Lisp. With their help and using other base functions, it is possible to set more complex predicates which will check presence of more complex properties.
Notwithstanding what usual processing of lists always can be reduced to three base functions described earlier ( CONS , FIRST and REST ) and to two base predicates ( ATOM and EQ ), programming only with their use would be very primitive and similar to programming on an internal computer language. Therefore the set of the built - in functions is switched on in Lisp-system for various actions and various situations.