0% found this document useful (0 votes)
46 views16 pages

A I Lec# 2 Lisp Introduction

This document provides instructions and an introduction to Common Lisp. It begins by listing important instructions for the lectures and assignments. It then provides a brief history of Lisp and discusses why it became a popular language for artificial intelligence. It also describes the syntax of Common Lisp, including S-expressions, upper and lower case conventions, and comments. Finally, it discusses programming in Common Lisp, including simple function calls and defining new functions.

Uploaded by

Shad Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views16 pages

A I Lec# 2 Lisp Introduction

This document provides instructions and an introduction to Common Lisp. It begins by listing important instructions for the lectures and assignments. It then provides a brief history of Lisp and discusses why it became a popular language for artificial intelligence. It also describes the syntax of Common Lisp, including S-expressions, upper and lower case conventions, and comments. Finally, it discusses programming in Common Lisp, including simple function calls and defining new functions.

Uploaded by

Shad Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Muhammad Asif Lecture# 2

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.

Introduction to Common Lisp


History about Lisp Language:
Lisp was originally developed in early 1960s by John McCarthy and his students at
MIT. It was one of the first languages to be aimed at non-numeric computation1
(i.e. handling symbols such as words, lists of symbols, and lists of lists), as opposed
to the numerically-oriented scientific and engineering languages such as FORTRAN.
It was also an early example of an interactive language, in which the user could type
in program statements for immediate execution (BASIC, Prolog), instead of having to
work through separate phases of editing, compiling and running (FORTRAN, Pascal,
C). The former aspect made it an obvious candidate for use as an AI language,
where arbitrary symbolic manipulations are needed, and the latter aspect led to a
particular style of program development, in which programs are put together, tried
out and debugged all within a single interactive environment.

If we address the following question, in Charniak‟s terms :


“Why is it that everyone uses Lisp, as opposed to, say, Pascal, or, heaven
forbid, FORTRAN? This is hard to explain until one has had more
experience with both AI and Lisp, but roughly speaking we can distinguish
two basic reasons. First, Lisp is much more flexible than most languages.
Users have such total control over what goes on that if they do not like the
syntax of the language, they may change it to suit themselves. Suppose
that you do not like the method for defining programs in, say, FORTRAN,

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.

-1- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
can you do anything about it? Short of switching to another language the
answer is clearly no. Indeed, the very idea seems absurd. But not to Lisp
users. The second reason for choosing Lisp is the way in which Lisp is
oriented toward the manipulation of symbols as opposed to, say,
numbers. Lisp gives us automatic facilities for associating information
with symbols... Also, Lisp has facility for easily constructing new data
structures and, should these data structures outlive their usefulness, taking
them apart again and using the storage space for something else. All of
this is without the programmer needing to think very much about what is
going on. This reclamation facility is called garbage collection.”
[Charniak 85]

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.

-2- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
processing languages, such as POP-2, POP-11, Prolog, ML. They have all been
used in AI to some extent, and Prolog being the most popular of them all. In fact, in
Europe and Japan, Prolog has been as popular as Lisp for AI work.

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.

-3- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098

Syntax in Common Lisp

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

-4- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
other forms of non-nil S-expression). So all the following forms of NIL refer to the
same S-expression (i.e. empty list), but suggest various notational conventions:

() is the empty list


‘NIL is the symbol named NIL
NIL is the truth-value “false”

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.

Upper and Lower Case


Lisp can sometimes seem a little eccentric in its handling of the case of
alphabetic symbols. The Steele standard attempts to define a simple consistent
approach, based on the idea that the internally stored form of everything is in
Upper-case. However, implementations may vary how exactly they let the user see
or refer to symbols. This can become very confusing. The best strategy to protect
your sanity is to:
• Never wRitE anyThiNg wHose mEANiNg dePends ON its CASE.
• Use quoted strings to refer to file-names or symbols with different cases, from
within Lisp, as the original case is preserved.

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

-5- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
Another way of placing long sections of comments is to enclose lines of text with #|
and |# characters. These are basically macro characters, which we will address later
on in the course. So a large section of code can be commented out by placing these
characters, such as:

#|
(setq a 10)
(setq b 20)
.... other code
|#

-6- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098

Programming in Common Lisp

As mentioned previously, the only construct in Lisp is an S-expression, which can


either be an atom or a list. The list S-expression is also used in defining and calling
the main programming construct the function (remember the similarity between the
data and the programs). The Lisp system has a vast number of built-in functions
(system functions), and the Lisp programmer can call these functions directly or can
incorporate them into definitions of new functions.

Simple Function Calls


The syntax (notation) for a function call (i.e. the application of a function to a
set of arguments) is very simple; Lisp statements are, in general, of the following
form:

(function-name <argument-1> ... <argument-n>)

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:

(+ 5 17) ; would evaluate to 22, and


(* 3 4) ; would evaluate to 12.

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.

Every Thing in Lisp is Evaluated

-7- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
As the title states, in Lisp every thing is evaluated. Lisp interpreter follows
two simple rules:
1) If the S-expression to be evaluated is an atom then its value is returned. For
example if we type in a numeral 7 to the Lisp interpreter‟s prompt, we will get
the value 7 back, since 7 evaluates to itself. On the other hand if we have a
symbol X, with a value 123 assigned to it, then that value will be returned.
> 7
7

2) If the S-expression to be evaluated is a list then it is assumed to be a function


call (or a macro call, which we will address later, but for the moment assume
they are similar to functions). The function definition is recalled from the first
item in the list (i.e. the function name), then all the arguments are evaluated,
and the function is applied to the evaluated arguments.

For example if we have:

