Presentation is loading. Please wait.

Presentation is loading. Please wait.

Short Circuits Design Recipe Redux Structures

Similar presentations


Presentation on theme: "Short Circuits Design Recipe Redux Structures"— Presentation transcript:

1 Short Circuits Design Recipe Redux Structures

2 Processing Start with a top-level environment (TLE) containing builtins like the addition and multiplication procedure; there’s also a current environment, which starts out as the TLE Top level expressions are evaluated to produce a value v; the printed representation of v is printed out. An identifier definition (define ident exp) is processed by checking that the identifier ident is not bound in the top-level environment; if it is, it’s an error; otherwise it gets bound to the value of exp. A function definition (define (ident arg1 ... argn) body) is processed by checking that ident is not bound in the TLE; if it is bound, it’s an error. Otherwise, ident is bound to a closure whose “args” piece contains the identifiers arg1, ..., argn, and whose “body” piece contains the expression body (and not the value of the expression body!)

3 Evaluation Numbers evaluate to themselves; so do strings.
The value of an identifier is the value to which it’s bound in the current enviroment; if it’s not bound, evaluation produces an error.

4 Proc-app-expressions (builtin)
The value of a procedure-application-expression (proc c1 ... cn) is determined by evaluating the expression proc; if the resulting value is neither a builtin procedure nor a user-defined procedure, it’s an error. Otherwise, we evaluate the expressions c1 ... cn (if any) in order to get values v1,...,vn. (a) if proc evaluated to a builtin, the builtin procedure is applied to the values v1, , vn to produce a result r, which is the value of the procedure application expression.

5 Proc-app-expr (user-defined procedures)
(b) if proc evaluated to a user-defined procedure, then we temporarily extend the current environment by binding the arguments of the closure to the corresponding values (if the number of arguments and number of values don’t match, it’s an error) evaluate the body of the closure in this enlarged environment to get a value v reset the current environment to its prior state, losing the newly-added bindings the value of the procedure-application-expression is v.

6 New rule: and-expressions
An and-expression (and exp1 .. expn) is evaluated by evaluating exp1, whose value must be a boolean. If it is false, the value of the and-expression is false and the remaining expressions are never evaluated. If it is true, however, we evaluate exp2; again, the value must be a boolean or it’s an error. If the resulting value is false, the value of the and-expression is false and the remaining expressions are never evaluated. We proceed similarly through any remaining expressions. If all expressions evaluate to true, then the value of the and-expression is true.

7 New rule: or-expressions
An or-expression (or exp1 .. expn) is evaluated by evaluating exp1, whose value must be a boolean. If it is true, the value of the or-expression is true and the remaining expressions are never evaluated. If it is false, however, we evaluate exp2; again, the value must be a boolean or it’s an error. If the resulting value is true, the value of the or-expression is true and the remaining expressions are never evaluated. We proceed similarly through any remaining expressions. If all expressions evaluate to false, then the value of the or-expression is false.

8 New rule: if-expressions
An if-expression (if test-exp true-exp false-exp) is evaluated by evaluating test-exp, whose value must be a boolean. If it is true, the value of the if-expression is the value of true-exp, and false-exp is never evaluated. If it is false, the value of the if-expression is the value of false- exp, and true-exp is never evaluated.

9 Example (define x 0) (if (= x 0) 7 (/ 1 x))

10 Exercise (if (> 1 0) + −)

11 Design recipe Provide data definitions for all non-atomic types
Provide examples of data the procedure will process Specify the procedure’s type signature, saying the type of data consumed and the type of data produced Following the type-signature, code the procedure’s call structure, i.e., give names to the procedure and its arguments. Write a specification of the procedure (in words, using names) Write test cases +8. Code the procedure 9. Run your program


Download ppt "Short Circuits Design Recipe Redux Structures"

Similar presentations


Ads by Google