Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Artificial Intelligence

Lecture #5: AI programming


Language (LISP)
Dr. Md. Sazzad Hossain, PhD(Japan)
Professor
Department of CSE
Mawlana Bhashani Science and Technology University

Email: sazzad.hossain14@northsouth.edu

1
Contents

 AI programming Language (LISP)


 Introduction to LISP
 LISP: Syntax and Numeric Functions
 List Manipulation function in LISP

2
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
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
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
(print (+ 5 6 9))
 LISP uses prefix notation.
5
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
(print (+(*(/ 9 5)50)32));
 Numerical functions: +, -, *, /

6
Basic List Manipulation Functions
Function call Value Remarks
returned
(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
Basic List Manipulation Functions
Function call Value Remarks
returned
(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
Example: cons
Given a list L and an item x (either an atom or a list)
(cons x L) returns a new list with x as first element and L
as rest (L is unaffected)

(let* ((L '(peanut butter jelly))


(J (cons 'apple L)))
(print J));

9
Example of (setq()) function

->(setq x `(a b c))


->x
ABC
->`x
X
->(setq x (+ 3 5))
8
10
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.
(defun avgthree (n1 n2 n3) (/(+ n1 n2 n3) 3))
(print (avgthree 1 2 3))
-> 2
(defun square (x) (* x x))
(print (square 6))
->36
(print (mapcar 'square '(1 2 3 4 5)))
-> 1 4 6 16 25 11
The Most Common Predicate calls

Function call Value Remarks


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

12
Conditional Statement
 The syntax for condition is,Example:
cond (<test1> <action1>) (defun max3 (a b c)
(cond ((> a b) (cond ((> a c) a) (t c)))
(<test2> <action2>)
((> b c) b)
. . (t c)))
. .
. . (print (max3 1 2 3))

(<testk>
<actionk>))

13
Input, Output Variables
prinl: print without new line
princ: remove double quotation marks
read: ->(+ 5 (read)) terpri: it does not take arguments
6 It introduces a new line whenever it appears
and then returns nil.
11 ->(defun circle-area()
-> (terpri)
print: ->(print`(a b c)) (princ “Please enter the radius:”)
(A B C) (setq radius (read))
(A B C) (princ “ The area of the circle is:”)
(princ (* 3.1416 radius radius))
->(print (terpri))
“hello”) CIRCLE-AREA
“hello”
“hello”
14
Input, Output Variables

(defun area-circle(rad) ->(circle-area)


"Calculates area of a circle with
given radius" Please enter the radius: 4
(terpri) The area of the circle is: 50.2656
(format t "Radius: ~5f" rad) ->
(format t "~%Area: ~10f" (*
3.141592 rad rad))
)
(area-circle 10)

15
Example

(defun factorial (num)


(cond ((zerop num) 1)
(t ( * num (factorial (- num 1)))) ) ) (setq n 6)
(format t "~% Factorial ~d is: ~d" n
(factorial n))

16
Iteration

(do ((x 0 (+ 2 x)) (y 20 ( - y 2))) ((= x y)(-


x y)) (format t "~% x = ~d y = ~d" x y) )

(loop for x from 12 to 25 by 3 do


(print x)
)

17
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.

18
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
19
Property Lists and Arrays

 remprop: remove properties (setf (get 'car 'color) 'red)


 get: retrieve property (print (get 'car 'color))
(remprop 'car 'color)
Example: (print (get 'car 'color))
-> (get `car `color)
RED
-> (remprop `car `color)
RED
->(get `car `color)
Nil
->

20
Property Lists and Arrays
Arrays:
->(setf myarray (make-array `(10)))
#A(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
->
To access array:
->(aref myarray 9)
NIL
->
Store:
->(setf (aref myarray 0) 25)
25

21
LISP Compiler

LISP Compiler
• BEE
• POPLOG
• LISP WORKS
• GNU C LISP

https://rextester.com/l/common_lisp_online_compiler
https://onecompiler.com/commonlisp
22
End of Presentation

Questions/Suggestions

Thanks to all !!!

You might also like