42
2 ררררר- רררר ררררר1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 21

Lecture 2 - Substitution Model (continued)

- Recursion

- Block structure and scope (if time permits)

Page 2: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 222

Evaluation of An Expression

To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

replaced by the corresponding actual values

To Evaluate a combination: (other than special form)a. Evaluate all of the sub-expressions in any orderb. Apply the procedure that is the value of the leftmost sub-

expression to the arguments (the values of the other sub-expressions)

The value of a numeral: numberThe value of a built-in operator: machine instructions to executeThe value of any name: the associated object in the environment

Page 3: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 23

lambda:

(lambda (x y) (+ x y x 2))

• 1st operand position: the parameter list (x y) a list of names (perhaps empty)

• 2nd operand position: the body (+ x y x 2) may be any sequence of expressions

The value of a lambda expression is a compound procedure.

Reminder: Lambda special form

Page 4: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 24

Evaluating expressions

To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

replaced by the corresponding actual values

==> ((lambda(x)(* x x)) 5)

Proc(x)(* x x) 5

(* 5 5)

25

Page 5: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 255

Using Abstractions

==> (square 3)

9

==> (+ (square 3) (square 4))

==> (define square (lambda(x)(* x x)))

(* 3 3) (* 4 4)

9 16+

25

Environment Table

Name Value

square Proc (x)(* x x)

Page 6: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 266

Yet More Abstractions

==> (define f (lambda(a) (sum-of-two-squares (+ a 3) (* a 3))))

==> (sum-of-two-squares 3 4)

25

==> (define sum-of-two-squares (lambda(x y)(+ (square x) (square y))))

Try it out…compute (f 3) on your own

Page 7: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 27 7

Syntactic Sugar for naming procedures

(define square (lambda (x) (* x x))

(define (square x) (* x x))

Instead of writing:

We can write:

Page 8: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 28 8

(define second )

(second 2 15 3) ==> 15

(second 34 -5 16) ==> -5

Some examples:(lambda (x) (* 2 x))

(lambda (x y z) y)

Using “syntactic sugar”:

(define (twice x) (* 2 x))

Using “syntactic sugar”:

(define (second x y z) y)

(define twice )

(twice 2) ==> 4

(twice 3) ==> 6

Page 9: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 299

Lets not forget The Environment

==> (define x 8)

==> (+ x 1)

9

==> (define x 5)

==> (+ x 1)

6The value of (+ x 1) depends on the environment!

Page 10: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 210 10

Using the substitution model(define square (lambda (x) (* x x)))(define average (lambda (x y) (/ (+ x y) 2)))

(average 5 (square 3))(average 5 (* 3 3))(average 5 9) first evaluate operands,

then substitute

(/ (+ 5 9) 2)(/ 14 2) if operator is a primitive procedure, 7 replace by result of operation

Page 11: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 211 11

Booleans

Two distinguished values denoted by the constants

#t and #f

The type of these values is boolean

==> (< 2 3)

#t

==> (< 4 3)

#f

Page 12: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 212 12

Values and types

Values have types. For example:

In scheme almost every expression has a value

Examples:

1) The value of 23 is 232) The value of + is a primitive procedure for addition3) The value of (lambda (x) (* x x)) is the compound

procedure proc (x) (* x x)

1) The type of 23 is numeral 2) The type of + is a primitive procedure3) The type of proc (x) (* x x) is a compound procedure4) The type of (> x 1) is a boolean (or logical)

Page 13: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 21313

No Value?• In scheme almost every expression has a value

• Why almost?

Example : what is the value of the expression(define x 8)

• In scheme, the value of a define expression is “undefined” . This means “implementation-dependent”

• Dr. Scheme does not return (print) any value for a define expression.

• Other interpreters may act differently.

Page 14: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 214 14

More examples==> (define x 8)

Name Value

Environment Table

8x

==> (define x (* x 2))

==> x

16 16

==> (define x y)

reference to undefined identifier: y

==> (define + -)

>-<#+

==> (+ 2 2)

0

Page 15: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 21515

The IF special form

ERROR2

(if <predicate> <consequent> <alternative>)If the value of <predicate> is #t,

Evaluate <consequent> and return it

Otherwise

Evaluate <alternative> and return it

(if (< 2 3) 2 3) ==> 2

(if (< 2 3) 2 (/ 1 0)) ==>

Page 16: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 21616

IF is a special form

