CS220 Programming Principles

32
CS220 Programming Principles 프프프프프프 프프 2002 프프프프 Class 6 프프

description

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. - PowerPoint PPT Presentation

Transcript of CS220 Programming Principles

Page 1: CS220 Programming Principles

CS220Programming Principles

프로그래밍의 이해2002 가을학기 Class 6

한 태숙

Page 2: CS220 Programming Principles

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)))))

Page 3: CS220 Programming Principles

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)))

Page 4: CS220 Programming Principles

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))

Page 5: CS220 Programming Principles

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

Page 6: CS220 Programming Principles

6

EQV?

(eqv? obj1 obj2) returns #t if obj1 and obj2 are – the empty list– pairs, vectors or strings that denote the same locatio

ns in the store– procedures whose location tags are equal

Page 7: CS220 Programming Principles

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 othe

r is an inexact number

Page 8: CS220 Programming Principles

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 i

s not– obj1 and obj2 are pairs, vectors, or strings that denot

e distinct locations– obj1 and obj2 are procedures that would behave diff

erently for some arguments

Page 9: CS220 Programming Principles

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

Page 10: CS220 Programming Principles

10

(eqv? ’(a) ’(a)) ==> unspecified

(eqv? ”a” ”a”) ==> unspecified

(eqv? ’(b) (cdr ’(a b))) ==> unspecified

(let ((x ’(a))) eqv? x x )) ==> #t

Page 11: CS220 Programming Principles

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

Page 12: CS220 Programming Principles

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

Page 13: CS220 Programming Principles

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

Page 14: CS220 Programming Principles

14

EQUAL?

Library procedure recursively compare the contents of pairs, vecto

rs, and strings, applying eqv? on other objects such as symbols and numbers.

Objects are generally equal? if the print the same.

Page 15: CS220 Programming Principles

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

Page 16: CS220 Programming Principles

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)))))

Page 17: CS220 Programming Principles

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)

Page 18: CS220 Programming Principles

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)

Page 19: CS220 Programming Principles

19

Differentiation

Numerical Computation(define (numerical-derivative f)

(define epsilon 0.0001)

(lambda (x)

(/ (- (f (+ x epsilon))

(f x))

epsilon)))

Page 20: CS220 Programming Principles

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)))

Page 21: CS220 Programming Principles

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))))

Page 22: CS220 Programming Principles

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?

Page 23: CS220 Programming Principles

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>

Page 24: CS220 Programming Principles

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))

Page 25: CS220 Programming Principles

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)))

Page 26: CS220 Programming Principles

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)

Page 27: CS220 Programming Principles

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))))

Page 28: CS220 Programming Principles

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))))

Page 29: CS220 Programming Principles

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)))))

Page 30: CS220 Programming Principles

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))

Page 31: CS220 Programming Principles

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)

Page 32: CS220 Programming Principles

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))))