Download presentation
Presentation is loading. Please wait.
Published byOwen Parks Modified over 9 years ago
1
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((= op ’numer) n) ((= op ’denom) d) (else: (error ”Bogus operation")))))) ((rat 3 4) ’numer) => 3
2
In Java: String m = new String("CS212");... if (m.equals("CS211")) {... if (m == "CS211") {...
3
New Special Form — set! (set! identifier expression)
4
set! trashes the substitution model: (define put-in-bowl (lambda (thing) (set! thing (cons thing '(in a bowl))) thing)) (put-in-bowl 'goldfish) => (goldfish in a bowl)
5
Which variable does set! set? (define x 'barney) => x (let ((x 'purple) (y (set! x 'monster))) x) => ??? x => ???
6
Which variable does set! set? (define x 'barney) => x (let* ((x 'purple) (y (set! x 'monster))) x) => monster x => barney
7
An example not involving set! (define x 10) (define foo (lambda () x)) (let ((x 15)) (foo)) => ???
8
An example not involving set! (define x 10) (define foo (lambda () x)) (let ((x 15)) (foo)) => 10
9
Static vs. Dynamic Scoping Static scoping: free variables resolved in the environment of the function definition Dynamic scoping: free variables resolved in the environment of the function call Closure: when a function object is created, the environment is packaged with it
10
Referential Transparency replace equal values with equal values set! destroys referential transparency
11
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are resolved in Scheme (and in almost any other programming language.) An expression only has a value with respect to an environment
12
Binding identifier : value Frame unordered set of bindings Environment ordered list of frames
13
x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) global environment (top level frame) (machine code for adding numbers)
14
The Lookup Rule the value of an identifier x is the value associated with x in the first (lowest) frame containing a binding for x undefined if there aren't any
15
Define rule To evaluate (define ident expr): evaluate expr in the current environment add a binding to the top-level frame (the global environment) Set! rule To evaluate (set! ident expr): evaluate expr in the current environment look up ident using lookup rule change that binding to value of expr
16
x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) x:10 +:{add} y:20 d:12 z:30 x:() w:foobar x:oh y:(list of stuff) (define d (+ 5 7)) beforeafter
17
x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) x:10 +:{add} y:20 z:30 x:() w:foobar x:my y:(list of stuff) (set! x 'my) beforeafter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.