מבוא מורחב למדעי המחשב בשפת scheme תרגול 10. environment model 3.2,...
TRANSCRIPT
מבוא מורחב למדעי המחשב Scheme בשפת
10תרגול
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))