1 / 10

Primitive Data Types

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

star
Download Presentation

Primitive Data Types

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

More Related