1 Pertemuan 4 Bahasa Pemrograman Logika Matakuliah: H0383/Sistem Berbasis Pengetahuan Tahun: 2005 Versi: 1/0
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan bahasa pemrograman untuk implementasi predicate logic
3 Outline Materi LISP PROLOG
4 Bahasa LISP LISP menggunakan notasi prefix –( ) – –Contoh: konversi 50 Celcius Fahrenheit: –(+(*(/ 9 5) 50) 32) List dalam LISP –(a b (c d) e f) adalah list –(c d) adalah sublist –c, d adalah top element dari sublist
5 Bahasa LISP List kosong = NIL Variable assignment: –(setq x 10) artinya x = 10 –(setq x (+ 3 5)) == x=3+5 Basic list manipulation function –(car’(a b c))= a –(cdr’(a b c))= (b c) –(cons’a’(b c))= (a b c) –(list ’a ’(bc))= (a (b c))
6 Bahasa LISP Function call: –(function-name arg1 arg2 …) Additional manipulation list –(append ’(a) ’(b c)) = (a b c) –(last ’(a b c d)) = (d) –(member ’b ’(a b d) = (b d) –(reserve ’(a (b c) d)) = (d (b c) a)
7 Bahasa LISP Defining function –(defun name(parm1, parm2 …) body) –Defun averagethree (n1 n2 n3) (/ (+ n1 n2 n3) 3)) –(averagethree ) = 20
8 Bahasa LISP Predicate call: –(atom ‘aabb) = true; aabb is a valid atom –(equal ‘a (car ‘(a b)) = true; a=a –(greaterp )= true; (lessp )= nil –(zerop ) = nil; –(evenp 3) = nil; (oddp 3) = true; –(number 10) = true; –(listp ‘(a)) = true; a is a valid list
9 Bahasa Lisp Conditional: –(cond( ) ( ) : ( ) (defun maximum2 (a b) (cod ((>a b) a) (t b))) maximum2( ) = 320
10 Bahasa LISP Logical function: or, and, not, t(true), null Input: (+5 (read)) If 6 is inserted by keyboard then result is 11 Output: print’(a b c) ; print a list
11 Bahasa LISP Iteration –(do ( <var-update1) : ( ) (s-expression) Contoh : factorial: (defun factorial (n) (do ((count n (- count 1)) (product n (* product (-count 1) ((equal 0 count) product)))
12 Bahasa LISP Recursive: (defun factorial (n) (cond (( zerop n) 1) (t (* n (factorial (- n 1)))))
13 Bahasa LISP Property lists –(putprop object value attribute) –(putprop ’car ’ford ’make) –(putprop ’car ’1988 ’year) –(putprop ’car ’red ’color) –(putprop ’car ’four-door ’style) –(get ‘car ‘make) = Ford –(get ‘car ‘color) = red
14 Bahasa LISP Arrays –(setf myarray(make-array ’(10))) –(setf (aref myarray 0) 25 –(setf (aref myarray 1) ’red –(aref myarray 0) = 25 –(aref myarray 1) = red
15 Bahasa LISP Mapping function: –(mapcar ’1 +’( )) = ( ) –(mapcar ’+ ’( ) ’( )) = ( ) Lambda function: –(defun cubic(lists) (mapcar #’(lambda (x) (*x x x)) lists)) –(cubic( )) = ( )
16 Bahasa PROLOG PROLOG: PROgramming in Logic –sister(sue,bill) –parent(ann,sam), parent(joe,ann) –male(joe), female(ann) If X is the parent of Y, and Y is the parent of Z, and X is a male, then X is grandfather of Z grandfather(X,Z) :- parent(X,Y), parent(Y,Z),male(X)
17 Bahasa PROLOG Query: ?- parent(X,sam) X=ann ?- female(joe) No ?-male(joe) yes
18 Bahasa PROLOG List in PROLOG: [tom,sue,joe,marry,bill] ?- [Head|Tail] = [tom,sue,joe,marry] Head = tom, Tail=[sue,joe,marry] List manipulation: append, member, conc, add, delete
19 Bahasa PROLOG member(X,[X|Tail]) member(X,[Head|Tail]):-member(X,Tail) X is a member of the list L if X is the head of L. X is a member of L if X is a member of the tail of L ?- member(c,[a,b,c,d]) yes ?- member(b,[a,[b,c],d]) No
20 Penutup Bahasa-bahasa pemrograman logika dapat digunakan untuk implementasi problem berbasis pengetahuan. Selain LISP dan PROLOG, terdapat berbagai jenis lainnya misal CLIPS. Meski beragam, namun semantiknya serupa.