1 / 32

CS220 Programming Principles

CS220 Programming Principles. 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙. Symbolic Data. A Symbol is a new primitive data type Value vs. Symbol (a b c d) (23 45 17) ((Norah 12)(Molly 9)(Anna 7)) (* (+ 23 45) (+ x 9)) (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))). Quote.

elon
Download Presentation

CS220 Programming Principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS220Programming Principles 프로그래밍의 이해 2002가을학기 Class 6 한 태숙

  2. Symbolic Data • A Symbol is a new primitive data type • Value vs. Symbol (a b c d) (23 45 17) ((Norah 12)(Molly 9)(Anna 7)) (* (+ 23 45) (+ x 9)) (define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))

  3. Quote • We create symbol with “quote” (define x 23) x ; ==> 23 (quote x) ; ==> x ’x (quote (a b c)) ’(a b c) (car ’(a b c)) (car (quote (a b c)))

  4. Symbol • Evaluate to? Print as? (define z ’y) z (+ x 3) (list + x 3) (list ’+ ’x ’3) ’(list + x 3) ’(2 a) ’(2 (b 3)) (car ’(a b c))

  5. EQ? EQV? EQUAL? • (eqv? obj1 obj2) returns #t if obj1 and obj2 are • both #t or both #f • both symbol and (string=? (symbol->string obj1) (symbol->string obj2)) => #t • both numbers, numerically equal, and either both exact or both inexact • both characters and the same character according to char=? procedure

  6. EQV? • (eqv? obj1 obj2) returns #t if obj1 and obj2 are • the empty list • pairs, vectors or strings that denote the same locations in the store • procedures whose location tags are equal

  7. Eqv? Returns #f • (eqv? obj1 obj2) returns #f if: • obj1 and obj2 are of different types • one of obj1 and obj2 is #t but the other is #f • obj1 and obj2 are symbols but (string=? (symbol->string obj1) (symbol->string obj2) =>#f • one of obj1 and obj2 is an exact number but the other is an inexact number

  8. Eqv? Returns #f (continue’d) • (eqv? obj1 obj2) returns #f if: • obj1 and obj2 are numbers(characters) for which the = (char=?) procedure returns #f • one of obj1 and obj2 is the empty list but the other is not • obj1 and obj2 are pairs, vectors, or strings that denote distinct locations • obj1 and obj2 are procedures that would behave differently for some arguments

  9. Examples of Eqv? (eqv? ’a ’a) ==> #t (eqv? ’a ’b) ==> #f (eqv? 2 2) ==> #t (eqv? ’() ’()) ==> #t (eqv? 10000 10000) ==> #t (eqv? (cons 1 2) (cons 1 2)) ==> #f (eqv? (lambda() 1) (lambda () 2)) ==> #f (eqv? #f ’nil) ==> #f (let ((p (lambda (x) x))) (eqv? p p)) ==> #t

  10. (eqv? ’(a) ’(a)) ==> unspecified (eqv? ”a” ”a”) ==> unspecified (eqv? ’(b) (cdr ’(a b))) ==> unspecified (let ((x ’(a))) eqv? x x )) ==> #t

  11. EQ? - similar to but finer than eqv? • guaranteed to have the same behavior on symbols, booleans, the empty list, pairs, procedures, and non-empty strings and vectors • may be implementation-dependent on numbers and characters and will return true only when eqv? would also return true • may behave differently from eqv? on empty vectors and empty strings

  12. Examples on Eq? (eq? ’a ’a) ==> #t (eq? ’(a) ’(a)) ==> unspecified (eq? (list ’a) (list ’a)) ==> #f (eq? ”a” ”a”) ==> unspecified (eq? ”” ””) ==> unspecified (eq? ’() ’()) ==> #t (eq? 2 2) ==> unspecified (eq? #\A #\A) ==> unspecified

  13. (eq? car car) ==> #t (let ((n (+ 2 3))) (eq? n n)) ==> unspecified (let ((x ’(a))) (eq? x x)) ==> #t (let ((x ’#())) (eq? x x)) ==> #t (let ((p (lambda (x) x))) (eq? p p)) ==> #t

  14. EQUAL? • Library procedure • recursively compare the contents of pairs, vectors, and strings, applying eqv? on other objects such as symbols and numbers. • Objects are generally equal? if the print the same.

  15. Examples on Equal? (equal? ’a ’a) ==> #t (equal? ’(a) ’(a)) ==> #t (equal? ’(a (b) c) ’(a (b) c)) ==> #t (equal? ”abc” ”abc”) ==> #t (equal? 2 2) ==> #t (equal? (make-vector 5 ’a) (make-vector 5 ’a)) ==> #t (equal? (lambda (x) x) (lambda (y) y)) ==> unspecified

  16. Memq - with eq? • (symbol , list ) -> #f if none is the same in list sublist with symbol otherwise (define (memq item x)(cond ((null? x) #f) ((eq? item (car x)) x) (else (memq item (cdr x)))))

  17. Exercise 2.53 (list ’a ’b ’c) (list (list ’george)) (cdr ’((x1 x2) (y1 y2))) (cadr ’((x1 x2) (y1 y2))) (pair? (cadr ’(a short list))) (memq ’red ’((red shoes) (blue socks))) (memq ’red ’(red shoes blue socks)) (car ’ ’abracadabra)

  18. Exercise 2.54 • Implement equal? (equal? ’(this is a list) ’(this is a list)) ==> #t (equal? ’(this is a list) ’(this (is a) list)) ==> #f Plan: both symbols and eq? symbols both lists and equal? (car a) (car b) and equal? (cdr a) (cdr b)

  19. Differentiation • Numerical Computation (define (numerical-derivative f) (define epsilon 0.0001) (lambda (x) (/ (- (f (+ x epsilon)) (f x)) epsilon)))

  20. Symbolic Differentiation (define (deriv exp var) (cond ((constant? exp)(make-constant 0)) ((variable? exp) (if (same-variable? exp var) (make-constant 1) (make-constant 0))) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var)))

  21. ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (multiplicand exp) (deriv (multiplier exp) var)))) (else (error ”unknown expression type” exp))))

  22. Math Expression Abstraction(I) (make-constant <c>) Construct constant <c> (constant? <e>) Is <e> a constant? (make-variable <v>) Construct a variable <v> (variable? <e>) Is <e> a variable? (same-variable <v1> <v2>) Are <v1> and <v2> same?

  23. Math Expression Abstraction(II) (make-sum <addend> <augend>) Construct sum (sum? <e>) Is <e> a sum? (addend <e>) Addend of sum <e> (augend <e>) Augend of sum <e> (make-product <multiplier> <multiplicand>) (product? <e>) Is <e> a product? (multiplier <e>) multiplier of product <e> (multiplicand <e>) multiplicand of product<e>

  24. Math Expression Implementation(I) • Represent (ax+b) with prefix notation such as (+ (* a x) b) (define (make-constant x) x) (define (constant? x) (number? x))

  25. Math Expression Implementation(II) (define (make-variable x) x) (define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2)))

  26. Math Expression Implementation(III) (define (make-sum a1 a2) (list ’+ a1 a2)) (define (sum? x) (and (pair? x) (eq? (car x) ’+))) (define (addend s) (cadr s) (define (augend s) (caddr s) (define (make-product m1 m2) (list ’* m1 m2)) (define (product? x) (and (pair? x) (eq? (car x) ’*))) (define (multiplier m) (cadr m) (define (multiplicand m) (caddr m)

  27. Reducing Math Exp Implementation (define (make-sum a1 a2) (cond ((and (constant? a1) (constant? a2)) (make-constant (+ a1 a2))) ((constant? a1) (if (= a1 0) a2 (list ’+ a1 a2))) ((constant? a2) (if (= a2 0) a1 (list ’+ a1 a2))) (else (list ’+ a1 a2))))

  28. (define (make-product m1 m2) (cond ((and (constant? m1) (constant? m2)) (make-constant (* m1 m2))) ((constant? m1) (cond ((= m1 0) (make-constant 0)) ((= m1 1) m2) (else (list ’* m1 m2)))) ((constant? m2) (cond ((= m2 0) (make-constant 0)) ((= m2 1) m1) (else (list ’* m1 m2)))) (else (list ’* m1 m2))))

  29. Adding Exponential Expression (define (deriv exp var) (cond ………. ((exponential? exp) (make-product (make-product (exponent exp) (make-exponential (base exp) (- (exponent exp) 1))) (deriv (base exp) var)))))

  30. (define (make-exponential b e) (cond ((= e 0) (make-constant 1)) ((= e 1) b) (else (list ’** b e)))) (define exponential? exp) (and (pair? exp) (eq? (car exp) ’**))) (define (base exp) (cadr exp)) (define (exponent exp) (caddr exp))

  31. Dotted Tail Notation • Exercise 2.20 (define (f x . y) <body> ) (f 1 2 3 4) in <body> x bound to 1 y bound to (2 3 4) (define (same-parity x . y) (same-parity 1 2 3 4 5 6 7) x==> 1 y==>(2 3 4 5 6 7)

  32. Implementation(II) of Math Exp • Using Variable # Terms -> (+ a b c) (define (make-sum a1 . a2) (cons ’+ (cons a1 a2))) (define (augend s) (if (null? (cdddr s)) (caddr s) (cons ’+ (cddr s)))) (define (multiplicand p) (if (null? (cdddr p)) (caddr p) (cons ’* (cddr p))))

More Related