•In a general form, we first evaluate all arguments and then apply the function

•(if <predicate> <consequent> <alternative>) is different:

<predicate> determines whether we evaluate <consequent> or <alternative>.

We evaluate only one of them !

Page 17: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 217

Using the substitution model(define square (lambda (x) (* x x)))(define average (lambda (x y) (/ (+ x y) 2)))

(average 5 (square 3))(average 5 (* 3 3))(average 5 9) first evaluate operands,

then substitute

(/ (+ 5 9) 2)(/ 14 2) if operator is a primitive procedure, 7 replace by result of operation

Page 18: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 218

Recursive Procedures• How to create a process of unbounded length?• Needed to solve more complicated problems.• Start with a simple example.

Page 19: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 219

Example: Sum of squares

•S(n) = 02 + 12 + 22 ………. …… (n-1)2 + n2

Notice that:•S(n) = S(n-1) + n2

•S(0) = 0

These two properties completely define the function

S(n-1)

Wishful thinking: if I could only solve the smaller instance …

Page 20: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 220

An algorithm for computing sum of squares

(define sum-squares (lambda (n) (if (= n 0)

0 (+ (sum-squares (- n 1)) (square n))))

Page 21: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 221

Evaluating (sum-squares 3) (define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))

(sum-squares 3)(if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3)))(+ (sum-squares (- 3 1)) (square 3))(+ (sum-squares (- 3 1)) (* 3 3))(+ (sum-squares (- 3 1)) 9)(+ (sum-squares 2) 9)(+ (if (= 2 0) 0 (+ (sum-squares (- 2 1)) (square 2))) 9)…(+ (+ (sum-squares 1) 4) 9)…(+ (+ (+ (sum-squares 0) 1) 4) 9)(+ (+ (+ (if (= 0 0) 0 (+ (sum-squares (- 0 1)) (square 0))) 1) 4) 9)(+ (+ (+ 0 1) 4) 9)…14

What would have happened if ‘if’ was a function ?

Page 22: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 222

(sum-squares 3)(if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3)))(if #f 0 (+ (sum-squares 2) 9))(if #f 0 (+ (if #f 0 (+ (sum-squares 1) 4)) 9))(if #f 0 (+ (if #f 0 (+ (if #f 0 (+ (sum-squares 0) 1)) 4)) 9))(if #f 0 (+ (if #f 0 (+ (if #f 0 (+ (if #t 0 (+ (sum-squares -1) 0)) 1)) 4)) 9))

..

Evaluating (sum-squares 3) with IF as regular form

(define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))

We evaluate all operands. We always call (sum-squares) again. We get an infinite loop…….. OOPS

Page 23: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 223

General form of recursive algorithms• test, base case, recursive case(define sum-sq (lambda (n)

(if (= n 0) ; test for base case 0 ; base case (+ (sum-sq (- n 1)) (square n))

; recursive case)))

• base case: small (non-decomposable) problem• recursive case: larger (decomposable) problem• at least one base case, and at least one recursive case.

Page 24: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 224

Another example of a recursive algorithm

• even?

(define even? (lambda (n)

(not (even? (- n 1))) ; recursive case

)))

(if (= n 0) ; test for base case #t ; base case

Page 25: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 225

Short summary• Design a recursive algorithm by

1. Solving big instances using the solution to smaller instances.

2. Solving directly the base cases.

• Recursive algorithms have

1. test

2. recursive case(s)

3. base case(s)

Page 26: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 226

Block StructureLets write a procedure that given x, y, and z computes

f(x,y,z) = (x+y)2 + (x+z)2

(define (sum-and-square x y) (square (+ x y)))

(define (f x y z) (+ (sum-and-square x y) (sum-and-square x z)))

Page 27: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 227

Block structure (cont.)

(define (f x y z) (define (sum-and-square x y)

(square (+ x y)))

(+ (sum-and-square x y) (sum-and-square x z)))

Lets write a procedure that given inputs x, y, and z, computes

f(x,y,z) = (x+y)2 + (x+z)2

while keeping sum-and-square private to f(hidden from the outside world):

Page 28: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 228

We still need to clarify the substitution model..

(define (f x y z) (define (sum-and-square x y)

(square (+ x y)))

(+ (sum-and-square x y) (sum-and-square x z)))

==> (f 1 2 3)

(define (sum-and-square 1 2)

(square (+ 1 2)))

(+ (sum-and-square 1 2) (sum-and-square 1 3)))

