cs 152: programming language paradigms february 17 class meeting department of computer science san...

25
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu /~mak

Upload: vivian-oneal

Post on 13-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

CS 152: Programming Language Paradigms

February 17 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

2

Defining Procedures

Scheme uses a lambda expression to create a procedure object. Example:

Apply a lambda expression to some arguments. Example:

Bind the lambda expression to a symbol in order to reuse the procedure. Example:

(lambda (item lst) (cons item lst))

parameters body

((lambda (item lst) (cons item lst)) 'x '(1 2 3)) (x 1 2 3)

(define add-item (lambda (item lst) (cons item lst)))

(add-item 'x '(1 2 3)) (x 1 2 3)

Page 3: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

3

Defining Procedures

Some syntactic sugar:

Instead of:

Use:

(define add-item (lambda (item lst) (cons item lst)))

(define (add-item2 item lst) (cons item lst))

(add-item2 'x '(1 2 3)) (x 1 2 3)

Page 4: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

4

Conditional Expressions

Scheme as a cond expression (short for conditional) and an if expression. Examples:

(define type-of (lambda (item) (cond ((pair? item) 'pair) ((null? item) 'empty-list) ((number? item) 'number) ((symbol? item) 'symbol) (else 'some-other-type))))

(type-of 123) number(type-of +) some-other-type

(define car-if-pair (lambda (item) (if (pair? item) (car item) 'not-a-pair)))

(car-if-pair '(one two)) one(car-if-pair '()) not-a-pair

The else clause is optional.

The alternative (else) clause is optional.

Page 5: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

5

Recursion

A functional language like Scheme forces us to think recursively. Example:

What is the simplest case, known as the base case or terminating condition? n equals 0: Just return 1.

Each recursive call passes an argument value that must be closer to the base case. The argument is decremented by 1 each time,

so eventually it will be 0.

(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1))))))

(factorial 5) 120

Page 6: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

6

The trace Procedure

An important Scheme debugging aid.

> (trace factorial)(factorial)> (factorial 5)|(factorial 5)| (factorial 4)| |(factorial 3)| | (factorial 2)| | |(factorial 1)| | | (factorial 0)| | | 1| | |1| | 2| |6| 24|120120> (untrace factorial)(factorial)> (factorial 5)120

Page 7: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

7

Recursion Example: member?

Write a predicate procedure member? that takes two arguments, an item and a list, and returns #t if the item is equal? to a top-level item in the list, else returns #f. Examples:

What is the base case? The list is empty.

(member? 'cat '(dog horse cat pig)) #t(member? 'cat '(dog (horse cat) pig)) #f

(define member? (lambda (item lst) ... ))

(define member? (lambda (item lst) (cond ((null? lst) #f) ... ))))

Page 8: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

8

Recursion Example: member? cont’d

If the list is not empty, what is the easiest comparison? Compare item to (car lst).

Else, the item might be found in the rest of the list, so recursively call member? on the rest of the list.

(define member? (lambda (item lst) (cond ((null? lst) #f) ((equal? item (car lst)) #t) ... ))))

(define member? (lambda (item lst) (cond ((null? lst) #f) ((equal? item (car lst)) #t) (else (member? item (cdr lst))))))

The value of the list argumentapproaches thebase case(the empty list).

Page 9: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

9

Recursion Example: remove-first

Write a procedure remove-first that removes the first top-level occurrence of an item from a list. Examples:

What is the base case? The empty list.

What is the easiest comparison? Compare the item against the car of the list. What should you return if this comparison is true? What should you return if this comparison is false?

(remove-first 'cat '(dog horse cat pig cat mouse)) (dog horse pig cat mouse)(remove-first 'cat '()) ()(remove-first '(1 2) '(1 2 (1 2) ((1 2)))) (1 2 ((1 2)))

Page 10: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

10

Recursion Example: remove-first cont’d

(define remove-first (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) (else (cons (car lst) (remove-first item (cdr lst)))))))

Base case

Remove thematching first item.

Put back a non-matching first item.

Think recursively! Assume that remove-first can process the rest of the list. Then just check if the first item of the list is a match.

If the first item matches, return the rest of the list. If the first item doesn’t match, put it back by cons’ing it

into a recursive call that processes the rest of the list.

Can you writeremove-all ?

Page 11: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

11

Recursion Example: swapper

Write a procedure swapper that takes three arguments: item x item y list lst

Scan lst and Replace each top-level occurrence of x with y. Replace each top-level occurrence of y with x.

(swapper 'cat 'dog '(my dog is smarter than my cat)) (my cat is smarter than my dog)(swapper 'a 'b '(a b (a b) b a)) (b a (a b) a b)

Page 12: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

12

Recursion Example: swapper cont’d

Think recursively! Assume that swapper can process the rest of the list. Then you only have to deal with the first item of the list. cons into a recursive call that processes the rest of the list:

Either x replaced by y, or y replaced by x, or the unchanged first element.

(define swapper (lambda (x y lst) (cond ((null? lst) '()) ((equal? x (car lst)) (cons y (swapper x y (cdr lst)))) ((equal? y (car lst)) (cons x (swapper x y (cdr lst)))) (else (cons (car lst) (swapper x y (cdr lst)))))))

Base case

Replace x with y.

Replace y with x.

No replacement.Process therest of the list.

Page 13: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

13

Recursion Example: swapper cont’d

Can we remove the duplicated code?

(define swapper (lambda (x y lst) (cond ((null? lst) '()) ((equal? x (car lst)) (cons y (swapper x y (cdr lst)))) ((equal? y (car lst)) (cons x (swapper x y (cdr lst)))) (else (cons (car lst) (swapper x y (cdr lst)))))))

(define swapper2 (lambda (x y lst) (if (null? lst) '() (cons (cond ((equal? x (car lst)) y) ((equal? y (car lst)) x) (else (car lst))) (swapper x y (cdr lst))))))

Page 14: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

14

Recursion Example: unique

Write a procedure unique that takes a list and removes all duplicate items from the list.

(unique '(a b b c)) (a b c)(unique '(a (b c) d e a (b c) d (b (c)) e f)) (a (b c) d (b (c)) e f)

Page 15: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

15

Recursion Example: unique cont’d

Think recursively! Assume that unique can process the rest of the list. Then you only have to deal with the first item of the list. Is the first item of the list already a member

of the processed rest of the list? If no, cons it into a recursive call

that processes the rest of the list. If yes, then don’t cons but just make

the recursive call that processes the rest of the list.

(define unique (lambda (lst) (cond ((null? lst) '()) ((not (member? (car lst) (unique (cdr lst)))) (cons (car lst) (unique (cdr lst)))) (else (unique (cdr lst))))))

Use member?as a helper function.

Page 16: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

16

Recursion Example: unique cont’d

This code is inefficient because the second conditional clause has to make two identical recursive calls.

Can you write a new version of unique that doesn’t require making this double recursive call? Hint: Use a helper function that has a second list argument

into which you put the unique members._

(define unique (lambda (lst) (cond ((null? lst) '()) ((not (member? (car lst) (unique (cdr lst)))) (cons (car lst) (unique (cdr lst)))) (else (unique (cdr lst))))))

Page 17: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

17

Assignment #2

Write some Scheme procedures.

Petite Chez Scheme: http://www.scheme.com/ Download and install the Windows or Mac version:

http://www.scheme.com/download/ The free version includes the interpreter only but not the

compiler. You'll only need the interpreter.

Also download and install Chez Scheme’s Software Widget Library (SWL) It only runs with the 32-bit non-threaded version

of Petite Chez Scheme. So make sure you download and install that version before attempting to install SWL._

Page 18: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

18

Scheme Documentation

Free online book on Scheme: http://www.scheme.com/tspl4/ Errata: http://www.scheme.com/tspl4-errata.html

Chez Scheme User's Guide: http://www.scheme.com/csug8/ Errata: http://www.scheme.com/csug8-errata.html

A very good tutorial on Scheme: http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-1.html

The MIT textbook on Scheme: http://mitpress.mit.edu/sicp/full-text/sicp/book/book.html

Page 19: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

19

Manipulating Symbols

Lisp is well-suited for manipulating symbols. People tend to think more in terms of symbols

rather than in numbers.

Write a Scheme procedure deriv to perform symbolic differentiation of a polynomial expression expwith respect to a given variable var.

We’ll start with four basic differentiation rules._

(define (deriv exp var) ...)

Page 20: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

20

Symbolic Differentiation

Recall these differentiation rules from freshman calculus: For a constant c or a variable other than x:

Also:

0dx

dc

1dx

dx

dx

dv

dx

du

dx

vud

)(

dx

duv

dx

dvu

dx

uvd )(

Page 21: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

21

Symbolic Differentiation

For simplicity, the polynomial expressions and their derivatives will be written in Lisp’s prefix notation. We’ll also limit the arithmetic operators to two arguments.

Example: (+ (+ 1 2) 3)and not: (+ 1 2 3)

Example: Find the derivative of xy(x + 3) with respect to x.

)()3()3(3 xy

dx

dxx

dx

dxyxxy

dx

d

)3( xyxy

(deriv '(* (* x y) (+ x 3)) 'x) (+ (* x y) (* y (+ x 3)))

Page 22: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

22

Symbolic Differentiation

A top-down approach! Assume we already have the following procedures:

(variable? e) Is e a variable?

(same-variable? v1 v2) Are v1 and v2 the same variable?

(sum? e) Is e a sum?

(addend e) Addend of the sum e.

(augend e) Augend of the sum e.

(make-sum a1 a2) Construct the sum of a1 and a2.

(product? e) Is e a product?

(multiplier e) Multiplier of the product e.

(multiplicand e) Multiplicand of the product e.

(make-product m1 m2) Construct the product of m1 and m2.

Page 23: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

23

Symbolic Differentiation

0dx

dc1

dx

dx

dx

dv

dx

du

dx

vud

)(

dx

duv

dx

dvu

dx

uvd )(

(define (deriv exp var) (cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) (else (error "unknown expression type -- DERIV" exp))))

deriv1.lisp

Page 24: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

24

Symbolic Differentiation

(define (variable? x) (symbol? x))

(define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2)))

(define (make-sum a1 a2) (list '+ a1 a2))(define (make-product m1 m2) (list '* m1 m2))

(define (sum? x) (and (pair? x) (eq? (car x) '+)))

(define (addend s) (cadr s))(define (augend s) (caddr s))

(define (product? x) (and (pair? x) (eq? (car x) '*)))

(define (multiplier p) (cadr p))(define (multiplicand p) (caddr p))

A sum is a list where the first element is the symbol +Example: (+ a b)

A product is a list where the first element is the symbol * Example: (* a b)

deriv1.lisp

Page 25: CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer ScienceSpring 2014: February 17

CS 152: Programming Language Paradigms© R. Mak

25

Symbolic Differentiation

03 dx

d

0adx

d

1xdx

d

1)3( xdx

d

3)3( xdx

d

3)23( xdx

d

(deriv 3 'x) 0

(deriv 'a 'x) 0

(deriv 'x 'x) 1

(deriv '(+ x 3) 'x) (+ 1 0)

(deriv '(* 3 x) 'x) (+ (* 3 1) (* 0 x))

(deriv '(* (* x y) (+ x 3)) 'x) (+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3)))

yxydx

d)( (deriv '(* x y) 'x)

(+ (* x 0) (* 1 y))

)3()3( xyxyxxydx

d

(deriv '(+ (* 3 x) 2) 'x) (+ (+ (* 3 1) (* 0 x)) 0)