LECTURE 9: November 16, 2009 GOALS ---------------------------------------------------------------------- questions? i assume you have read the documentation for universe.ss 1. an interviewer's question: why are you using recursion? 2. organizing and running your solution for problem set 9, launch-many-worlds 3. design with classes OUTLINE ---------------------------------------------------------------------- 'hour 1' : (i do not expect to spend a full hour on this) Say your interviewer asks you to write the function reverse (on lists). structural design: rev.ss, plus stress test: rev.ss square growth with accumulator: rev-accu.ss, plus stress test rev-accu.ss linear growth BOTH are recursive, what happened? 1. accumulator-style processing helps eliminate the re-processing of the result of a recursion: see design with accumulators (when needed) 2. accumulator-style thus eliminates need for second recursion, linear 3. PLUS: accumulator-style turns the function into a tail-recursive function. Tail-recursive functions need no space to recall what happens on return, and good languages implement all "tail" function calls in this manner. You can turn this into a "loop" (foldl) and from there into a loop. with abstraction: use foldl to shorten rev-accu.ss rev-fold.ss looks like loop in C ---------------------------------------------------------------------- 'hour 2' : (i do not expect to spend a full hour on this) 1. how to arrange the solution for 9.1: universe-demo.ss 2. how to run it 3. how to see the state information via (state true) ---------------------------------------------------------------------- 'hour 3' : I expect to have 90mins+ for this 'hour' WHY CLASSES? say you design a snake game. you will come up with data definitions and functions along the following lines: world = snake x food world-tock : world -> world world-control : world keyevent -> world world-render : world -> scene snake = (make-snake segment [listof segment]) snake-move : snake -> snake snake-eat : snake -> snake snake-control : snake keyevent(subset) -> snake snake-to-scene : snake scene -> scene segment (make-segment ...) segment-to-scene : segment scene -> scene ... take a close look, and you will see language patterns: all functions for world have name world and take world as first arg all functions for snake have name snake and take snake as first arg all functions for seg have name seg and take seg as first arg ... how about we organize our programs so that we always just 'know' these things. what this means is to 'draw boxes' or to 'bracket' these above blocks and we'll save a lot of 'typing': { world = snake x food tock : -> world control : keyevent -> world render : -> scene } { snake = (make-snake segment [listof segment]) move : -> snake eat : -> snake control : keyevent(subset) -> snake to-scene : scene -> scene } { segment (make-segment ...) to-scene : scene -> scene ... } these kinds of blocks are called classes. the idea of classes is all about program organization, put things together that belong together. don't mention superfluous things, such as that the first argument is always left implicit. ================================= HOW CLASSES work; the mechanics of classes and interfaces: see classes.ss design posn and explain plus render: Scene -> Scene add the posn to the given scene plus translate : Number Number -> posn% 'move' the position by deltax and deltay the basics of testing in this world PREFERENCES: debugging xor test coverage ================================= DESIGN: classes are all about 'structure', specifically capturing the structure of a data definition with language mechanisms. in 8 weeks you have seen these 'structural' elements in data defs: -- atomic data (they exist in class-based languages too) : OK -- intervals, enumerations of atomic data : OK -- structures: see posn% -- nested structures : BELOW -- itemizations of all kinds of data : BELOW -- self-referential and cross-referential data defs : BELOW -- higher-order data (functions) : WON"T COVER HERE, see book ... BELOW: designing with classes and interfaces: data definitions: informal in terms of information uml class diagram sketches (introduce as doodle notation to be used while you pair-program; no need to add to programs) basic strategy: classes, fields, interfaces for a group of connected classes; classes expose more in code than Data Def in BSL/ISL, which are just comments. start running example: a shape is one of -- circle -- square make up examples, representation and interpretation designing methods: stage 1: design1.ss -- area -- render |=======================================================| |(NOTE: I didn't have time for this one.) | |stage 2: design2.ss | |-- add triangle% | | EXERCISE: represent orthogonal triangle with two | | sides parallel to vertical and horizontal sides | | of the canvas; draw an example | |=======================================================| -- purpose: data extensibility stage 3: design3.ss -- add overlay% -- purpose: self-referential data definitions and recursion via interfaces