1 / 35

Tutorial 3

Tutorial 3. CSCI3230 (2013-2014 First Term) By Paco WONG (pkwong@cse.cuhk.edu.hk) Antonio SZE-TO (hyszeto@cse.cuhk.edu.hk) Qin CAO (qcao@cse.cuhk.edu.hk). Hands on Lab @SHB924. Outline. Lispbox Setup IDE Example Short Review Guided Practice Programming Exercises.

oki
Télécharger la présentation

Tutorial 3

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. Tutorial 3 CSCI3230 (2013-2014 First Term) By Paco WONG (pkwong@cse.cuhk.edu.hk) Antonio SZE-TO (hyszeto@cse.cuhk.edu.hk) Qin CAO (qcao@cse.cuhk.edu.hk) Hands on Lab @SHB924

  2. Outline • Lispbox • Setup • IDE • Example • Short Review • Guided Practice • Programming Exercises

  3. Lispbox • Download from http://common-lisp.net/project/lispbox/ • Open Lispbox User input and interpreter output Messages and instructions

  4. Hello World

  5. LISP IDE • A prompt for you to enter a LISP expression when CL started: CL-USER> • REPL: Read-evaluate-print loop • Interactive: at any time you can try your expressions CL-USER>(cos (/ pi 4)) 0.7071067811865476D0 CL-USER>"hello world" "hello world" CL-USER> Read Print Evaluate Q: How is s-expression evaluated? [See Tutorial 1: Control the Flow of Evaluation]

  6. How to Edit, Save, Load and Compile • Edit • Emacs from Lispbox(Ctrl-h t for a tutorial) • Any other text editors • Save e.g., testing.lisp(or .cl) • Load (load "testing.lisp") • Compile (compile-file "testing.lisp")

  7. Example 1 mysum.lisp (defunmysum (x y) ;"Sum any two numbers after printing a message." ;printf("Summing %d and %d.\n", x, y) (format t "Summing ~d and ~d.~%" x y) (+ x y)) Lisp IDE > (load “mysum.lisp") ;Load the mysum.lisp to the environment #P"d:/Software/lispbox-0.7/mysum.lisp" > (mysum10 2) ;Test your function Summing 10 and 2.12

  8. Example 2 testing.lisp (defun sum_square (n) (let ((r 0)) (do ((i 1 (+ i 1))) ((= i n) r) (setq r (+ r (* i i)))))) Lisp IDE > (load "testing.lisp") ;Load the testing.lisp to the environment #P"d:/Software/lispbox-0.7/testing.lisp" > (sum_square 10) ;Test your function 285

  9. Try (sum_square) Click 3to kill OR Press key 3to abort OR Press key a to abort

  10. Emacs • Split the buffer to two vertical frames • Ctrl-X 3 • Open a file in a buffer • Click the open button OR • Ctrl-X f • Enable Parentheses Match Highlighting • Save the buffer • Click the save button

  11. SLIME • The Superior Lisp Interaction Mode for Emacs

  12. Review

  13. The most basic (indivisible) unit in LISP • Any combination of characters, except "(" and ")", can be an atom. For an atom with " "(whitespace) character, it needs to be written as |an atom is here|. • 3 types of Atom: • Symbols • Not case sensitive. • E.g. John, abc, 23-Jordan • Numbers • E.g. 123, 0 • 3/4 ; rational number • #C(3 4) ; complex number = 3 + 4j • #C(3/4 4) ; complex number = 0.75 + 4j != 3/4 + 4j • Constants (self-evaluating) • Symbols that have special meaning • E.g. NIL, T Atom ;Comment here

  14. List • A non-atomic s-expression. • A collection of atom or list enclosed by parentheses ( ). • (Jordan 23) ; a list of atoms "Jordan" and "23" • (Jordan (3/4 23)) ; a list of atoms Jordan and a list of "3/4" and "23" • ( ) ; a null list

  15. Symbolic-Expression • An s-expression is defined recursively: • An atom is an s-expression • If s1, s2, …, sn are s-expressions, then so is the list (s1 s2 … sn).

  16. NIL An special list called the null (empty) list ( ) Also an atom Means "false" or "nothing" ANY non-"NIL" symbol is considered as "true" in LISP A subtype of everything T Reserved as the default symbol for "true". ALL the data types are subtypes of T. NIL and T

  17. Form • A form is an s-expression that is intended to be evaluated. • If it is a list, the first element is treated as the operator(functions, macros or special forms)and the subsequent elements are evaluatedto obtain the function arguments. Example 1 (+24)

  18. Many Functions • S-expression • Atom and list • Evaluation and its control • Form, QUOTE and EVAL • Binding variable explicitly • SET, SETQ and SETF • Cons cell and list • CONS, CAR, CDR, NTHCDR, NTH, APPEND, … • Predicates • TYPEP, SUBTYPEP, EQL, … • Conditional constructs • IF THEN ELSE, COND, … • Iteration • LOOP, DO, … • Let • Function • DEFUN • Macro • DEFMACRO • Structure • DEFSTRUCT • Property list • GET Refer to the previous tutorials for details

  19. Guided Practice If-then-else Recursion 1 Recursion 2 Prime Test Tree Traversal

  20. 1. If-then-else (defunisZero (n) (if (= n 0) (format t "~D is zero.~%" n) (format t "~D is not zero.~%" n))) Try this (isZero 1) (isZero 0)

  21. 2. Recursion 1 ;To demonstrate recursion: 1+2+3+4+...+n (defun sum1 (n) (if (< n 1) 0 (if (= n 1) 1 (+ (sum1 (- n 1)) n)))) Try this (sum 5) (sum 10) (sum -5)

  22. 3. Recursion 2 ;To demonstrate recursion - 1^3+2^3+3^3+4^3+...+n^3 (defun sum3 (n) (if (< n 1) 0 (if (= n 1) 1 ) ) ) <Put your code here>

  23. 4. Is Prime ? Try this (isPrime 1) (isPrime 11) (isPrime -5)

  24. 5. Tree Traversal Try this (printTree '(1 nil nil)) (printTree'(2 '(1 nil nil) nil)) (printTree'(2 '(1 nil nil) '(3 nil nil))) ;print the tree in order (defunprintTree (tree) (let ((num (car tree)) (ltree (eval (cadr tree))) (rtree (eval (caddr tree)))) (if (not tree) nil (progn (print num) (printTreeltree) (printTreertree) )) num))

  25. Programming Ex Define a function Define a recursive function and iterative function Read a segment from a list Remove a segment from a list Sort a list Traverse a tree Use program as data Propose an interesting question for yourself!

  26. Programming Exercise 1 • Define a function sum(n) which returns the result of 1+2+3+4+5+6+…+n

  27. Programming Exercise 2 • Define a recursive function Fibonacci_r(n) which returns the nth number in the Fibonacci sequence. • (Fibonacci_r 1) gives 1 • (Fibonacci_r 2) gives 1 • (Fibonacci_r3) gives 2 • (Fibonacci_r4) gives 3 • Similarly, define an iterative function Fibonacci_i(n) • Finally, execute (mapcar #'Fibonacci_r '(1 2 3 4 5))

  28. Programming Exercise 3 • Write a function Extract (L i j) which extracts the ith to jth elements. • (Extract '(1 2 3 4 5) 1 2) gives (2 3) • (Extract '(1 2 3 4 5) 1 1) gives 2 • (Extract '(1 2 3 4 5) 0 4) gives (1 2 3 4 5) • (Extract '(1 2 3 4 5) 0 5) gives (1 2 3 4 5) • (Extract '(1 2 3 4 5) 5 6) gives NIL

  29. Programming Exercise 4 • Given a list of length n, we want to remove the cons cells from j to j+1, where j is from 0 to n-1. Define Splice (L i j) for the purpose. • (Splice '(1 2 3 4 5) 1 2) gives (1 4 5) • (Splice '(1 2 3 4 5) 1 1) gives (1 3 4 5) • (Splice '(1 2 3 4 5) 0 4) gives NIL • (Splice '(1 2 3 4 5) 0 5) gives NIL • (Splice '(1 2 3 4 5) 5 6) gives (1 2 3 4 5)

  30. Programming Exercise 5 • Write a merge sort function for a list of number.

  31. Programming Exercise 6 • Define a function called tree_all(T) , T is a binary tree as described in the tutorial, tree_all will return a list (A1,A2,A3,A4), which • A1 is the largest node of T’s left sub tree • A2 is the smallest node of T’s left sub tree • A3 is the largest node of T’s right sub tree • A4 is the smallest node of T’s right sub tree • Example • >(tree_all '(10 '(38 nil nil) '(20 nil '(11 nil nil)) • (38 38 20 11) • >(tree_all '(1 '(10 '(2 nil nil) '(3 nil nil)) '(320 '(24 nil nil) '(95 nil nil) )) • (10 2 320 24) (* (+ (2) (3)) (- (7) (8)))

  32. Programming Exercise 7 • Define a macro called run_prog(p x) which returns the value of (p x). • Example • > (run_prog '(+ x 2) 2) • 4 • > (run_prog '(+ x (- x 2)) 2) • 2

  33. Hints • Simplify testing.lisp • http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/1/tutorial1.html • Use do, nthcdr • Read next slide • http://en.literateprograms.org/Merge_sort_%28Lisp%29 • Use car, cdr, numberp and define a recursive function • Choose among let, lambda, eval, quote and list

  34. Hints Hints for Q4 > (setq l '(1 2 3 4 5 6 7 8 9 10 11 12)) (1 2 3 4 5 6 7 8 9 10 11 12) > (nthcdr 3 l) (4 5 6 7 8 9 10 11 12) > (nthcdr 3 l) (4 5 6 7 8 9 10 11 12) > (setf (cdr (nthcdr 3 l)) (nthcdr 6 l)) ;because clisp doesn't allow setf on nthcdr (7 8 9 10 11 12) > l (1 2 3 4 7 8 9 10 11 12)

  35. Suggested Readings • Common Lisp the Language, 2nd Edition • http://www.cs.cmu.edu/Groups/AI/html/cltl/cltl2.html • http://lib.store.yahoo.net/lib/paulgraham/onlisp.pdf • Common LISP Hints • http://www.carfield.com.hk/document/languages/common-lisp-tutorial.html

More Related