MIT6_01F09_lec02

MIT6_01F09_lec02 - 6.01: Introduction to EECS 1 Week 2...

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

View Full Document Right Arrow Icon
6.01: Introduction to EECS 1 Week 2 September 15, 2009 6.01: Introduction to EECS I Inheritance, Functional Programming, Recursion Week 2 September 15, 2009 Inheritance Just as Classes capture shared attributes among their instances , Superclasses capture shared attributes among their classes . State Machines We will represent many systems as state machines. state s n input i n output o n On the n th step , the system gets input i n alters the internal state s n and generates output o n Outline Inheritance Procedures as first-class objects Recursion Dictionaries and Arrays Read, if you haven’t already, Python material in Chapters 2, 3 and Appendix A Inheritance Just as Classes capture shared attributes among their instances , Superclasses capture shared attributes among their classes . Superclasses are environments Classes are environments whose parent can be a superclass en- vironment Instances are environments whose parent is a class environment Python Representation of State Machines class SM : def start (self): self.state = self.startState def step (self, inp): (s, o) = self.getNextValues(self.state, inp) self.state = s return o class Accumulator ( SM ): startState =0 def getNextValues (self, state, inp): return (state + inp, state + inp) 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
6.01: Introduction to EECS 1 Week 2 September 15, 2009 Check Yourself >>> a = Accumulator() >>> a.start() >>> a.step(15) >>> a.state >>> a.step(-5) >>> a.state ??? >>> b = Accumulator() >>> b.start() >>> b.step(5) >>> b.state ??? Classes and Instances for Accumulator b a Accumulator SM global run step start transduce getNextValues startState 0 5 state 10 state Classes and instances for an Accumulator SM . All of the dots represent procedure objects. 2 Simulation time 01234 5 6 7 input ’a’ ’b’ ’c’ ’a’ ’c’ ’a’ ’b’ state 01201 3 3 3 output True True True True False False False 0 3 2 1 c / True a / False b / False b / False c / False a / True b / True a / False c / False a / False b / False c / False Python implementation class ABC ( SM ): startState = 0 def getNextValues(self, state, inp): if state == 0 and inp == ’a’: return (1, True) elif state == 1 and inp == ’b’: return (2, True)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

MIT6_01F09_lec02 - 6.01: Introduction to EECS 1 Week 2...

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

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