You are on page 1of 11

Trng i Hc Bch Khoa TP H Ch Minh Khoa Cng Ngh Thng Tin

Hunh Tn t
Email: htdat@cse.hcmut.edu.vn Home Page: http://www.cse.hcmut.edu.vn/~htdat/

LISt Processing John McCarthy, 1958 X l trn cc k hiu Ngn ng lp trnh hm vi vic s dng nhiu cc php gi qui L ngn ng hng chc nng (Functional language) Mt trong nhng ngn ng c bn cho ngnh Tr tu Nhn to (Artificial Intelligence)

Faculty of Computer Science and Engineering HCMUT

Slide 2

Dng cch k hiu tin t (prefix) v du ngoc n V d f(x, y, z) (f x y z) x + y k hiu l (+ x y) sin(3x + /2) (sin(+(* 3 x) (/ pi 2))) Lisp l ngn ng thng dch (interpreted language) Bin trong Lisp khng c kiu. Mt bin c th c nhiu kiu d liu khc nhau V d * (setq a 2) * (setq a (1 2 3)) 2 (1 2 3)
Faculty of Computer Science and Engineering HCMUT Slide 3

Lisp l ngn ng c trng cho vic x l danh sch Chng trnh c biu din bng danh sch v c th thao tc trn nh d liu V d: (+ (* 3 4) (- 5 2)) L hm cng p dng vo hai i s V mt d liu: l danh sch gm ba thnh phn Khi ng vai tr l i s ca mt hm, mt danh sch lun c xem l s p dng ca mt hm * (+ 3 4) 7 * (+ (* 3 4) (- 5 2)) 15 *4 4 * (+ 3 4) 7 * (+ 3 4) (+ 3 4)
Slide 4

Faculty of Computer Science and Engineering HCMUT

Gn: setf v setq Cc php tnh s hc +, , *, /, \\ 1+ v 1 max, min v abs * (setf x 27) 27 * (/ x 9) 3 * (/ 22 7) 3.14286 * (max 3 4 5) 5 * (abs -2) 2
Slide 5

Thnh phn c bn ca biu thc l cc i tng atom Mt nhm cc i tng atom to nn list Cc danh sch c th c nhm vi nhau c danh sch mc cao hn Biu thc expression ::= atom | list Danh sch list ::= (expression1 ... expressionn) Atoms atom ::= s | symbols Hng Boolean T v nil

Faculty of Computer Science and Engineering HCMUT

Faculty of Computer Science and Engineering HCMUT

Slide 6

