220 likes | 409 Views
Pertemuan 4 Bahasa Pemrograman Logika. Matakuliah : H0383/Sistem Berbasis Pengetahuan Tahun : 2005 Versi : 1/0. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan bahasa pemrograman untuk implementasi predicate logic. Outline Materi. LISP
E N D
Pertemuan 4Bahasa Pemrograman Logika Matakuliah : H0383/Sistem Berbasis Pengetahuan Tahun : 2005 Versi : 1/0
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Mendemonstrasikan bahasa pemrograman untuk implementasi predicate logic
Outline Materi • LISP • PROLOG
Bahasa LISP • LISP menggunakan notasi prefix • (+ 5 6 9) • 5 + 6 + 9 • 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
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))
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)
Bahasa LISP • Defining function • (defun name(parm1, parm2 …) body) • Defun averagethree (n1 n2 n3) (/ (+ n1 n2 n3) 3)) • (averagethree 10 20 30) = 20
Bahasa LISP • Predicate call: • (atom ‘aabb) = true; aabb is a valid atom • (equal ‘a (car ‘(a b)) = true; a=a • (greaterp 2 4 27)= true; (lessp 5 3 1 2)= nil • (zerop .000001) = nil; • (evenp 3) = nil; (oddp 3) = true; • (number 10) = true; • (listp ‘(a)) = true; a is a valid list
Bahasa Lisp • Conditional: • (cond (<test1> <action1>) (<test2> <action2>) : (<testk> <actionk>) (defun maximum2 (a b) (cod ((>a b) a) (t b))) maximum2(234 320) = 320
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
Bahasa LISP • Iteration • (do (<var1 val1><var-update1) : (<test> <return-value>) (s-expression) • Contoh : factorial: (defun factorial (n) (do ((count n (- count 1)) (product n (* product (-count 1) ((equal 0 count) product)))
Bahasa LISP • Recursive: • (defun factorial (n) (cond (( zerop n) 1) (t (* n (factorial (- n 1)))))
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
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
Bahasa LISP • Mapping function: • (mapcar ’1 +’(5 10 15 20 25)) = (6 11 16 21 26) • (mapcar ’+ ’(1 2 3 4 5 6) ’(1 2 3 4)) = (2 4 6 8) • Lambda function: • (defun cubic(lists) (mapcar #’(lambda (x) (*x x x)) lists)) • (cubic(1 2 3 4)) = (1 8 27 64)
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)
Bahasa PROLOG • Query: • ?- parent(X,sam) • X=ann • ?- female(joe) • No • ?-male(joe) • yes
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
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
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.