1
Contents
LISP and OtherAI programming Language
Introduction to LISP
LISP: Syntax and Numeric Functions
List Manipulation function in LISP
2.
August 16, 20252
Introduction to LISP
LISP (LISt processing) is invented by Jhon McCarthy
during the late 1950.
The basic building blocks of LISP are the atom, list,
and the string.
An atom is a number of string of contiguous
characters, including numbers and special characters
A List is a sequence of atoms and/or other lists
enclosed within parentheses.
String is a group of characters enclosed in double
quotation marks.
3.
August 16, 20253
Example of Atom, List & String
Atom: sat, week_day
List: (mon, tue, wed, thur, fri, sat, sun)
String: “please enter your name”
Atom, List and String are the only valid objects in LISP
and they are called Symbolic expression.
Any s-expression is potentially a valid program.
4.
August 16, 20254
Introduction: LISP
LISP programs run either on an interpreter or as
compiled code.
The interpreter examines source programs in a
repeated loop, called read-evaluate-print loop.
For example: sum of 3 numbers
-> (+ 5 6 9)
20
->
LISP uses prefix notation.
5.
August 16, 20255
Example of LISP
Convert Fahrenheit to Centigrade
C/5=(F-32)/9
or, F=C(9/5)+32
For C=50,
LISP program
->(+(*(/ 9 5)50)32)
122
->
Numerical functions: +, -, *, /
6.
August 16, 20256
Basic List Manipulation Functions
Function call Value
returned
Remarks
(car `(a b c)) a Car takes one argument, a list, and returns
the first element.
(cdr `(a b c)) (b c) Cdr takes one argument, a list, and returns
a list with the first element removed.
(cons `a`(b c)) (a b c) Cons takes two arguments, an element and
a list and returns a list with the element
inserted at the beginning.
(list `a`(b c)) (a (b c)) List takes any number arguments and
returns a list with the arguments as
elements.
7.
August 16, 20257
Basic List Manipulation Functions
Function call Value
returned
Remarks
(append `(a) `(b c)) (a b c) Merge two or more lists into a single list
(last `(a b c d)) (d) return a list containing the last element.
(member `b `( a b d)) (b d) Returns remainder of second argument list
starting with element matching first
argument.
(reverse `(a (b c) d)) (d (b c) a) returns a list with top elements in reverse
order.
8.
August 16, 20258
Example of (setq()) function
->(setq x `(a b c))
->x
A B C
->`x
X
->(setq x (+ 3 5))
8
9.
August 16, 20259
How to Define Function?
(defun name (parm1 parm2 …) body)
Example:
->(defun avgthree (n1 n2 n3) (/(+n1 n2 n3) 3))
AVGTHREE
->
Note: its return the function name. If you call this
function it will return the result.
10.
August 16, 202510
The Most Common Predicate calls
Function call Value
returned
Remarks
(atom `aabb) t aabb is a valid atom
(equal `a (car `(a b)) t
(evenp 3) nil
(oddp 3) t
(numberp 10ab) nil
(greaterp 2 4 27) t Arguments are successively larger
(lessp 5 3 12) nil Arguments are not successively
smaller
(listp `(a)) t Valid list
(zerop .0001) nil
(null nil) t Nil is an empty set
11.
August 16, 202511
Conditional Statement
The syntax for condition is,
cond (<test1> <action1>)
(<test2> <action2>)
. .
. .
. .
(<testk>
<actionk>))
Example:
->(defun maxthree(a b c)
(cond ((>a b) (cond ((>a c) a)
(t c)))
((> b c) b)
(t c)))
MAXTHREE
->
->(maxthree 20 30 10)
30
->
12.
August 16, 202512
Input, Output Variables
read: ->(+ 5 (read))
6
11
->
print: ->(print`(a b c))
(A B C)
(A B C)
->(print
“hello”)
“hello”
“hello”
prinl: print without new line
princ: remove double quotation marks
terpri: it does not take arguments
It introduces a new line whenever it
appears and then returns nil.
->(defun circle-area()
(terpri)
(princ “Please enter the radius:”)
(setq radius (read))
(princ “ The area of the circle is:”)
(princ (* 3.1416 radius radius))
(terpri))
CIRCLE-AREA
13.
August 16, 202513
Input, Output Variables
->(defun circle-area()
(terpri)
(princ “Please enter the radius:”)
(setq radius (read))
(princ “ The area of the circle is:”)
(princ (* 3.1416 radius radius))
(terpri))
CIRCLE-AREA
->(circle-area)
Please enter the radius:
4
The area of the circle is:
50.2656
->
14.
August 16, 202514
Iteration
-> (defun factorial(n)
(do (count n (-count 1))
(product n (* product (-count 1))
((equal 0 count) product)))
FACTORIAL
->
15.
August 16, 202515
Property Lists and Arrays
One of the unique and most useful features
of LISP as an AI language is the ability to
assign properties to atoms.
Example: properties of a car are make, year,
color, style, etc.
16.
August 16, 202516
Property Lists and Arrays
The function putprop assign properties to an atom or
objects
(purtop object value attribute)
->(putprop `car `toyota `make)
TOYOTA
->(putprop `car 1998 `year)
1998
->(putprop `car `red `color)
RED
->(putprop `car `four-door `style)
FOUR-DOOR
17.
August 16, 202517
Property Lists and Arrays
remprop: remove properties
get: retrieve property
Example:
-> (get `car `color)
RED
-> (remprop `car `color)
RED
->(get `car `color)
Nil
->
August 16, 202519
PROLOG
PROLOG (PROgramming in LOGic)
Invented by Alain Colmerauer, 1970.
Programming in PROLOG is accomplished by
creating a data base of facts and rules about objects,
their properties, and their relationships to other
objects.
20.
PROLOG
PROLOG meansPROgramming in
LOGic
– The programmer uses the system to
draw inferences from facts and rules
PROLOG is
– declarative - specify facts and logical
relationships
– symbolic - symbols are used to
represent objects
– high level - contains a built-in problem
solving mechanism
PROLOG Programs
– solve problems by declaring objects
and their relationships
21.
PROLOG Paradigm
PROLOG
database
Queries
Facts Rule
s
•The PROLOG Programmer
– Loads facts and rules into the database.
– Makes queries to the database to see if a
fact is:
• in the database or
• can be implied from the facts and rules
therein
Facts:
isa(dog, mammal).
isa = predicate
(dog, mammal) = argument
2 = binary
22.
PROLOG Paradigm …
…
PROLOG
database
Queries
FactsRule
s
Predicates (clauses)
In this example, both “isa” and
“animal” are examples of predicates.
Predicates are used to indicate
relations between objects and hence
can represent FACTS and RULES.
Two ways to use predicates in a
query:
– 1. As a TRUE/FALSE test:
?- isa(dog, mammal).
yes
– 2. For DATA RETRIEVAL
?- isa(dog, X).
X = mammal
23.
• Constants
– Atoms
•Alphanumeric atoms - alphabetic
character sequence starting with a lower
case letter
Examples: apple a1 apple_cart
• Quoted atoms - sequence of characters
surrounded by single quotes
Examples: ‘Apple’ ‘hello world’
• Symbolic atoms - sequence of symbolic
characters
Examples: & < > * - + >>
• Special atoms
Examples: ! ; [ ] {}
– Numbers
• Integers and Floating Point numbers
PROLOG syntax
24.
PROLOG syntax ……
• Variable Names
A sequence of alphanumeric characters beginning with an
upper case letter or an underscore
Examples: Anything _var X
• Compound Terms (structures)
– an atom followed by an argument list containing terms. The
arguments are enclosed within brackets and separated by
commas
Example: isa(dog, mammal)
25.
System Interaction
Reminder:
Problem solvingin PROLOG
– 1. insert facts and rules into the database
– 2. ask questions (queries) based on the contents of the database
• Facts
– Used to represent unchanging information about objects and
their relationships.
– Only facts in the PROLOG database can be used for problem
solving.
– Insert facts into the database by,
• typing the facts into a file and loading (consulting) the file
into a running PROLOG system
26.
• Queries
– Retrieveinformation from the database by entering
QUERIES
– A query,
• is a pattern that PROLOG is asked to match against
the database
• has the syntax of a compound query
• may contain variables
– A query will cause PROLOG to
• look at the database
• try to find a match for the query pattern
• execute the body of the matching head
• return an answer
System Interaction … …
27.
System Interaction ……
Example:
Assume that the database contains:
– likes(hasan, rita).
– likes(belal, rita).
– likes(mohsin, beli).
The actual system interaction looks
like
?- likes(hasan, rita).
yes
?- likes(mohsin, rita).
no
?- likes(mohsin, X).
X = beli;
28.
Variables in Prolog
Reminder:a variable starts with a
capital letter or underscore.
When a variable is used, PROLOG tries
to find a match (instantiation) for it.
?-likes(Who, rita).
Who = hasan;
Who = belal;
no
29.
Simple
Backtracking
PROLOG searchesits database attempting to satisfy
a query (goal), stopping at the first success or
returning no for failure.
Often, there will be more than one successful
match and the programmer would like to tell the
PROLOG system to try again and search for other
successful matches.
When PROLOG retries a goal, it is called
backtracking.
Backtracking may be forced by typing a ;
30.
Examples: Backtracking
?- likes(Who,rita).
Who = hasan ;
Who = belal ;
no
?- likes(Who, Whoelse).
Who = hasan
Whoelse = rita ;
Who = belal
Whoelse = rita ;
Who = mohsin
Whoelse = beli ;
no
?- likes(Who, beli).
Who = mohsin
?- likes(hasan, Whom).
Whom = rita ;
no
?- likes(belal, Whom).
Whom = rita ;
no
31.
Backtracking with AnonymousVariable
Suppose that you want to know if Hasan likes anyone and
do not care who in particular.
Then use the anonymous variable which is the character
“_”
It represents a different variable each time it occurs
?- likes(hasan, _).
yes
?- likes(_, _).
yes
Suppresses output of variable binding
32.
Rules
A PROLOGrule consists of one conclusion
followed by one or more conditions
– a fact is a rule with no conditions
– conclusion :-
condition1,
.....
condition N.
– read as:
The conclusion is true if condition1 and condition2... and
condition N are true.
Conditions are separated by commas
Rules , facts and queries are all examples of Horn clauses -
33.
Rules … …
A PROLOG rule consists of a
head and a body
– head :-
body.
– read as:
If a match is made on the
head, then carry out the body.
34.
Arithmetic Functions/ Predicates
Operators for the basic arithmetic operations
+, -, *, /, **, mod, // (integer division).
Examples:
?- 7+3=10. ? - X is 5/2
No ? - Y is 17//3.
?- 10 is 7+3. ?- Z is 23 mod 5.
Yes
Predicates for the basic arithmetic operations
<, =<, >, >=, =.=, ==.
Examples:
?- 14-9=.=3+2. ? - 10 ==7+5.
Yes Yes
35.
PROLOG syntax ……
Lists
A sequence of terms of the form
[t1, t2, t3, t4, ..., tn]
where term ti is the ith element of the list
Examples:
[a,b,c] is a list of three elements a, b and c.
[[a,list,of,lists], and, numbers,[1,2,3]]
is a four element list.
[ ] is the ‘empty list’. It is an atom not a list data type
36.
Knowledge from SemanticNet
Example:
The Queen Mary is an ocean liner.
Every ocean liner is a ship
ship
Ocean_liner
Queen_mary
IS-A
IS-A
37.
Semantic Networks
Knowledgeis represented as a network or graph
animal
reptile
elephant
Nellie
mammal
apples
large
head
subclass subclass
HAS_PART
subclass
instance
LIKES
SIZE
africa
LIVES_IN