Symbol (~ identifier): t to bi cc k t bt k, ngoi tr ( ) ` ; v khong trng V d: + * example l cc symbol Ba trng hp thng s dng: Tn hm D liu: chui k t hay s Tn bin Vi vai tr l i s trong hm, symbol c xem nh tn ca mt bin V d: +, *, example l cc symbol

* example Unbound variable: EXAMPLE * (setf example 3) 3 * (+ example 4) 7 * example example

Bi tp: Symbol hay s ? AARDVARD symbol 87 number 1-2-3-GO symbol 3.12 number 7-11 symbol 22/7 symbol 12 number

Faculty of Computer Science and Engineering HCMUT

Slide 7

Faculty of Computer Science and Engineering HCMUT

Slide 8

Lisp khng c kiu d liu Boolean nil biu din gi tr logic sai, tt c cc biu thc khc biu din tr logic ng Mc nh dng T tr logic ng nil v T l cc symbol hng * nil NIL *T T nil danh sch rng ()

FIRST v REST CAR v CDR CONS, APPEND, LIST NTHCDR, BUTLAST v LAST LENGTH v REVERSE * (setf l (a b c d e) k (f g h)) (F G H) * (first l) * (list a b c) (A B C) A * (nthcdr 2 l) * (rest l) (C D E) (B C D E) *l * (cons f l) (A B C D E) (F A B C D E)
Slide 9 Faculty of Computer Science and Engineering HCMUT

* (butlast l 2) (A B C) * (last l) (E) * (reverse l) (E D C B A)


Slide 10

Faculty of Computer Science and Engineering HCMUT

Danh sch kt hp association list hay a-list Key Key

(setf sarah ((height .54) (weight 4.4))) Value Value

Thm mt thnh phn mi vo danh sch (ACONS <key> <value> <asociation list>) V d: * (setf Andrew ((height .74) (weight 6.4))) ((HEIGHT 0.74) (WEIGHT 6.4)) * (acons nick Bobby Andrew) ((NICK BOBBY) (HEIGHT 0.74) (WEIGHT 6.4))

Ly mt thnh phn t mt danh sch (ASSOC <key> <asociation list>) * (assoc weight sarah) (WEIGHT 4.4)
Faculty of Computer Science and Engineering HCMUT Slide 11 Faculty of Computer Science and Engineering HCMUT Slide 12

C php
(defun FunctionName <parameter-list> <expressions>)

V d
* (defun square (x) (* x x)) SQUARE * (square 3) 9 * (defun abs(x) (if (>= x 0) x (* -1 x) ) ) ABS * (abs -3) 3

(defun fac (n) (if (= n 0) 1 (* n (fac (1- n) ) ) ) ) Bi tp: Vit hm tr v phn t th n trong danh sch. nh gi biu thc Exp l cch vit tt ca (quote Exp) eval >< quote * (setf c a) * (eval (list car l)) A a * (eval (list + (1+ 3) 2)) * (setf l (a b c)) 6 (a b c)
Faculty of Computer Science and Engineering HCMUT Slide 14

V d: Tnh giai tha n! = n * (n 1)! 0! = 1

Faculty of Computer Science and Engineering HCMUT

Slide 13

(numberp E) tr v ng nu E l s chui (stringp E) danh sch (listp E) nil (null E) atom (atom E) * (numberp 4) T * (numberp 3.45) T * (symbolp ListProcessor) T * (listp (a b c)) T

ZEROP PLUSP ODDP EVENP < > =

kim tra zero kim tra s dng kim tra s l kim tra s chn so snh first < second so snh first > second so snh first = second

MINUSP kim tra s m

Faculty of Computer Science and Engineering HCMUT

Slide 15

Faculty of Computer Science and Engineering HCMUT

Slide 16

= eq eql equal

Are two argument values the same number? the same symbol? the same symbol or number? the same expression? * (setf x a y a) A * (equal x y) T * (eq x y) T * (eql x y) T * (eq 3 3) T * (eql 3 3.0) NIL * (= 3 3) T * (= 3 3.0) T
Slide 17

equal kim tra tha eql hay khng. Nu khng, xem i s l hai danh sch v xem tng cp phn t c tha equal. eql kim tra tha eq hay khng. Nu khng, kim tra l hai s cng kiu v cng gi tr. eq kim tra xem cc i s c c biu din bi cng khi b nh khng (cc symbol ging nhau tha iu ny). = kim tra hai i s cng l mt s (khng cn cng kiu)

* (setf x (a b) y (a b)) (A B) * (equal x y) T * (eq x y) NIL * (eql x y) NIL

Faculty of Computer Science and Engineering HCMUT

Faculty of Computer Science and Engineering HCMUT

Slide 18

AND (and E1 ... En) sai nu t nht mt Ei sai AND nh gi t tri phi v dng li khi gp i s sai Nu mi i s u ng, AND tr v gi tr ca i s cui cng * (and (numberp n) (oddp n)) T * (setf n 3) * (and (numberp n) (oddp n) (1+ n)) 3 4 * (and (numberp n) (> n 3) ) NIL
Faculty of Computer Science and Engineering HCMUT Slide 19

OR (or E1 ... En) ng nu t nht mt Ei ng OR nh gi t tri phi v dng li khi gp i s ng * (setf x a) A *x A * (or (numberp x) (> x 1)) wrong type argument * (or (symbolp x) (list x)) T
Faculty of Computer Science and Engineering HCMUT Slide 20

Ton t NOT NOT n gin i nonNILNIL v NILT * (not nil) T * (not dog) NIL * (not (symbolp 4.3)) T

IF (if E1 E2): Nu E1 ng, tr v gi tr E2 (if E1 E2 E3): ... nu khng tr v gi tr E3 COND (if Test1 (progn E1 ) (cond (Test1 E1 ) (if Test2 (progn E2 ) (Test2 E2 ) (if Test3 (progn E3 ) (Test3 E3 ) (if Testn (progn En )) (Testn En ) ) ) ) )
Slide 21 Faculty of Computer Science and Engineering HCMUT Slide 22

Faculty of Computer Science and Engineering HCMUT

* (if (numberp 1) (a number) (not a number)) (A NUMBER) * (if (numberp a) (a number) (not a number)) (NOT A NUMBER) * (setf x (a b c)) (A B C) * (cond ((numberp x) (This is a number)) ((symbolp x) (This is a symbol)) ((listp x) (This is a list)) ) (THIS IS A LIST)
Faculty of Computer Science and Engineering HCMUT Slide 23

WHEN (when Test E1 ... En): Nu Test ng, thc hin E1 ... En v tr v kt qu En V d: * (setf n 3) * (when (numberp n) (setf n 5) (setf l (a b c))) (A B C)

Faculty of Computer Science and Engineering HCMUT

Slide 24

PROG1 v PROGN (prog1 E1 ... En) nh gi tun t cc biu thc E1, ..., En t tri sang phi v kt qu tr v l gi tr ca biu thc E1 (progn E1 ... En) nh gi tun t cc biu thc E1, ..., En t tri sang phi v kt qu tr v l gi tr ca biu thc En * (progn (setf x (a b c)) * (prog1 (setf x (a b c)) (append x x) ) (append x x) ) (A B C A B C) (A B C) *x (A B C)
Faculty of Computer Science and Engineering HCMUT Slide 25

Ton t let Em+1 En) (let ((var1 E1) (varm Em)) Gn cho mi bin gi tr ca biu thc tng ng, sau nh gi: (progn Em+1 En) * (let ((x (fac 4))) (* x x)) 576 Che ph cc bin ton cc * (setf x 5) 5 * (let ((x 1)) x) 1

*x 5 * (let ((x 1)) (setf x 2) x) 2 *x 5


Slide 26

Faculty of Computer Science and Engineering HCMUT

Che ph cc i s ca mt hm * (defun foo(x) * (foo 4) 1 (let ((x 1)) x ) ) FOO Cc lin kt c thc hin song song * (defun bar(x) * (bar 4) (let ((x 1) (y (1+ x))) 5 y) ) BAR

DOTIMES Lp trn s (dotimes (<count> <upper-bound-form> <result form>) <body>) upper-bound-form c nh gi n count c gn cc s t 0 n 1 (n ln) Thn vng lp <body> c thc hin n ln, gi tr ca <result form> l kt qu cn tnh

Faculty of Computer Science and Engineering HCMUT

Slide 27

Faculty of Computer Science and Engineering HCMUT

Slide 28

V d Tnh ly tha mn = m m m (defun power(m n) (let ((result 1)) (dotimes (count n result) (setf result (* m result)) ) ) )

DOLIST Lp trn danh sch (dolist (<element parameter > <list form> <result form>) <body>) list form c nh gi thnh danh sch a1 an element c gn t a1 an (n ln) Vi mi gi tr ai, phn thn vng lp (body) c nh gi. Khng c result form, DOLIST s tr v NIL

Faculty of Computer Science and Engineering HCMUT

Slide 29

Faculty of Computer Science and Engineering HCMUT

Slide 30

V d m s phn t l s trong danh sch (defun collect-number(l) (let ((result 0)) (dolist (e l result) (when (numberp e) (setf result (+ result 1)) ) ) ) )
Faculty of Computer Science and Engineering HCMUT Slide 31

Apply (apply F L): p dng hm F trn cc phn t ca danh sch L (apply F L) (eval (cons F L)) V d: * (apply append ((a b) (c d))) * (setq a *) (a b c d) * * (apply + (1 2)) * (apply a (3 4)) 3 12 * (eval (+ 3 4)) 7
Faculty of Computer Science and Engineering HCMUT Slide 32

m s k hiu, s hay chui trong mt list *(defun count (test l) (if (null l) 0 (if (apply test (list (car l))) (1+ (count test (cdr l))) (count test (cdr l)) ) ) ) COUNT * (count numberp (2 a b 3 4 c 5 d 6)) 5 * (count symbolp (2 a b 3 4 c 5 d 6)) 4
Faculty of Computer Science and Engineering HCMUT Slide 33

Funcall (F E1 En) (funcall F E1 En) p dng gi tr ca thng s th nht (mt hm hay tn ca mt hm) vo cc thng s tip theo V d: * (setq a +) + * (funcall a (+ 3 4) 5) 12 (funcall f E1 E2) (apply f (list E1 En))

Faculty of Computer Science and Engineering HCMUT

Slide 34

m cc s nguyn nh hn 10 trong danh sch: (defun count (test l) (if (null l) 0 (if (funcall test (car l)) (1+ (count test (cdr l))) (count test (cdr l)) ) )) * (defun inf10 (x) (and (integerp x) (< x 10)) ) INF10 * (count inf10 (4 12 a 11 3)) 2
Faculty of Computer Science and Engineering HCMUT Slide 35

An unnamed function - Lambda expression (lambda <parameter-list> <expressions>) * (count (lambda (x) (and (integer x) (< x 10) ) ) (4 12 a 11 3) 2 C th nh ngha lambda-expression khi x l: * (+ 10 ((lambda (x) (* x x)) 4) ) 26

Faculty of Computer Science and Engineering HCMUT

Slide 36

Hm v danh v bin cc b (let ((var1 val1) (varN valN)) E) <=> ( ( lambda (var1 varN) E) val1 valN) V d * (let ((x 1) (y 2)) (+ x y)) 3 * ( ( lambda (x y) (+ x y) ) 1 2 ) 3

Syntax: (defun FunctionName <parameter-list> <expressions>) (lambda <parameter-list> <expressions>) an unnamed function <parameter-list> ::= (Var1 Var2 Var3 ...) <expressions> ::= <expression>+ <expression> ::= <symbol> | Variable | <symbol-list> | <function-call> <symbol> ::= Number | String <symbol-list> ::= (<symbol> <symbol> <symbol>) <function-call> ::= (FunctionName <expressions>)
Slide 37 Faculty of Computer Science and Engineering HCMUT Slide 38

Faculty of Computer Science and Engineering HCMUT

(load filename) reads from the file name and interprets its contents. Ex: (load test) (close-all-files) close all open streams connected to files. (print <expression>) prints value of the expression to the screen. Ex: (print (+ 3 7)) (setq variable <expression>) sets variable to the value of the expression. variable prints the value of the variable. list prints the contents of the list. The last expression evaluated in a function is displayed.
Faculty of Computer Science and Engineering HCMUT Slide 39

(quote <something>) same as ( <something>) (car list) first element of list (cdr list) the list without its first element (caddr list) car(cdr(cdr list) (null x) true if the list x is null. Ex: (null ()) returns true (cons a b) makes a list with a and b (append list1 list2) appends 2 lists

Faculty of Computer Science and Engineering HCMUT

Slide 40

(if P E1 E2) (cond (P1 E1) (P2 E2) ... (else En)) if P1 then E1, else if P2 then E2, ..., else En (lambda (x) (* x x)) an unnamed function that can be used as an operator (let ((x1 E1) (x2 E2) ...(xn En)) F) assigns var to each expression and then evaluates F

Enter the editor: Ctrl + E Exit editor into Lisp Environment: F1 Get into LISP Explorer: Alt + E Get help for function documentation: 1) Alt + H to get into help mode 2) Alt + D to get into query mode for function name 3) Type function name See contents of another directory: F6 then enter the path name. Load a file into the editor: F8 Save a file: F9 Save a file as another name: F10
Faculty of Computer Science and Engineering HCMUT Slide 42

Faculty of Computer Science and Engineering HCMUT

Slide 41

You might also like