CS 152 Programming Language Paradigms February 19 Class
CS 152: Programming Language Paradigms February 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Symbolic Differentiation o Recall these differentiation rules from freshman calculus: n For a constant c or a variable other than x: n Also: SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 2
Symbolic Differentiation, cont’d o For simplicity, the polynomial expressions and their derivatives will be written in Lisp’s prefix notation. n We’ll also limit the arithmetic operators to two arguments. o n 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))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 3
Symbolic Differentiation: Helper Procedures o A top-down approach! n Assume we already have the following procedures: (variable? e) Is e a variable? (same-variable? v 1 v 2) Are v 1 and v 2 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 a 1 a 2) Construct the sum of a 1 and a 2. (product? e) Is e a product? (multiplier e) Multiplier of the product e. (multiplicand e) Multiplicand of the product e. (make-product m 1 m 2) Construct the product of m 1 and m 2. SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 4
Symbolic Differentiation: Main Procedure (define (deriv exp var) deriv 1. lisp (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)) )) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 5
Symbolic Differentiation: Helper Procedures (define (variable? x) (symbol? x)) deriv 1. lisp (define (same-variable? v 1 v 2) (and (variable? v 1) (variable? v 2) (eq? v 1 v 2))) (define (make-sum a 1 a 2) (list '+ a 1 a 2)) (define (make-product m 1 m 2) (list '* m 1 m 2)) (define (sum? x) (and (pair? x) (eq? (car x) '+))) A sum is a list where the first element is the symbol + Example: (+ a b) (define (addend s) (cadr s)) (define (augend s) (caddr s)) (define (product? x) (and (pair? x) (eq? (car x) '*))) A product is a list where the first element is the symbol * Example: (* a b) (define (multiplier p) (cadr p)) (define (multiplicand p) (caddr p)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 6
Symbolic Differentiation: Examples (deriv 3 'x) 0 (deriv '(+ x 3) 'x) (+ 1 0) (deriv 'a 'x) 0 (deriv '(* 3 x) 'x) (+ (* 3 1) (* 0 x)) (deriv 'x 'x) 1 (deriv '(* x y) 'x) (+ (* x 0) (* 1 y)) (deriv '(+ (* 3 x) 2) 'x) (+ (+ (* 3 1) (* 0 x)) 0) (deriv '(* (* x y) (+ x 3)) 'x) (+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 7
Symbolic Differentiation: Simplifications o Can we simplify: n n o (+ e 0), (+ 0 e), (* 1 e), and (* e 1) each to just e ? (* e 0) and (* 0 e) to just 0 ? Start with: (define (make-sum a 1 a 2) (list '+ a 1 a 2)) (define (make-product m 1 m 2) (list '* m 1 m 2)) o We can perform the simplifications there by performing the additions by 0 and the multiplications by 0 and 1. _ SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 8
Symbolic Differentiation: Simplifications, cont’d (define (=number? exp num) (and (number? exp) (= exp num))) deriv 2. lisp (define (make-sum a 1 a 2) (cond ((=number? a 1 0) a 2) ((=number? a 2 0) a 1) ((and (number? a 1) (number? a 2)) (+ a 1 a 2)) (else (list '+ a 1 a 2)))) (define (make-product m 1 m 2) (cond ((or (=number? m 1 0) (=number? m 2 0)) 0) ((=number? m 1 1) m 2) ((=number? m 2 1) m 1) ((and (number? m 1) (number? m 2) (* m 1 m 2))) (else (list '* m 1 m 2)))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 9
Symbolic Differentiation: Examples (deriv '(+ x 3) 'x) 1 (deriv '(* 3 x) 'x) 3 (deriv '(* x y) 'x) y (deriv '(+ (* 3 x) 2) 'x) 3 (deriv '(* (* x y) (+ x 3)) 'x) (+ (* x y) (* y (+ x 3))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 10
Symbolic Differentiation: Exponentiation o What about exponentiation? n n Use symbol ^ for exponentiation. Example: (^ x 2) represents x 2 (define (exponentiation? x) (and (pair? x) (eq? (car x) '^))) deriv 3. lisp (define (base e) (cadr e)) (define (exponent e) (caddr e)) (define (make-exponentiation base exponent) (cond ((=number? exponent 0) 1) ((=number? exponent 1) base) ((and (number? base) (number? exponent)) (expt base exponent)) (else (list '^ base exponent)))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 11
Symbolic Differentiation: Exponentiation, cont’d (define (deriv exp var) (cond deriv 3. lisp ((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)))) ((exponentiation? exp) (make-product (exponent exp) (make-exponentiation (base exp) (make-sum (exponent exp) -1))) (deriv (base exp) var))) (else (error "unknown expression type -- DERIV" exp)) )) SJSU Dept. of Computer Science CS 152: Programming Language Paradigms 12 Spring 2014: February 19 © R. Mak
Symbolic Differentiation: Examples (deriv '(^ x 2) 'x) (* 2 x) (deriv '(* a (^ x 2)) 'x) (* a (* 2 x)) (deriv '(* 4 (^ x 3)) 'x) (* 4 (* 3 (^ x 2))) o We should simplify (* 4 (* 3 (^ x 2))) to (* 12 (^ x 2)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 13
Symbolic Differentiation: Simplifications (define (make-product m 1 m 2) (cond ((or (=number? m 1 0) (=number? m 2 0)) 0) ((=number? m 1 1) m 2) ((=number? m 2 1) m 1) ((and (number? m 1) (number? m 2) (* m 1 m 2))) (else (list '* m 1 m 2)))) From (* 4 (* 3 (^ x 2))) to (* 12 (^ x 2)) (define (make-product m 1 m 2) deriv 4. lisp (cond ((or (=number? m 1 0) (=number? m 2 0)) 0) ((=number? m 1 1) m 2) ((=number? m 2 1) m 1) ((and (number? m 1) (number? m 2)) (* m 1 m 2)) ((and (number? m 1) (product? m 2) (number? (cadr m 2))) (list '* (* m 1 (cadr m 2)) (caddr m 2))) (else (list '* m 1 m 2)))) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 14
Symbolic Differentiation: Simplifications, cont’d (deriv '(* 4 (^ x 3)) 'x) (* 12 (^ x 2)) (deriv '(+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7) 'x) (+ (+ (* a (* 5 (^ x 4))) (* b (* 4 (^ x 3)))) (* (6 (^ x 2)))) (* 12 x)) 3) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 15
Symbolic Differentiation: Enhancements o Further enhancements: n Allow the + operator to have more than two operands. (deriv '(+ (* a (^ x 5)) (* b (^ x 4)) (* 2 (^ x 3)) (* 6 (^ x 2)) (* 3 x) 7) 'x) n Use infix notation. (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) o References n n http: //mitpress. mit. edu/sicp/full-text/sicp/book/node 39. html http: //www. billthelizard. com/2012/04/sicp-256 -258 -symbolic-differentiation. html SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 16
Scheme and f(x) and f '(x) o For the polynomial function we have o For a given set of coefficient values (a, b, etc. ), we can evaluate both f(x) and f '(x) for some value x. n n o Let a = 1 and b = 1 Then f(0) = 7 and f(1) = 20 f '(0) = 3 and f '(1) = 30 For any polynomial function f such as the ones we’ve been working with and a set of coefficient values: n Can we generate Scheme procedures that evaluate f(x) and f '(x) for any value x SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 17
Scheme and f(x) and f '(x) , cont’d o First rewrite the polynomial expression in Scheme: (define f '(+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) f. lisp 7)) o or simply: But what we really want is: (lambda (x) (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) SJSU Dept. of Computer Science Spring 2014: February 19 (lambda (x) f) CS 152: Programming Language Paradigms © R. Mak 18
Scheme and f(x) and f '(x) , cont’d (lambda (x) (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) o (lambda (x) f) We can generate the above as a list expression: (list 'lambda '(x) f) (lambda (x) (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 19
Scheme and f(x) and f '(x) , cont’d (list 'lambda '(x) f) (lambda (x) (+ (+ (+ (* a (^ x 5)) (* b (^ x 4))) (* 2 (^ x 3))) (* 6 (^ x 2))) (* 3 x)) 7)) o But this is just a list expression generated with the built-in list procedure! (list 'a 'b 'c) (a b c) o To generate a procedure object, we need to tell Scheme to evaluate the generated list expression: (eval (list 'lambda '(x) f)) #<procedure> SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 20
Scheme and f(x) and f '(x) , cont’d (eval (list 'lambda '(x) f)) #<procedure> o Now we can generate a Scheme procedure from any polynomial expression f in terms of x : f. lisp (define make-proc (lambda (f) (eval (list 'lambda '(x) f)) )) (make-proc f) #<procedure> SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 21
Scheme and f(x) and f '(x) , cont’d o Back to our original problem: n n Let a = 1 and b = 1 Then f(0) = 7 and f(1) = 20 f '(0) = 3 and f '(1) = 30 (define ^ expt) (^ 4 2) 16 f. lisp (define a 1) (define b 1) ((make-proc f) 0) 7 ((make-proc f) 1) 20 ((make-proc (deriv f 'x)) 0) 3 ((make-proc (deriv f 'x)) 1) 30 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 22
eval o Procedure evaluates its list argument. n Just like in the Scheme command interpreter’s read-eval-print loop. (+ 2 3) 5 '(+ 2 3) (eval '(+ 2 3)) 5 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 23
apply o Scheme procedure apply allows you to apply a procedure of k parameters to a list of k items. (+ 1 2 3) 6 (apply + '(1 2 3)) 6 (apply (lambda (a b c) (* (+ a b) c)) '(1 2 4)) 12 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 24
Metalinguistic Power o Metalinguistic power gives Scheme the capability to n n Dynamically build, manipulate, and transform a list of symbols at runtime. And then evaluate the resulting list. (cons 'lambda (list 'a 'b) '(+ a b))) (lambda (a b) (+ a b)) (eval (cons 'lambda (list 'a 'b) '(+ a b)))) #<procedure> ((eval (cons 'lambda (list 'a 'b) '(+ a b)))) 2 3) 5 (apply (eval (cons 'lambda (list 'a 'b) '(+ a b)))) '(2 3)) 5 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 25
Flat Recursion o In our recursive procedures so far with a list argument: n n o This is known as flat recursion. n o We operated on the car of the list. We made a recursive call on the cdr of the list. The procedure operates on the top-level elements of the argument list. Deep recursion operates also on the nested sublists of the argument list. _ SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 26
Flat Recursion Example: append o Recursive procedure append takes two argument lists and appends the first list to the head of the second list. (append '(1 2 3) '(3 4)) '(1 2 3 3 4) n o One element at a time, cons the tail element of the first list into the head of the second list. What is the base case? n If the first list is empty, simply return the second list. (if (null? lst 1) lst 2 SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 27
Flat Recursion Example: append cont’d o Think recursively! n Assume that append can process the rest of the first list. o n In other words, assume that the rest of the first list has already been appended to the head of the second list. Then you only have to cons the head of the first list into a recursive call that process the rest of the list. (define append (lambda (lst 1 lst 2) (if (null? lst 1) lst 2 (cons (car lst 1) (append (cdr lst 1) lst 2))) )) Trace SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 28
Mutual Recursion o Two recursive procedures can be mutually recursive. n Example (define even? (lambda (n) (if (zero? n) #t (odd? (sub 1 n))) )) n (define odd? (lambda (n) (if (zero? n) #f (even? (sub 1 n))) )) Trace both procedures to verify that they can really work together! _ SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 29
Deep Recursion o Flatly recursive procedure remove-top removes all top-level occurrences of an item from a list: (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)) 4)) (1 3 (1 2)) 4) o Deeply recursive procedure remove-all removes all occurrences of an item from a list. n No matter how deeply nested. SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 30
Deep Recursion Example: remove-all (define remove-top (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove item (cdr lst))) (else (cons (car lst) (remove 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)) 4)) (1 3 (1)) 4) SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 31
Flat vs. Deep Recursion o Flat recursion n o Apply the recursive call only to the cdr of the list argument. Deep recursion n Apply the recursive call also to the car of the list argument if the car is a nested sublist. _ SJSU Dept. of Computer Science Spring 2014: February 19 CS 152: Programming Language Paradigms © R. Mak 32
- Slides: 32