Primitive Data Types - PowerPoint PPT Presentation

star
primitive data types n.
Skip this Video
Loading SlideShow in 5 Seconds..
Primitive Data Types PowerPoint Presentation
Download Presentation
Primitive Data Types

play fullscreen
1 / 10
Download Presentation
Presentation Description
192 Views
Download Presentation

Primitive Data Types

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 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

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

  3. Example of Evaluation (* (+ 2 (* 4 6)) (+ 3 5 7)) 390 15 26 * 7 + 5 3 + 2 24 * 6 4

  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.

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

  6. 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

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

  8. 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

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

  10. Compound Predicates (and (> x 5) (< x 10)) (define (>= x y) (or (> x y) (= x y))) (define (>= x y) (not (< x y)))