day14

day14 - altech CS1 -- Fall 2007 CS1: Introduction to...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: altech CS1 -- Fall 2007 CS1: Introduction to Computation Day 14: November 14, 2007 Environment Model and Local State altech CS1 -- Fall 2007 Today Review environment model frames environments procedures Encapsulation of state Message-passing style altech CS1 -- Fall 2007 Bindings A binding is an association between a name and a Scheme value Names: variable names, procedure names formal parameters of procedures in let statements: ( let ((name value) ...) ...) Values: any Scheme value altech CS1 -- Fall 2007 Bindings A binding is an association between a name and a Scheme value Examples: name: x value: 10 name: y value: #f name: square value: (lambda (x) (* x x)) altech CS1 -- Fall 2007 Frames A frame is a collection of bindings: x: 10 y: #f square: (lambda (x) (* x x)) altech CS1 -- Fall 2007 Frames Frames are used to look up the value associated with a name x = ? y = ? square = ? x: 10 y: #f square: (lambda (x) (* x x)) altech CS1 -- Fall 2007 Frames Frames have an enclosing environment which will be another frame ( parent frame ) if lookup fails, go to parent frame and try again then to its parent frame etc. x: 10 y: #f square: (lambda (x) (* x x)) to parent frame altech CS1 -- Fall 2007 Environments A n environment is a linked chain of frames ending in the global environment Every frame defines an environment starting from it x: 10 y: 20 z: 30 (global environment) E1 altech CS1 -- Fall 2007 Environments A n environment is a linked chain of frames ending in the global environment Every frame defines an environment starting from it x: 10 y: 20 z: 30 (global environment) E2 altech CS1 -- Fall 2007 Environments A n environment is a linked chain of frames ending in the global environment Every frame defines an environment starting from it x: 10 y: 20 z: 30 (global environment) E3 altech CS1 -- Fall 2007 Environments T h e global environment is there when Scheme interpreter starts up Evaluating code can create new frames and thus new environments All code being evaluated does so in the context of an environment because names must be looked up called the current environment altech CS1 -- Fall 2007 Rule 1: define define creates a new binding in the current environment (current frame) Example: (define x 10) (global environment) altech CS1 -- Fall 2007 Rule 1: define define creates a new binding in the current environment (current frame) Example: (define x 10) (global environment) x: 10 altech CS1 -- Fall 2007 Rule 2: set! s e t ! changes an old binding in the current environment NEVER creates a new binding Example: (set! x 'foo) (global environment) x: 10 altech CS1 -- Fall 2007 Rule 2: set!...
View Full Document

This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

Page1 / 97

day14 - altech CS1 -- Fall 2007 CS1: Introduction to...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online