Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak."— Presentation transcript:

1 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

2 SJSU Dept. of Computer Science Spring 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)) parametersbody ((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)

3 SJSU Dept. of Computer Science Spring 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)

4 SJSU Dept. of Computer Science Spring 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.

5 SJSU Dept. of Computer Science Spring 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

6 SJSU Dept. of Computer Science Spring 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 | | 2 | |6 | 24 |120 120 > (untrace factorial) (factorial) > (factorial 5) 120

7 SJSU Dept. of Computer Science Spring 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)... ))))

8 SJSU Dept. of Computer Science Spring 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 argument approaches the base case (the empty list).

9 SJSU Dept. of Computer Science Spring 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)))

10 SJSU Dept. of Computer Science Spring 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 the matching 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 write remove-all ?

11 SJSU Dept. of Computer Science Spring 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)

12 SJSU Dept. of Computer Science Spring 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 the rest of the list.

13 SJSU Dept. of Computer Science Spring 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))) )))

14 SJSU Dept. of Computer Science Spring 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)

15 SJSU Dept. of Computer Science Spring 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.

16 SJSU Dept. of Computer Science Spring 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))) )))

17 SJSU Dept. of Computer Science Spring 2014: February 17 CS 152: Programming Language Paradigms © R. Mak 17 Assignment #2  Write some Scheme procedures.  Petite Chez Scheme: http://www.scheme.com/http://www.scheme.com/ Download and install the Windows or Mac version: http://www.scheme.com/download/ 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. _

18 SJSU Dept. of Computer Science Spring 2014: February 17 CS 152: Programming Language Paradigms © R. Mak 18 Scheme Documentation  Free online book on Scheme: http://www.scheme.com/tspl4/ http://www.scheme.com/tspl4/ Errata: http://www.scheme.com/tspl4-errata.htmlhttp://www.scheme.com/tspl4-errata.html  Chez Scheme User's Guide: http://www.scheme.com/csug8/ http://www.scheme.com/csug8/ Errata: http://www.scheme.com/csug8-errata.htmlhttp://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 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 http://mitpress.mit.edu/sicp/full-text/sicp/book/book.html

19 SJSU Dept. of Computer Science Spring 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 exp with respect to a given variable var.  We’ll start with four basic differentiation rules. _ (define (deriv exp var)... )

20 SJSU Dept. of Computer Science Spring 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:

21 SJSU Dept. of Computer Science Spring 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. (deriv '(* (* x y) (+ x 3)) 'x)  (+ (* x y) (* y (+ x 3)))

22 SJSU Dept. of Computer Science Spring 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.

23 SJSU Dept. of Computer Science Spring 2014: February 17 CS 152: Programming Language Paradigms © R. Mak 23 Symbolic Differentiation (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

24 SJSU Dept. of Computer Science Spring 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

25 SJSU Dept. of Computer Science Spring 2014: February 17 CS 152: Programming Language Paradigms © R. Mak 25 Symbolic Differentiation (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))) (deriv '(* x y) 'x)  (+ (* x 0) (* 1 y)) (deriv '(+ (* 3 x) 2) 'x)  (+ (+ (* 3 1) (* 0 x)) 0)


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

Similar presentations


Ads by Google