Page 29: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 229

Bounded variables and scope

A procedure definition binds its formal parameters

The scope of the formal parameter is the body of the procedure. This is called lexical scoping

(define (f x y z) (define (sum-and-square x y)

(square (+ x y)))

(+ (sum-and-square x y) (sum-and-square x z)))

x,y,zx,y

Page 30: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 230

Evaluation of An Expression (refined)

To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

replaced by the corresponding actual values. Do not substitute for occurrences that are bound by an internal definition.

To Evaluate a combination: (other than special form)a. Evaluate all of the sub-expressions in any orderb. Apply the procedure that is the value of the leftmost sub-

expression to the arguments (the values of the other sub-expressions)

The value of a numeral: numberThe value of a built-in operator: machine instructions to executeThe value of any name: the associated object in the environment

Page 31: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 231

The refined substitution model

(define (f x y z) (define (sum-and-square x y)

(square (+ x y)))

(+ (sum-and-square x y) (sum-and-square x z)))

==> (f 1 2 3)

(define (sum-and-square x y)

(square (+ x y)))

(+ (sum-and-square 1 2) (sum-and-square 1 3)))

Page 32: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 232

The refined substitution model

==> (f 1 2 3)

(define (sum-and-square x y)

(square (+ x y)))

(+ (sum-and-square 1 2) (sum-and-square 1 3)))

(+ (sum-and-square 1 2) (sum-and-square 1 3)))

Sum-and-square Proc (x y) (square (+ x y))

Page 33: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 233

Computing SQRT: A Numeric Algorithm

To find an approximation of square root of x, use the following recipe:• Make a guess G• Improve the guess by averaging G and x/G• Keep improving the guess until it is good enough

G = 1X = 2

X/G = 2 G = ½ (1+ 2) = 1.5

X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666

X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156

.2for :Example xx

Page 34: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 234

(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))

(define (good-enough? guess x) (< (abs (- (square guess) x)) precision))

(define (improve guess x) (average guess (/ x guess)))

(define (sqrt x) (sqrt-iter initial-guess x))

(define initial-guess 1.0)(define precision 0.0001)

Page 35: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 235

Good programming Style1. Divide the task to well-defined, natural, and

simple sub-tasks.

E.g: good-enough? and improve .

Rule of thumb : If you can easily name it, it does a well-defined task.

2. Use parameters. E.g.: precision, initial-guess.

3. Use meaningful names.

Page 36: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 236

Procedural abstractionIt is better to:

•Export only what is needed•Hide internal details.

The procedure SQRT is of interest for the user.

The procedure improve-guess is an internal detail.

Exporting only what is needed leads to:•A clear interface•Avoids confusion

Page 37: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 237

Rewriting SQRT (Block structure)

(define (sqrt x)

(define (good-enough? guess x) (< (abs (- (square guess) x)) precision))(define (improve guess x) (average guess (/ x guess)))

(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))

(define initial-guess 1.0)(define precision 0.00001)

(sqrt-iter initial-guess x))

Page 38: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 238

Further improving sqrt

Note that in every application of sqrt we substitute for x the same value in all subsequent applications of compound procedures !

Therefore we do not have to explicitly pass x as a

formal variable to all procedures. Instead, can leave

it unbounded (“free”).

Page 39: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 239

SQRT again, taking advantage of the refined substitution model

(define (sqrt x)

(define (good-enough? guess) (< (abs (- (square guess) x)) precision))

(define (improve guess) (average guess (/ x guess)))

(define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision 0.00001)

(sqrt-iter initial-guess))

Page 40: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 240

SQRT (cont.)==>(sqrt 2)

(define (good-enough? guess) (< (abs (- (square guess) 2)) precision))

(define (improve guess) (average guess (/ 2 guess)))

(define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision 0.00001)

(sqrt-iter initial-guess))

Page 41: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 241

Lexical Scoping - again

The lexical scoping rules means thatthe value of a variable which is unbounded (free) in a procedure f is taken from the procedure in which f was defined.

It is also called static scoping

Page 42: מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור 242

Another example for lexical scope

(define (proc1 x) (define (proc2 y) (+ x y)) (define (proc3 x) (proc2 x)) (proc3 (* 2 x)))

Proc3.x

Proc1.x

(proc1 4) proc1.x = 4(proc3 8) proc3.x = 8(proc2 8) proc2.y = 8 proc2.x=proc1.x=412