CSC153.01 2004S, Class 3: Types Admin: * Read "Procedures in Scheme" * Begin homework 1 (lab writeup) * Any questions so far? * Choose good titles for your email Overview: * What is a type? * Symbols * Lists * Numbers * Lab What is a type? * A description of the kind of information a variable can hold * A type is a set of values * A type is a set of restrictions on how you use values Many languages require *explicit* typing of variables * Benefits: * Makes sure that you know what you're doing * Permits you to allocate memory carefully * Catch type errors at compile time * Drawbacks: * Makes life harder if you don't know what you're doing * Typing can be hard: Predicting the type of the result of an equation may be difficult without knowing the input values * You sometimes need to subvert typing to get things to work right * Types are sometimes obvious Some language designers believe that typing should be optional. The designers of LISP and Scheme fall into that category. Three key types in Scheme * Symbols: "Atomic" values with names you can choose 'cat 'cheer 'here * All you can do with symbols is create them and compare them * "Is variable x the symbol 'cat?" * Warning! When the scheme interpreter prints symbols, it elides the quote * The quote is important for input, so that we can distinguish the variable x from the symbol 'x * Numbers: Lots and lots of kinds of numbers * Integers (arbitrarily long): A number you can arrive at by repeatedly incrementing (adding 1 to) or decrementing (subtracting 1 from) 0. * Rational numbers: Ratio of two integers * Real numbers: Anything on the real number line * Complex numbers: A Physicist's favorite kind of numbers Real part and "imaginary" part Points in the real/imaginary plane * Lists: "Ordered" collections of values * Empty list: null * Add something to the front of a list, thereby building a new list (cons newvalue oldlist) * Get the first value in a list with (car lst) * Get all but the first value with (cdr list) Try the lab! Almost everyone had trouble creating the following list using cons (a (b c) (d (e))) (Which is, of course, why we assign it.) Key ideas: * Build it up slowly! * Think carefully about what the parts are * Identify common patterns A list of two elements has the form (cons FIRST (cons SECOND null)) Have a good weekend; try to finish the lab; ask questions via email and on Monday