CSC151.01 2006S, Class 49: Stacks Admin: * Apologies about reading. * Tons of cool talks this week: * Today, 1:15 (2417): Eye Tracking * Today, 4:15 (CCC): Assistive Technologies for Art * Thursday, 11:00 (2424): Interactive Character Animation * Thursday, 9:00 p.m. (2424): Rockstar Games Presents Table Tennis * Friday, 4:15 p.m. (2424): Careers in the Gaming Industry. * Happy purgatory week! * Are there questions on the exam? Overview: * Abstract Data Types (ADTs) and Data Structures. * Stacks. * Implementing Stacks in Scheme. * Some Applications. * Lab. /About CS, Revisited/ * CS is "the study of algorithms" * "and data" * For data, we must consider: * How to represent the data * In Scheme, a list is a bunch of linked cons cells * What "natural operations" the data support * For lists, key operations include car, cdr, (cadr), (append), cons, null, and null? * Computer scientists then think about natural collections of operations * And efficient ways to represent data to support those operations. * Some others: * Stacks, Queues, Dictionaries, Arrays/Vectors, Priority Queues, ... * Today: Stacks * push - put on top * pop - grab from top * peek - look at, but do not remove the top * empty? - check if there is anything * Real-world examples * Napkin holders * Stacks of plates in Cowles * Legos are particularly hard to remove in the middle * Applications in CS * Match tags in an HTML document (or parens in Scheme) * Representing expressions unambiguously 3 + 4 * 5 * Can we represent them so that people don't have to memorize order of operations? * Yes, we've done it in Scheme * Can we do it without so many parens? * Yes, if we have fixed-arity procedures * RPN - "reverse Polish notation": Operator follows operands 3 4 + 5 * 3 4 5 * + * Can implement with a stack * If you see a number, push it * If you see an operation, pop the params, evaluate, and push the result * Procedure calls /How do we implement stacks (in Scheme)?/ * A stack can be represented by a list peek = car push = cons value to front and reset name pop = car and reset name to cdr empty = null? * Use an object /Lab!/ Potential form for correctly-nested? (define correctly-nested? (letrec ((kernel (lambda (lst stack) (cond ((null? lst) ...) ((not (tag? (car lst))) ...) ((end-tag? (car lst)) ...) (else ; It's an opening tag ...))))) (lambda (lst) (kernel lst (make-stack)))))