A I Lec# 2 Lisp Introduction
A I Lec# 2 Lisp Introduction
Email# [email protected]
Whatsapp# 03215551098
Important Instructions
These lectures will be included in the mid-term exam.
If any student has any query in this lecture must consult via whatsapp or email.
Assignments and Tasks will be given at the end of each Lecture notes.
1It is worth pointing out that, though Lisp was aimed at non-numeric computations, the current Common Lisp standard
is very strong in numeric computations.
List processing languages (LISP is an acronym for LISt Processing) have proved to
be singularly appropriate to programming for problem solving. Fundamental
techniques like generate and test, constraint propagation and so on have all been
developed within this computational framework. Indeed, there are certain kinds of
architectures which would be hard to implement in any other way, because they
require the manipulation of programs as if they were data2. Such architectures
seem to be essential for the design of intelligent systems. List processing languages
appear to provide the most elegant and economical way of doing this. Such languages
have also made modular and incremental programming far easier to do. Whether one
is considering techniques associated with production systems or with the quite
different approach of object oriented schemes, it is hard to see how these would be
implemented outside of the context of list processing.
We have used the terms “list processing languages” and Lisp interchangeably,
though it is worth pointing out that there were and are other symbolic or list
2This is another unique quality of Lisp that it allows no distinction between data and programs. That is to say, that data
items or lists in Lisp could may as well be programs, which can be executed or evaluated.
Prolog shares most of the Lisp‟s advanced features, such as, interactive environment,
programming flexibility, and garbage collection. But again, we need to stress, that if
one is going to learn a language, it might as well be one with a growing literature,
rather than a dead tongue or one which is just becoming popular. And it goes beyond
saying, that Lisp is the most popular language for AI programming.
S-Expressions
Syntactically, in Lisp there is only one construct - the S-expression. An
S-expression can either be an atom or a list. Examples of atomic (atoms)
S-expressions are:
ABC
12
456.9
X1
THIS_IS_A_LONG_ATOM
Historically, such items were known as “atoms”, but the terminology of Common
Lisp (or “Lisp” for short from here on) refers to these as symbols. Lists or
parenthesised S-expressions consist of a left-parenthesis, a sequence of zero or
more S-expressions, and a right-parenthesis. This recursive definition essentially
allows any mixture of atomic S-expressions and parentheses in which the parentheses
are properly matched. Examples of lists are:
(A)
(1 2 3 4)
()
(( (A) (B) ) C)
(((() () ())))
The empty list S-expression “()” is taken as the special atom NIL. This item can also
act as the truth value “false” (where as the truth value “true” is represented by all
All the three notations refer to the same unique internal Lisp item, but writing them in
different ways depending on what they are being used for can add clarity to a
program. The second one makes use of the extremely convenient quote symbol,
which will be dealt with later and which can be seen as an abbreviatory conventions.
Comments
Comments in Lisp are started with a ; character (i.e. semicolon). So a
statement of Lisp would be like:
(+ 5 10) ; sets the symbol a with value 10, this is a comment
#|
(setq a 10)
(setq b 20)
.... other code
|#
That is, a left-parenthesis, the function to be used (or called), and various values for
the parameters (separated by spaces), and a final right-parenthesis. For example, +
and * are built-in functions for performing addition and multiplication respectively:
Notice that the placing of the brackets is different from that used in other
languages, such as Pascal and C++, where the function is placed outside the
brackets, and the arguments are separated with commas.
> (+ 2 4) ;Lisp will first extract the definition of the function + and
6 ;then evaluate 2 and 4, which return 2 and 4, and then + is
> ;applied to 2 and 4.
> (* (+ 8 3) (+ 4 ( * 9 2 ) ) )
242
>
Symbols
No assignment operator
The names used to denote functions are not a separate class. They are just
Symbols! Like x, y, w, z
> (hello 2 3)
Error: Symbol has no function definition: HELLO
> (+ 2 3)
5
> (1+ 20) ;1+ function adds 1 to its argument
21
> (setq 1+ 30) ;setq assigns value 30 to symbol 1+
30
> 1+
30
> (1+ 25) ;1+ function adds 1 to 25
26
> (+ 5 1+) ;1+ symbol has value 30 so this S-exp adds 5 to it
Lists
(a b c) is a list of three elements, symbols or atoms a, b, c
(a (b c) d) is again a list of three elements: a, (b c), and d
Symbol b is not element of list. However, (b c) is
(((x))) is a list of one element: ((x))
((a b)) is again a list of one element.
Lisp is a List Processing language, and one of the fundamental tasks that is
performed in Lisp is managing/processing/manipulating lists. The Two most
common functions used for this purpose are car and cdr
Cons
Note that just like car and cdr, cons is also non destructive, i.e. cons does
not effect the values of symbols.
>(setq x ‘a)
A
> (setq y ‘(b c))
(B C)
> (cons x y)
(A B C)