35
TUTORIAL 3 CSCI3230 (2014-2015 First Term) By Leo LIU ([email protected]) Paco WONG ([email protected]) CAO Qin ([email protected]) 1 Hands on Lab @SHB924

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

Embed Size (px)

Citation preview

Page 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

1

TUTORIAL 3CSCI3230 (2014-2015 First Term)

By Leo LIU ([email protected])

Paco WONG ([email protected])

CAO Qin ([email protected])

Hands on Lab@SHB924

Page 2: 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

2

Outline• Lispbox

• Setup• IDE• Example

• Short Review• Guided Practice • Programming Exercises

Page 3: 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

3

Lispbox

1. Download from http://common-lisp.net/project/lispbox/

2. Open Lispbox

Messages and instructions

User input and interpreter output

Page 4: 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

4

Hello World

Page 5: 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

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.7071067811865476D0CL-USER> "hello world""hello world"CL-USER>

Read

EvaluatePrint

Q: How is s-expression evaluated? [See Tutorial 1: Control the Flow of

Evaluation]

Page 6: 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

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

Page 7: 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

7

Example 1mysum.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 functionSumming 10 and 2.12

Page 8: 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

8

Example 2testing.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 function285

Page 9: 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

9

Try (sum_square)Click 3 to kill OR

Press key 3 to abort OR

Press key a to abort

Page 10: 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

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

Page 11: 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

11

SLIME• The Superior Lisp Interaction Mode for Emacs

Page 12: 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

12

REVIEW

Page 13: 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

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

Page 14: 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

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

Page 15: 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

15

Symbolic-Expression• An s-expression is defined recursively:

1. An atom is an s-expression

2. If s1, s2, …, sn are s-expressions, then so is the list (s1 s2 … sn).

Page 16: 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

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.

Page 17: 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

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)

Page 18: 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

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• GETRefer to the previous tutorials for details

Page 19: 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

19

GUIDED PRACTICE1. If-then-else

2. Recursion 1

3. Recursion 2

4. Prime Test

5. Tree Traversal

Page 20: 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

20

1. If-then-else

(defun isZero (n)

(if (= n 0)

(format t "~D is zero.~%" n)

(format t "~D is not zero.~%" n)))

Try this(isZero 1)(isZero 0)

Page 21: 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

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)

Page 22: 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

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>

Page 23: 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

23

4. Is Prime ?

Try this(isPrime 1)(isPrime 11)(isPrime -5)

Page 24: 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

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

Try this(printTree '(1 nil nil))(printTree '(2 '(1 nil nil) nil))(printTree '(2 '(1 nil nil) '(3 nil nil)))

Page 25: 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

25

PROGRAMMING EX1. 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!

Page 26: 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

26

Programming Exercise 1• Define a function sum(n) which returns the result of

1+2+3+4+5+6+…+n

Page 27: 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

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

Page 28: 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

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

Page 29: 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

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)

Page 30: 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

30

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

Page 31: 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

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

Page 32: 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

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

Page 33: 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

33

Hints

1. Simplify testing.lisp

2. http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/1/tutorial1.html

3. Use do, nthcdr

4. Read next slide

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

Page 34: 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

34

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