Presentation Description

192 Views

Download Presentation
## Primitive Data Types

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Primitive Data Types**• Symbol: any sequence of characters except ()[]{};,”`’#\ • Number • signed integers • signed real numbers • bignums (large integers) • rationals • exponential notation for reals • complex numbers • Boolean constants: #t #f**Naming**(define pi 3.14159) ;; special form (define radius 10) (* pi (* radius radius)) 314.159 (define circumference (* 2 pi radius)) circumference 62.8318 (define (circumference r) ;; redefinition (* 2 pi r)) (circumference 20)**Example of Evaluation**(* (+ 2 (* 4 6)) (+ 3 5 7)) 390 15 26 * 7 + 5 3 + 2 24 * 6 4**Rules of Evaluation**• The value of a numeral is itself. • The value of a primitive operation is a pointer to the internal machine instructions to accomplish it. • The value of a name is the value associated with that name in an environment. • The value of a combination is obtained by: • Evaluating the sub-expressions in any order. • Applying the value of the operator sub-expression to the values of the other sub-expressions, where applying a compound procedure means evaluating the body of the procedure with each formal parameter replaced by is corresponding value.**Procedure Definition**(define (square x) (* x x)) (square 21) (square (+ 2 5)) (square (square 3)) (define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 3 4) (define (f a) (sum-of-squares (+ a 1) (* a 2))) (f 5)**Substitution Model**(f 5) ;; (sum-of-squares (+ a 1) (* a 2)) ;: (sum-of-squares (+ 5 1) (* 5 2)) ;: (+ (square 6) (square 10)) ;: (+ (* 6 6) (* 10 10)) ;: (+ 36 100) 136 • NOT how the interpreter really works! • A way to start thinking about the process • Applicative order of evaluation: eval-and-apply**Normal Order Evaluation**(f 5) (sum-of-squares (+ 5 1) (* 5 2)) (+ (square (+ 5 1))(square (* 5 2))) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) (+ (* 6 6) (* 10 10)) (+ 36 100) 136**Evaluation: An Example**(define (p) (p)) (define (test x y) (if (= x 0) 0 y)) (test 0 (p)) • applicative order: eval-and-apply infinite loop due to (p) • normal order: fully-expand-and-reduce infinite term, safe execution**Conditional Expressions**(define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)))) (define (abs x) (cond ((< x 0) (- x)) (else x))) (define (abs x) (if (< x 0) (- x) x))**Compound Predicates**(and (> x 5) (< x 10)) (define (>= x y) (or (> x y) (= x y))) (define (>= x y) (not (< x y)))