CSC151.02 2003F, Class 7: Lists Admin: * Sorry for the heat * Are there questions on homework 1? * Did anyone go to the Festival for the Arts events? * Wednesday at 4:15 * Professor Jepsen on Polygonal thingys * Choed Khannabha on Computer-Assisted Room Draw * Be prepared to complete the lab on your own. * Read "Numeric Values in Scheme" for tomorrow. * If you're registered in Grinnell, don't forget to vote tomorrow Topics: * Symbolic values * Lists * Lab: Symbols and Lists LISP's original design: A language for exploring thought * Theory was that thought is based on symbolic processing of groups of symbols * LISP/Scheme make it easy to generate symbols and put them in groups At least two ways of viewing a word or letter: * As the name of something else (define dozen 12) (define ta "Reese") * As the abstractions that we combine 'cheese We want to join those symbols into lists * The empty list: null * Why start with an empty list? You have to start somewhere. Computers and computer scientists like to start as simply as possible. * Add something to the front of a list (cons NEWTHING OLDLIST) => gives new list with NEWTHIGN at the front E.g., if former-tas were (cassie vivek rachel) (cons 'reese former-tas) => (reese cassie vivek rachel) Scheme uses the same syntax for different things and you're expected to figure out the meaning from context. * YOu can also build lists with list (list 'a 'b 'c) => (a b c) * You can get the first element of a list with car * You can get all but the first element of a list with cdr * To get the second element of a list (say students), you take the car of the cdr (car (cdr students)) * Because taking the nth element of a list is a common operation, it is also built-in (but ends up defined in terms of car and cdr (list-ref LIST NUMBER) Takes the appropriate element of the list (list-ref (list 'a 'b 'c) 2) => c How do you build up a list? Do you name it at every stage? Yes (define initial-list null) (define a (cons 'a null)) (define ba (cons 'b a)) (define aba (cons 'a ba)) --- REFLECTION * Groups work faster than individuals * cons builds a new list; it does not modify the existing list * (cons alpha beta gamma) is not the same as (cons 'alpha 'beta 'gamma) * To get a copy of the previous command to edit, use Esc-P * The dots you get in the middle of lists are Scheme's subtle way of saying "You screwed up" * In order to create the list (d (e)) you need to get over a mental barrier. * This is a two element list * The first element is the symbol d * The second element is the list of the symbol e * TO make it (list 'd (list 'e)) * How would you create it with cons?