1 / 35

# Tutorial 3 - PowerPoint PPT Presentation

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Tutorial 3' - oki

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

• Lispbox

• Setup

• IDE

• Example

• Short Review

• Guided Practice

• Programming Exercises

• Open Lispbox

User input and interpreter output

Messages and instructions

LISP IDE

• A prompt for you to enter a LISP expression when CL started: CL-USER>

• Interactive: at any time you can try your expressions

CL-USER>(cos (/ pi 4))

0.7071067811865476D0

CL-USER>"hello world"

"hello world"

CL-USER>

Print

Evaluate

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

• Edit

• Emacs from Lispbox(Ctrl-h t for a tutorial)

• Any other text editors

• Save e.g., testing.lisp(or .cl)

• Compile (compile-file "testing.lisp")

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

#P"d:/Software/lispbox-0.7/mysum.lisp"

> (mysum10 2) ;Test your function

Summing 10 and 2.12

testing.lisp

(defun sum_square (n)

(let ((r 0))

(do ((i 1 (+ i 1)))

((= i n) r)

(setq r (+ r (* i i))))))

Lisp IDE

#P"d:/Software/lispbox-0.7/testing.lisp"

> (sum_square 10) ;Test your function

285

Try (sum_square)

Click 3to kill OR

Press key 3to abort OR

Press key a to abort

• 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

• The Superior Lisp Interaction Mode for Emacs

### Review

• 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

• 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

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

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

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

• 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

### Guided Practice

If-then-else

Recursion 1

Recursion 2

Prime Test

Tree Traversal

(defunisZero (n)

(if (= n 0)

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

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

Try this

(isZero 1)

(isZero 0)

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

;To demonstrate recursion - 1^3+2^3+3^3+4^3+...+n^3

(defun sum3 (n)

(if (< n 1)

0

(if (= n 1)

1

) ) )

Try this

(isPrime 1)

(isPrime 11)

(isPrime -5)

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

(if (not tree)

nil

(progn

(print num) (printTreeltree) (printTreertree)

))

num))

### 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!

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

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

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

• 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

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

• Write a merge sort function for a list of number.

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

• 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

• Simplify testing.lisp

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

• Use do, nthcdr

• 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

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)

• 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