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

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

Upload: suzan-glenn

Post on 28-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

CS 152: Programming Language Paradigms

February 24 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

2

Assignment #3

You are provided with Scheme code that differentiates polynomial expressions using infix notation.

Write two additional procedures evaluate and evaluate-deriv that each takes two parameters, f and x. f is a list that represents the function’s polynomial expression

in infix notation. x is the value of x to evaluate f or f ', respectively.

_

312645)7362( 2342345 xxbxaxxxxbxaxdx

d

(deriv '(a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7) 'x) (5 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3)

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

3

Assignment #3, cont’d

The coefficient values can be defined beforehand. Example: (define a 1)

Your procedures must work at least with the following values for f, a, and b, and x bound to 0 and then to 1.

Of course, you should test with other values.

(define f '(a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7))(deriv f 'x) (5 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3)

(define a 1)(define b 1)

(evaluate f 0) 7(evaluate f 1) 20

(evaluate-deriv f 0) 3(evaluate-deriv f 1) 30

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

4

Assignment #3, cont’d

Tip: Write helper procedures to convert the polynomial expression from infix to prefix notation.

Email a zip file to [email protected] containing: Text files containing your new Scheme procedures. Text files containing output from the above example values for

f, a, b, and x, and other test values. A short report (a few paragraphs) that briefly explains your

code design. Name your zip file after your team name,

such as SuperCoders.zip. Your email subject line should be

CS 152 Assignment #3, team name CC all your team members.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

5

Flat Recursion Example: remove-top Flatly recursive procedure remove-top removes

all top-level occurrences of an item from a list:

Deeply recursive procedure remove-all removes all occurrences of an item from a list. No matter how deeply nested.

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

(remove-top 2 '(1 2 3 (1 2 3 (1 2)) 4)) (1 3 (1 2 3 (1 2)) 4)

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

6

Deep Recursion Example: remove-all(define remove-top (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove-top item (cdr lst))) (else (cons (car lst) (remove-top item (cdr lst)))))))

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

