Com S 342 meeting -*- Outline -*- * Symbolic Data (2.3) symbols, like strings but have a fast equality test ** quotation (2.3.1) in English: "leave the room" in Scheme: the form (quote e), which can be abbreviated as 'e, suppresses evaluation of the expression e > (define my-int 3) > (sin my-int) > (quote (sin my-int)) > '(sin my-int) applying quotation to a name makes a symbol 'my-int versus my-int ** fast equality test > (eq? 'a 'a) #t > (eq? 'a 'b) #f > (eq? 3 3) #t > (eq? 5555555555555 5555555555555) #f > (eqv? 5555555555555 5555555555555) #t > (= 4 4.0) #t > (eqv? 4 4.0) #f > (eq? (list 1 2) (list 1 2)) #f > (eqv? (list 1 2) (list 1 2)) #f > (equal? (list 1 2) (list 1 2)) #t > (eq? (make-vector 1 2) (make-vector 1 2)) #f > (equal? (make-vector 1 2) (make-vector 1 2)) #t eq? is a fast equality test for symbols similar to == in Java, compares pointers for equality (object identity) note that the behavior of eq? on numbers and characters is is implementation dependent compare to the intern method of Java strings s.intern() == t.intern() iff s.equals(t) = is a test that comparison numbers for their value note that this does not distinguish inexact and exact numbers eqv? is a test that compares: - Booleans and characters for their value - exact numbers for their value using = - inexact numbers for their value using = - all other types using eq? note that eqv? distinguishes exact and inexact numbers equal? recursively compares the contents of pairs, vectors, and strings using eqv? on atomic objects objects are equal? if they print the same Write equal? (use versions of accumulate for vectors and strings) ** an example miniature expressions and differentiation, page 146 *** grammar ::= | | | ::= ::= ::= (+ ) ::= (* ) *** helping procedures numeric-literal? : (-> (expression) boolean) variable? : (-> (expression) boolean) same-variable? : (-> (variable variable) boolean) sum? : (-> (expression) boolean) make-sum : (-> (expression expression) sum) addend, augend : (-> (sum) expression) product? : (-> (expression) boolean) make-product : (-> (expression expression) product) multiplier, multiplicand : (-> (product) expression) *** derivative example **** in Scheme examples: (deriv (make-sum 'x 'y) 'x) ==> (+ 1 0) (deriv (make-product 'x (make-sum 'x 'y)) 'x) ==> (deriv '(* x (+ x y)) 'x) ==> (deriv '(+ (* x x) (+ x y)) 'x) ==> (define (deriv exp var) ;; TYPE: (-> (expression symbol) expression) (cond ((numeric-literal? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ...)) see expression.scm in this directory. **** in Java define an interface Expression, and classes that implement it: NumericLiteral, Variable, Sum, Product use the visitor pattern by adding to the interface Expression public Object visit(ExpressionVisitor v); and defining public interface ExpressionVisitor { public Object visitNumericLiteral(NumericLiteral n); public Object visitVariable(Variable v); public Object visitSum(Sum s); public Object visitProduct(Product p); } then code DerivVisitor as a class that implements ExpressionVisitor *** lambda calculus example Grammar: ::= | | ::= ::= (lambda () ) ::= ( ) Exercises for the discussion: count-varrefs, occurs-free-in?, substitute (ignore capture) See lambda-1-exp* in $PUB/lib