Presentation is loading. Please wait.

Presentation is loading. Please wait.

TUTORIAL 3 CSCI3230 (2014-2015 First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on.

Similar presentations


Presentation on theme: "TUTORIAL 3 CSCI3230 (2014-2015 First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on."— Presentation transcript:

1 TUTORIAL 3 CSCI3230 (2014-2015 First Term) By Leo LIU (pfl@cse.cuhk.edu.hk) Paco WONG (pkwong@cse.cuhk.edu.hk) CAO Qin (qcao@cse.cuhk.edu.hk) 1 Hands on Lab @SHB924

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

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

4 Hello World 4

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 Evaluate Print 5 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") 6

7 Example 1 mysum.lisp (defun mysum (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" > ( mysum 10 2);Test your function Summing 10 and 2. 12 7

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 8

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

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 10

11 SLIME The Superior Lisp Interaction Mode for Emacs 11

12 REVIEW 12

13 Atom 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 ; Comment here 13

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 14

15 Symbolic-Expression An s-expression is defined recursively: 1. An atom is an s-expression 2. If s 1, s 2, …, s n are s-expressions, then so is the list (s 1 s 2 … s n ). 15

16 NIL and T 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. 16

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 evaluated to obtain the function arguments. Example 1 (+ 2 4) 17

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 18 Refer to the previous tutorials for details

19 GUIDED PRACTICE 1. If-then-else 2. Recursion 1 3. Recursion 2 4. Prime Test 5. Tree Traversal 19

20 1. If-then-else (defun isZero (n) (if (= n 0) (format t "~D is zero.~%" n) (format t "~D is not zero.~%" n))) 20 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)))) 21 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 ) ) ) 22

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

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

25 PROGRAMMING EX 1. Define a function 2. Define a recursive function and iterative function 3. Read a segment from a list 4. Remove a segment from a list 5. Sort a list 6. Traverse a tree 7. Use program as data 8. Propose an interesting question for yourself! 25

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

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

28 Programming Exercise 3 Write a function Extract (L i j) which extracts the i th to j th 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 28

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) 29

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

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) 31 (* (+ (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 32

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

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) 34

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 http://www.carfield.com.hk/document/languages/common-lisp- tutorial.html 35


Download ppt "TUTORIAL 3 CSCI3230 (2014-2015 First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on."

Similar presentations


Ads by Google