1 / 12

LISP part 2

LISP part 2. By : asisten KBP ‘09. LIST. List dipakai baik untuk program maupun untuk data. List : urutan dari 0 (nol) atau beberapa harga. Element dari list dapat terdiri dari boolean, angka, simbol, list, function. List kosong : () atau null

corbin
Download Presentation

LISP part 2

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. LISP part 2 By : asisten KBP ‘09

  2. LIST • List dipakai baik untuk program maupun untuk data. • List : urutan dari 0 (nol) atau beberapa harga. Element dari list dapat terdiri dari boolean, angka, simbol, list, function. • List kosong : () atau null • Untuk membuat list menggunakan perintah Setf atau Setq.

  3. Contoh • (it seems that) => list 3 elemen • ((it seems that) you (like) me) => list 4 elemen • (a) => list 1 elemen • (a ()) => list 2 elemen

  4. Operasi – operasi pada list • (null? X) • (car X) => mengambil elemen pertama • (cdr X) => mengambil sub- himpunan dengan membuang elemen pertama • (cons a x)

  5. Contoh • (setf x ‘((it seems that) you (like) me)) • (car x) => (it seems that) • (car (car x)) = (caar x) => it • (cdr (car x)) = (cdar x) => (seems that) • (cdr x) => (you (like) me) • (car (cdr x)) = (cadr x) => you • (cdr (cdr x)) = (cddr x) => ((like) me)

  6. Conses • Conses sangat mudah dipakai: > (cons 4 5);mengalokasikan sebuah cons. Set car menjadi 4 dan cdr menjadi 5. (4 . 5) > (cons (cons 4 5) 6) ((4 . 5) . 6) > (car (cons 4 5)) 4 > (cdr (cons 4 5)) 5

  7. Function • > (defun foo (x y) (+ x y 5)) • ;membuat fungsi FOO • > (foo 5 0);memanggil function • 10 ;output

  8. Fungsi rekursi : > (defun fact (x) (if (> x 0) (* x (fact (- x 1))) 1 ) ) FACT ;output > (fact 5) 120 ;output

  9. Perulangan >(setq a 4) 4 >(loop (setq a (+ a 1)) (when (> a 7) (return a) ) ) 8

  10. Sorting • LISP menyediakan dua dasar untuk sorting: sort and stable-sort. >(sort '(2 1 5 4 6) #'<) (1 2 4 5 6) >(sort '(2 1 5 4 6) #'>) (6 5 4 2 1)

  11. Beberapa Fungsi List lainnya > (append '(1 2 3) '(4 5 6));menggabung list (1 2 3 4 5 6) > (reverse '(1 2 3));membalik elemen dalam list (3 2 1) > (member ‘d '(b d a c)) ;membuat membership– mengembalikan nilai dari awal sampai akhir (D A C)

  12. > (intersection '(a b c) '(b));set intersection (B) > (union '(a) '(b));set union (A B) >(set-difference '(a b) '(a));set difference (B)

More Related