CSC151 2007S, Class 05: Symbols and Lists Admin: * Are there questions on HW3? * Yes, it is the case that the final does not count in (approximately) part c. Overview: * Reminder: Key parts of an algorithm. * Symbolic values. * Lists. * Lab. /What might we see or care about in an algorithm?/ * Variables - Named values (define NAME VALUE) * The basic operations on those values * Generic * Untie a twisty-tie * Add two numbers * In Scheme * Basic numeric operations (+ NUM NUM) * Other numeric operations expt, log, max, min * Operations on strings: string-append * Lists and their operations * Symbols and their operations * Sequences of operations * For one operation: (OPERATION OPERAND OPERAND ...) * For more than one operation: Just put them in order. * When applying a procedure to other expressions, the inner expressions are computed first (+ (* 2 3) (* 4 5)) * Conditionals to decide between sequences of operations * Loops to repeat operations * Subroutines to group and parameterize operations /What might we expect to see in a Scheme program?/ * Parentheses * Lists /Symbols/ * Look a lot like names * All meaning is external * Write with 'cat * cat and 'cat are different * What can you do with symbols? * Check if they are equal. /Lists/ * Value of a list is "a sequenced bunch of values" * Core operations: * (cons VALUE LIST) - shove VALUE on the front of LIST, making a new list (cons 'a (list 'b 'c)) => (a b c) * (car LIST) - get the first element * (cdr LIST) - get all but the first element * Detour mnemonic - a way to represent something that's easy to remember * null - the name of the empty list * (null? LIST) - is list empty? * Advanced operations * (list VAL0 VAL1 ... VALn) - build a list more easily * (reverse LIST) * (list-ref LIST POS) - extract the thing at the given position * (length LIST) - how many elements are in the list? * How do we make a list of values, where the values are computed? (list (+ 23 b) (- 156 a)) /Reflection/ * Reminder: Reading for tomorrow (numbers!) * Scheme has another mechanisms for sequencing In (OP (OP OPERAND) (OP OPERAND)), the inner operations are performed first. * How do you build (a (b c) (d (e)))