Recitation notes for 30-Sep-1998: Agenda: ------- * Announcements * I need the whole 50 minutes today, please be patient. * I haven't seen the midterm * Old midterms online w/ solutions; HIGHLY suggest you try at least the last two semesters' * There is no typechecker on the exam * TA evaluation * What continuations are used for (from last recitation) * Exceptions * Pop Quiz (no, it doesn't count towards your grade) * My guess at the midterm * Question on typing (here's an expression, what's the type) * Write a function to do _______ * Prove your function is correct * Implement a structure for this signature * Do something with higher-order functions * Midterm * Continuations, but not specifics of reg exp matching * Type-checking and evaluating by hand * Data abstraction, also type checking with this * Datatypes * Structural Induction * Simple Exceptions (e.g. raising Div on divide by zero) * What we've done so far (a nostalgic look back) * Vote * After-hours (I'll stay until 5:20) TA Evaluation: --------------- Please take the time to fill out this evaluation. Teaching is something I take very seriously and it is important that action is taken if I am falling short of your expectations. This survey is anonymous. Do not put your name on it. Nobody except myself (Adam) will see this survey or its results -- it is for my personal use only (ie, I won't get fired if you say that I suck, so be brutally honest). ---------------------------------------------------------------- On a scale of 1 to 5, 1 being "strongly disagree" and 5 being "strongly agree". If you do not have experience with a particular point, leave it blank. The TA speaks clearly 1 2 3 4 5 The TA's work on the board is 1 2 3 4 5 clear, organized, and easy to read The TA appreciates students' 1 2 3 4 5 questions The TA responds well to questions 1 2 3 4 5 The TA appears organized and prepared 1 2 3 4 5 for class The TA seems well in touch with 1 2 3 4 5 what is going on in lecture The TA shows concern for individual 1 2 3 4 5 students The TA uses class time efficiently 1 2 3 4 5 The TA significantly enhances my 1 2 3 4 5 learning experience The TA definately understands 1 2 3 4 5 the material being taught ---------------------------------------------------------------- I would like Adam to (check all that apply): ___ Give more introduction to each class ___ Give shorter introductions to each class ___ Spend more time discussing questions related to homework ___ Spend less time discussing questions related to homework ___ Discuss more examples ___ Discuss less examples ___ Go over examples in more detail ___ Go over examples in less detail ___ Speak more slowly ___ Speak louder ___ Discuss more "real life" examples ---------------------------------------------------------------- Is there anything I can do to improve the recitations? Is there anything I should stop doing? ---------------------------------------------------------------- Is there anything I do in recitation that you think really helps? (ie, more so than the average CS recitation) ---------------------------------------------------------------- Uses for continuations: ----------------------- * Exceptions without exceptions - Other languages w/o exceptions - To be "pure" functional (strictly speaking, exceptions are effects) * Cooperative Multi{task/thread}ing - When a thread decides it has had enough CPU time, it simply returns a continuation * Upcalls in - Windowing systems (message handlers) - Out-of-kernel filesystems (UserFS on Linux) * Encapsulating effects - again, you don't have effects yet. we'll fix this. Exceptions: ----------- * format: exception DivisionByZero exception UnknownException ( ... raise DivisionByZero ... ) handle NameOfASpecificException => 0 | OtherException => raise UnknownException * Also appear if pattern match is not exhaustive datatype 'a tree = Empty | Tree of 'a * 'a Tree * 'a Tree fun sumtree (Tree(a,l,r)) = a + sumtree l + sumtree r (* raises Match *) sumtree (Tree(3,Empty,Empty)) Pop Quiz: --------- -------------------------------------------------------------- What are the types of these expressions? (if the expression is incorrectly typed, say so) a) 1::2 Type error b) fun foo1 a b ((c,d), e) = a + b + e c + d + 4 val foo1 : int -> int -> (('a * int) * ('a -> int)) -> int c) exception DivideByZero fun foo2 a = if (a <> 0) then 4 / b else DivideByZero Type error -------------------------------------------------------------- Write a function to evaluate a polynomial represented by a list of integer coefficients [a0..an]. Assume you have (* pow(a,b) = a^b *) val pow : (int * int) -> int Your function should have type val eval : int list -> int -> int fun eval nil _ = 0 | eval (h::t) x = h + x * eval t x -------------------------------------------------------------- Make your function tail-recursive through the use of a continuation. Your function should have type val eval' : int list -> int -> (int -> int) -> int fun eval' nil _ cont = cont 0 | eval' (h::t) x cont = eval t x (fn z => cont(h + x * z)) -------------------------------------------------------------- Prove that eval' is correct. Be aware of notation (overbars denote ML language elements as opposed to mathematical elements). What we've done so far: ----------------------- * Evaluation and typing * Binding, scope, functions * Recursion * Induction * Datatypes * Patterns * Tail Recursion * Structural Induction * Data Structures and Representational Invariants * Continuations