Paradigms_of_Programming (2)
Paradigms_of_Programming (2)
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
1 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
(define (sum-of-squares x y)
(+ (square x) (square y))
)
(define (f a)
(sum-of-squares (+ a 1) (+ a 2))
)
(f 2)
2 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
Evaluation of (f 2)
(sum-of-squares (+ 2 1) (+ 2 2))
(sum-of-squares 3 4)
(+ square(3) square(4))
(+ (* 3 3) (* 4 4))
(+ 9 16)
25
The “evaluate the arguments and then apply” method that the
interpreter actually uses, which is called applicative-order evaluation.
Scheme uses this method for efficiency.
This is not the exact manner in which the interpreter works, but for
ease of understanding this works just fine.
3 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
(f 2)
(sum-of-squares (+ 2 1) (+ 2 2))
(+ square (+ 2 1) square (+ 2 2))
(+ (* (+2 1) (+ 2 1)) (* (+ 2 2) (+ 2 2)))
(+ (* 3 3) (* 4 4)) (+ 9 16)
25
This type of evaluation also has its own uses, but for now we stick
to applicative order evaluation.
4 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
5 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
else is a special symbol that can be used in place of the < p > in
the final clause of a cond.
7 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
Alternatively, one could also use if conditional, if there are only two
cases to be dealt with.
if has the following structure
8 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
9 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
10 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
11 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
11 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
12 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
12 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
13 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
14 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
15 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
15 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
16 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
17 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
18 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
So you can consistently replace all instances of guess with say esti
and all instances of x with say y, without affecting the procedure’s
execution/output.
18 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
One cannot freely replace the abs with some other arbitrary variable
say sab unless both these procedures do the exact same thing.
In this example, <, −, abs, and square happen to be the free
variables.
19 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
Since the user is primarily concerned with the sqrt procedure, the
rest of the procedures only clutter up their minds/code.
20 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
22 / 24
Evaluation order/ Substitution model
Conditionals
Black-Box Abstraction
Example
Food for thought
Local Names
Internal definitions and block structure
24 / 24