(remove-all 2 '(1 2 3 (1 2 3 (1 2)) 4)) (1 3 (1 3 (1)) 4)

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

7

Flat vs. Deep Recursion

Flat recursion Apply the recursive call only to the cdr of the list argument.

Deep recursion Also apply the recursive call to the car of the list argument

if the car is a nested sublist._

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

8

Deep Recursion Example: flatten

Deeply recursive procedure flatten brings all the elements of a list up to the top level. No matter how deeply nested the elements are.

(define flatten (lambda (lst) (cond ((null? lst) '()) ((pair? (car lst)) (append (flatten (car lst)) (flatten (cdr lst)))) (else (cons (car lst) (flatten (cdr lst)))))))

(flatten '(1 2 (3 (4 (5 6)) 7 (((8)))) 9)) (1 2 3 4 5 6 7 8 9)

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

9

Deep Recursion Example: remove-leftmost

Deeply recursive procedure remove-leftmost removes the leftmost occurrence of an item from the argument list. No matter how deeply nested the item is.

What is the extra challenge? The leftmost occurrence can be in

either the car or the cdr of the argument list._

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

10

Deep Recursion Example: remove-leftmost

We need a helper procedure member-all? that determines whether or not an item occurs in a list, no matter how deeply nested.

Procedure remove-leftmost uses it to check the car of its argument list. If the item is in the car of the list, it recursively removes the item

from the car of the list and cons’s the result with the cdr of the list. If the item is not in the car of the list, it cons’s the car with the

result from recursively removing the item from the cdr of the list.

(define remove-leftmost (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) ((and (pair? (car lst)) (member-all? item (car lst))) (cons (remove-leftmost item (car lst)) (cdr lst))) (else (cons (car lst) (remove-leftmost item (cdr lst)))))))

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

11

Deep Recursion Example: member-all?

Deeply recursive procedure member-all? checks both the car (if necessary) and the cdr of its argument list.

(define member-all? (lambda (item lst) (if (null? lst) #f (or (equal? item (car lst)) (and (not (pair? (car lst))) (member-all? item (cdr lst))) (and (pair? (car lst)) (or (member-all? item (car lst)) (member-all? item (cdr lst))))))))

(member-all? 2 '(1 2 3)) #t(member-all? 2 '(1 (2) 3)) #t(member-all? 2 '((1 2) 2 3)) #t(member-all? 2 '((1 9) 9 3)) #f

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

12

Deep Recursion Example: remove-leftmost

(define remove-leftmost (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) ((and (pair? (car lst)) (member-all? item (car lst))) (cons (remove-leftmost item (car lst)) (cdr lst))) (else (cons (car lst) (remove-leftmost item (cdr lst)))))))

(remove-leftmost 2 '(1 2 3)) (1 3)(remove-leftmost 2 '(1 (2) 3)) (1 () 3)(remove-leftmost 2 '((1 2) 2 3)) ((1) 2 3)(remove-leftmost 2 '((1 9) 9 3)) ((1 9) 9 3)

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

13

Recursion vs. Iteration

A recursive procedure requires a runtime stack to hold “pending” results during recursive calls. Example: A recursive definition of the factorial function.

(define factorial (lambda (n) (if (zero? n) 1 (* n (factorial (sub1 n))))))

(trace factorial)

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

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

14

Iterative Factorial

Rewrite the procedure so that its computation proceeds as an iterative process rather then a recursive one. An extra argument nfact holds the intermediate results.

nfact must be initialized to 1.

Pending results no longer need to be kept on the runtime stack.

This is an example of tail recursion.

(define iter-factorial (lambda (n nfact) (if (zero? n) nfact (iter-factorial (sub1 n) (* n nfact)))))

(trace iter-factorial)

> (iter-factorial 5 1)|(iter-factorial 5 1)|(iter-factorial 4 5)|(iter-factorial 3 20)|(iter-factorial 2 60)|(iter-factorial 1 120)|(iter-factorial 0 120)|120120

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

15

Tail Recursion

In procedure factorial The result from each recursive call is multiplied by n. Therefore, a call to factorial cannot return a result

until the recursive call returns its result. In procedure iter-factorial

The result from a recursive call is simply returned. Therefore, each call can return immediately.

Scheme automatically converts tail recursion to iteration. This optimization can greatly improve runtime efficiency.

(define iter-factorial (lambda (n nfact) (if (zero? n) nfact (iter-factorial (sub1 n) (* n nfact)))))

(define factorial (lambda (n) (if (zero? n) 1 (* n (factorial (sub1 n))))))

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

16

Tail Recursion Example: Fibonacci

Why is this recursive procedure fib to compute Fibonacci numbers so highly inefficient?

(define fib (lambda (n) (if (< n 2) n (+ (fib (- n 2)) (fib (- n 1))))))

(trace fib)

> (fib 5)|(fib 5)| (fib 4)| |(fib 3)| | (fib 2)| | |(fib 1)| | |1| | |(fib 0)| | |0| | 1| | (fib 1)| | 1| |2| |(fib 2)| | (fib 1)| | 1| | (fib 0)| | 0| |1| 3| (fib 3)| |(fib 2)| | (fib 1)| | 1| | (fib 0)| | 0| |1| |(fib 1)| |1| 2|55

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

17

Tail Recursion Example: Fibonacci

An iterative version Extra arguments fib1 and fib2 must be initialized to 0 and 1,

respectively.

(define iter-fib (lambda (n fib1 fib2) (if (= n 1) fib2 (iter-fib (sub1 n) fib2 (+ fib1 fib2)))))

(trace iter-fib)

> (iter-fib 10 0 1)|(iter-fib 10 0 1)|(iter-fib 9 1 1)|(iter-fib 8 1 2)|(iter-fib 7 2 3)|(iter-fib 6 3 5)|(iter-fib 5 5 8)|(iter-fib 4 8 13)|(iter-fib 3 13 21)|(iter-fib 2 21 34)|(iter-fib 1 34 55)|5555

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

18

Scope

In this lambda expression

the variables x and y in the body (+ x y) are lexically bound (AKA lambda bound) since they are in the parameter list of the lambda expression.

In this lambda expression

the variables x and y in the body are bound, but variables a and b are free.

In both these examples, the scope of x and y is the lambda expression.

(lambda (x y) (+ x y)

(lambda (x y) (+ x y a b)

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

19

Scope, cont’d

Scheme is statically scoped (AKA lexically scoped). The scope of a variable is determined

by where the variable is declared in the source code . If a variable appears in the parameter list of a lambda expression,

then its scope is the body of that lambda expression.

Nested scopes: ((lambda (x) ((lambda (y) (- x y)) 15)) 20) 5

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

20

Local Bindings with let

Scheme has the special form let to create scopes and to do local bindings of variables.

Examples: (let ((a 2) (b 3)) (+ a b)) 5

(let ((add2 (lambda (x) (+ x 2))) (b 10)) (- b (add2 b))) -2

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

21

Local Bindings with let, cont’d

With let, we can rewrite procedure remove-leftmost without needing the member-all? helper procedure.

(define remove-leftmost-let (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) ((pair? (car lst)) (let ((remlst (remove-leftmost-let item (car lst)))) (cons remlst (if (equal? remlst (car lst)) (remove-leftmost-let item (cdr lst)) (cdr lst))))) (else (cons (car lst) (remove-leftmost-let item (cdr lst)))))))

(remove-leftmost-let 2 '(1 2 3)) (1 3)(remove-leftmost-let 2 '(1 (2) 3)) (1 () 3)(remove-leftmost-let 2 '((1 2) 2 3)) ((1) 2 3)(remove-leftmost-let 2 '((1 9) 9 3)) ((1 9) 9 3)

Explain this equal? test.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

22

Local Bindings with letrec

This won’t work:

In the recursive call, factorial-let is unbound. The procedure name wasn’t bound outside of the let expression.

Use letrec instead

(let ((factorial-let (lambda (n) (if (zero? n) 1 (* n (factorial-let (sub1 n))))))) (factorial-let 5))

(letrec ((factorial-let (lambda (n) (if (zero? n) 1 (* n (factorial-let (sub1 n))))))) (factorial-let 5)) 120

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

23

Local Bindings with letrec, cont’d

Use letrec to define an iterative factorial procedure that only requires one argument. The two-argument procedure is

nested inside the one-argument procedure. The one-argument procedure is guaranteed to initialize

the second argument of the two-argument procedure to 1.

(define iter-factorial-letrec (lambda (n) (letrec ((iter-fact (lambda (k kfact) (if (zero? k) kfact (iter-fact (sub1 k) (* k kfact)))))) (iter-fact n 1))))

(iter-factorial-letrec 5) 120

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

24

Closures

A lambda expression is a closure that consists of Its list of parameters. Its body. The runtime environment in which any free variables in its body

are bound at the time the lambda expression is evaluated. Recall that evaluating a lambda expression creates a procedure.

(define addb (let ((b 100)) (lambda (x) (+ x b))))

(let ((b 10)) (addb 25)) 125

The let creates a runtime environment where b is bound to 100. This environment is in effect when the lambda expression is evaluated, andit is a part of the lambda expression’s closure.

When addb is applied to argument 25, it looks within its closure to get the value 100 for the free variable b in its body.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

25

Closures, cont’d

(let ((b 2)) (let ((add2 (lambda (x) (+ x b))) (b 5)) (+ b (add2 b)))) 12

The outer let creates a runtime environment where b is bound to 2. This environment will be in effect when the lambda expression is evaluated, andit is a part of the lambda expression’s closure.

The inner let creates a nested environment whereb is bound to 5. In the expression (+ b (add2 b)),both occurrences of b are each bound to 5.But the free variable b in the lambda expressionis bound to 2.

Therefore, (+ 5 (+ 5 2)) 12.

SJSU Dept. of Computer ScienceSpring 2014: February 24

CS 152: Programming Language Paradigms© R. Mak

26

Closures, cont’d

Closure is a key feature of Lisp and Scheme.

Are closures used in other languages? Java? C++? Python?

Reference: http://en.wikipedia.org/wiki/Closure_%28computer_science%29