Lisp is a programming language with the list oriented, parentheses - centric syntax.. This syntax, however, is quite powerful: Lisp pioneered tree data structures, automatic storage management, dynamic typing, self-hosting compiler and other revolutionary ideas.
Lisp makes no difference between statements and expressions; everything is a list. The list is space delimited and surrounded by parenthesis. If the list is a function call, then the first element is the function name and other elements are the function parameters, for instance
(times 3 2)
computes 3 * 2 = 6. Lists can be nested, for instance
(times (plus 1 3 2) (plus 4 5) )
computes ( 3 + 1 + 2) * ( 4 + 5 ) = 6 * 9 = 54.
The second example also shows that some functions (plus, times, max, etc) can accept three and more parameters as well. It is relatively easy to learn building quite complex expressions this way, and the notation is actually very easy for a machine to interpret and execute. Surely the "real world" Lisp also has much more standard capabilities as well as dialect-specific extensions. Most of the recent dialects allow to use arithmetic symbols like + - * / instead of words plus, minus, time and quotient, so (times 3 2) can be abbreviated to (* 3 2).
Evaluating expressions is not enough, need to build expression abstractions that could be reused in the future. Functions are defined with defun keyword:
(defun double (x) (* x 2))
In the above, we define a function named double, which returns twice the value of its input argument x. Now we can call it to compute double(7) = 14:
Functions can then be involved into expressions, calling other functions as well as the same function recursively, for instance double(double(1+1)) = 2*2*(1+1) = 8:
(double (double (+ 1 1)))
Many tasks require conditional construct. In List, it is similar to the C ternary operator: if (condition, ifyes, ifno), for instance
(if (> 3 2 ) 333 111 )
returns 333 as 3 is more than 2. To have if useful, it must be combined with recursion.
Using if, you can write your first useful function, factorial that is relatively easy to implement in Lisp:
(defun factorial (N) (if (= N 1) 1 (* N (factorial (- N 1)))))
Factorial is implemented through recursion. Factorial of N is computed using the following rules:
This is of course not all but should satisfy the requirement to give the brief introduction that the language is look like. The applet supports everything that is described so far or is highly similar. However it may not handle more complex constructs properly so after you have the idea, it is better to use standalone interpreter, like one from the . To continue, please follow the proposed references.
This article is not finished.