> (+ 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.

> (+ X Y) ;If we have two symbols X and Y, with values 12 and 22


34 ;respectively, then for the S-expression (+ X Y) we will get
> ;34. In this case the function + was applied to the evaluated
;values of X and Y, which turned out to be 12 and 22.

> (* 8 ( + 3 7) ) ;evaluates 8 and (+ 3 7), then evaluates * with arguments


80 ;8 and 10, to produce the result 80
>

-8- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098

> (* (+ 8 3) (+ 4 ( * 9 2 ) ) )
242
>

Symbols

No assignment operator

> (setq x 10) ;in Lisp x = 10 is performed by setq


10
>x ;x evaluates to 10
10
> (+ x 8)
18
>x ;value of x remains same
10
> hello ;symbol hello being evaluated, has no value
Error: Variable has no value.

Setq does not evaluate First Argument! Special Function in Lisp


However, Second Argument is evaluated.

> (setq w 20)


20
> (setq z w) ;z is not evaluated, w is evaluated and returns 20
20 ;z is Assigned 20

> (+ 2 (setq x (* 3 4) ) ) ;x is not evaluated, 12 is assigned to x, then

-9- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
14 ;+ s-expression is evaluated and 2 is added to 12
>x ;x still has value 12
12

Function Names and Symbols

The names used to denote functions are not a separate class. They are just
Symbols! Like x, y, w, z

Symbols can have some or all of following


Name, a string of characters, such as “a”, “hello”
 Value
 Function
 Property Lists

> (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

-10- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
35

However, assigning values to known functions is generally bad


programming practice.
Exiting Lisp and effect on Symbols

> (setq x 20) ;assign x a value 20


20
>x ;x evaluates to its value
20
> (exit) ;exit from lisp environment (or session)
start new session of Lisp
>x ;previous session‟s symbols are deleted
Error: Variable has no value.

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.

> (a b c) ;Lisp tries to get function „a‟


Error: Symbol has no function definition: A

To stop Lisp to evaluate a List as an S-Expression, we use quote

> ( quote (a b c) ) ;Quote is a Special Function


(A B C) ;does not evaluate argument

-11- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
;returns argument as value
Quote is used so extensively in lisp, i.e. forcing lisp not to evaluate arg
That a special format is provided to quote

> ‘(a b c) ;Special format of quote fun


(A B C)

> ‘a ;symbol a has no value


a ;but quote returns a without
;evaluating
> (setq x ‘(a b c) )
(A B C) ;setq assigns (A B C) to x

> (setq x ‘(+ 3 4) ) ;setq assigns (+ 3 4) to x


(+ 3 4) ;without evaluating (+ 3 4)
Car and Cdr

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

car Returns first element of list; Current Address Register


cdr Returns rest of list; Current Data Register

> (car ‘(a b c) )


A
> (cdr ‘(a b c) )
(B C)

-12- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
> (setq x ‘(a b c)) ;car and cdr do not effect the symbol‟s values
(A B C) ;i.e. they are non-destructive
> (car x)
A
>(cdr x)
(B C)
>x
(A B C)

> (car (cdr ‘( a b) ))


B

> (car ‘((a b)) ) ;((a b)) is a list of one element (a b)


(A B)

> (car (car ‘((a b)) ))


A

> (cdr ‘((a b) (c d)))


((C D))

> (cdr ‘((a b) (c d) (e f)))


((C D) (E F))

> (cdr (car ‘((a b) (c d)) ) )


(B)
Combination of cars and cdrs is used so frequently that Lisp provides a
variety of combinations of cars and cdrs:

-13- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
caar same as (car (car „((a b)) ))
….
caaaar same as (car (car (car (car …. ))))
cadadr same as (car (cdr (car (cdr …. ))))
….
cddddr same as (cdr (cdr (cdr (cdr …. ))))

Hence the following two s-expressions return the same result

> (car (cdr (car (cdr ‘((a b c) (d e f)) ))))


E

> (cadadr ‘((a b c) (d e f)) )


E

The Empty List

> (cdr ‘(c) ) ; cdr of a one element list in an empty list


NIL

>’( ) ; empty list is same as NIL


NIL

>() ; empty list evaluates to NIL


NIL

Cons

To construct a list, the basic function used is cons

-14- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098

> (cons ‘a ‘(b c) ) ;cons concatenates first argument to second arg


( A B C)

> (cons ‘a ‘(b) )


(A B)
> (cons ‘a ‘( ) )
(A)
> (cons ‘(a b) ‘(c d) )
((A B) C D)

> (cons ‘a (cons ‘b ‘(c d) ) )


(A B C D)

> (setq x ‘(a b))


(A B)
> (cons (car x) (cons (cadr x) ‘(c d) )
(A B C D)

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)

-15- Artificial Intelligence


Muhammad Asif Lecture# 2
Email# [email protected]
Whatsapp# 03215551098
>x
A
>y
(B C)

List Construction Functions

> (cons ‘a (cons ‘b (cons ‘c nil)))


(A B C)
> (cons ‘a (cons (cons ‘b (cons ‘c nil)) (cons ‘d nil)))
(A (B C) D)

> (list ‘a ‘b ‘c) ;list makes list of all elements


(A B C)
> (list ‘a ‘(b c) ‘d)
(A (B C) D)

> (append ‘(a b) ‘( c d) ‘(e f)) ;append combines lists


(A B C D E F)
Task #1
 In this course we will be learning a Language used in A I is LISP language.
 First install the Lisp interpreter i.e. Allegro Common Lisp
 Take a snap of your desktop screen that you have installed successfully and send that
snap with your name and registration number on whatsapp group of A I Course.
st
 Last date for Task the submission of Task #1: 21 March 2020.
Assignment #1
Question: what is the role of artificial intelligence in today‟s modern era? And
write the names of some expert systems with explanation.
th
Submission date: 26 March 2020.

-16- Artificial Intelligence

You might also like