Professional Documents
Culture Documents
CSE440 Lect 5 LISP
CSE440 Lect 5 LISP
Email: sazzad.hossain14@northsouth.edu
1
Contents
2
Introduction to LISP
4
Introduction: LISP
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.
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)
9
Example of (setq()) function
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
15
Example
16
Iteration
17
Property Lists and Arrays
18
Property Lists and Arrays
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