1 / 20

Pertemuan 4 Bahasa Pemrograman Logika

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

gaerwn
Download Presentation

Pertemuan 4 Bahasa Pemrograman Logika

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pertemuan 4Bahasa 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 • (+ 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

  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 10 20 30) = 20

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

  9. Bahasa Lisp • Conditional: • (cond (<test1> <action1>) (<test2> <action2>) : (<testk> <actionk>) (defun maximum2 (a b) (cod ((>a b) a) (t b))) maximum2(234 320) = 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 (<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)))

  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 +’(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)

  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.

More Related