+ All Categories
Home > Documents > מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages...

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages...

Date post: 19-Dec-2015
Category:
View: 232 times
Download: 3 times
Share this document with a friend
Popular Tags:
34
בבבב בבבבב בבבבב בבבבב בבבבScheme בבבבב8
Transcript

מבוא מורחב למדעי המחשב Scheme בשפת

8תרגול

Environment Model

3.2, pages 238-251

Environments• Binding: a pairing of a name and a value

• Frame: a table of bindings

• Environment: a sequence of frames

• A precise, completely mechanical, description of:• name-rule looking up the value of a variable• define-rule creating a new definition of a var• set!-rule changing the value of a

variable• lambda-rule creating a procedure• application rule applying a procedure

The Environment Model

The Environment Model• Name-rule: A name X evaluated in environment E gives

the value of X in the first frame of E where X is bound• Define-rule: A define special form evaluated in environment E creates

or replaces a binding in the first frame of E• Set!-rule: A set! of variable X evaluated in environment E changes the

binding of X in the first frame of E where X is bound• Lambda-rule: A lambda special form evaluated in environment E

creates a procedure whose environment pointer points to E• Application Rule: To apply a compound procedure P to arguments

1.Create a new frame A2. Make A into an environment E:

A's enclosing environment pointer goes to the same frame as the environment pointer of P

3. In A, bind the parameters of P to the argument values

4. Evaluate the body of P with E as the current environment

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

• (square 5)

(define (square x) (* x x))(define (sum-of-squares x y) (+ (square x) (square y)))(define (f a) (sum-of-squares (+ a 1) (* a 2)))

Application: (f 5)

Nested Procedures

(define g (lambda () (lambda (x y) (* x y))))

(define f (g))

(f 3 4) => 12

GE

p:b:(lambda (x y) (* x y))

g:

(define g (lambda () (lambda (x y) (* x y))))

GE

p:b:(lambda (x y) (* x y))

g:f:

p: x yb: (* x y)

E1 empty

(define f (g))

GE

p:b:(lambda (x y) (* x y))

g:f:

p: x yb: (* x y)

E1 empty

(f 3 4)

X=3Y=4

E2

Nested Procedures

(define g

(lambda (z)

(lambda (x y)

(* x y z))))

(define f (g 2))

(f 3 4) => 24

GE

p: zb:(lambda (x y) (* x y z))

g:f:

(define g (lambda (z) (lambda (x y) (* x y z))))

GE

p: zb:(lambda (x y) (* x y z))

g:f:

p: x yb: (* x y z)

E1 Z: 2

(define f (g 2))

GE

p: zb:(lambda (x y) (* x y z))

g:f:

p: x yb: (* x y z)

E1 Z: 2

(f 3 4)

X=3Y=4

E2

Let expressions

(let ((<var> <exp>)) <body>)is syntactic sugar for((lambda (<var>) <body>) <exp>)

(define a 5)(define b 6)(let ((a 2) (c a))

(+ a b c))=((lambda (a c) (+ a b c)) 2 a)

Let – cont.GE

a: 5 b: 6

p: a c

b: (+ a b c)

E1

a: 2

c: 5

(define a 5)(define b 6)(let ((a 2) (c a))

(+ a b c))=((lambda (a c) (+ a b c)) 2

a)

The cash machine(define (make-withdraw balance)

  (lambda (amount)    (if (>= balance amount)        (begin (set! balance (- balance amount))               balance)        "Insufficient funds")))

(define W1 (make-withdraw 100))

> W1

>

>(W1 50)

>

>(W1 40)

>

>(W1 20)

>

50

10

Insufficient funds

#<procedure>

(define (make-withdraw balance)  (lambda (amount)    (if (>= balance amount)        (begin (set! balance (- balance amount))               balance)        "Insufficient funds")))

(define W1 (make-withdraw 100))

(W1 50)

More than one cash machine>(define W1 (make-withdraw 100))

>(define W2 (make-withdraw 100))

>

>(W1 50)

>

>(W2 40)

>

50

60

(define W2 (make-withdraw 100))

question from past exams

26

Make-line

(define (make-line a b) (lambda (x) (cond ((pair? x) (set! a (car x)) (set! b (cdr x)))

(else (+ b (* x a)))) ) ) (define a 4)(define b 5)(define proc (make-line 1 2))

Q1

make-line:a: 4b: 5proc:

GE

p: a bb:(lambda (x)…

a: 1b: 2

E1

p: xb:(cond…

make-line:a: 3b: 5proc:

GE

p: a bb:(lambda (x)…

a: 1b: 2

E1

p: xb:(cond…

x: 1E2

(+ b (* x a))(set! a (proc 1))

make-line:a: 3b: 5proc:

GE

p: a bb:(lambda (x)…

a: 1 3b: 2 4

E1

p: xb:(cond…

x: 3.4E3

(set! a (car x))(set! b (cdr x))(proc (cons 3 4))

make-line:a: 3b: 5proc: c: 7

GE

p: a bb:(lambda (x)…

a: 3b: 4

E1

p: xb:(cond…

x: 1E4

(+ b (* x a))(define c (proc 1))

(

(lambda (z)

(define x (lambda (x) (lambda (y z) (y x))))

( ( (x (lambda () z)) (lambda (z) z) 3 ) )

)

2)

Expression Env Comment

1 ((lambda1(z) op1 op2) 2) GE (op1 op2)

2 (op1 op2) E1

3 Op1(define x (lambda2 (x) (lambda3(y z) (y x))))

E1

4 Op2(((x (lambda5 () z)) (lambda4(z) z) 3))((op3 (lambda4 (z) z) 3))

E1

5 op3(x (lambda5 () z))

E1 l3

6 ((l3 l4 3)) E1

7 (y x) E3 (l4 l5)

8 Z E4 l5

9 (l5) E1

10 Z E5 2

L1:p: zb: (define x ...)

(...)

E1z: 2x:L2

L2:p: xb: (lambda (y z) (y x)))

L5:p: -b: z

E2x:L5

L3:p: y,zb: (y x)

L4:p: zb: z

E3y: L4z: 3

E4z: L5

E5

GE


Recommended