Com S 342 meeting -*- Outline -*- * data types (1.2) ** characteristics Q: What characterizes (specifies) an Abstract Data Type (ADT)? values and operations ------------------------------------------ CHARACTERISTICS OF DATA TYPES IN PROGRAMMING LANGUAGES (EOPL 1.2) Characteristics - Values + abstract + syntax of printed output - Operations + procedures + syntax of literals + special forms Scheme example, the booleans - Values - Operations + procedures: not, eqv? + syntax of literals: #t, #f + special forms: if, cond, and, or ------------------------------------------ Note that in the programming notion, don't usually consider syntax of printed output, or syntax of literals Special forms could be defined by macros in programming ... + abstract: true, false + printed in output as: #t, #f ------------------------------------------ FOR YOU TO DO: NUMBERS Values? + abstract? + syntax of printed output? Operations + procedures? + syntax of literals? + special forms? ------------------------------------------ procedures: + - * / zero? positive? negative? odd? even? < > <= >= abs quotient remainder modulo max min floor ceiling truncate round expt log ** types *** Scheme universe of types Most programming languages split values up into different types Q: do any not do that? What's the advantage? turing machines? BCPL advantage: language figures out for you what piece of code used to add numbers (int vs. float vs. double) this is why C has types... ------------------------------------------ THE SCHEME TYPE UNIVERSE (not to scale) datum !------------------!=================-! ! boolean ! null ! ! ! !-----------!------! list ! ! ! !_______________________! ! ! char ! pair ! !-----------!-------------------------! ! ! ! ! number ! procedure ! ! ! ! !--------!-----------------!----------! !string / ! \ port ! ! / ! \ ! ! / symbol ! vector \ ! !-------------------------------------! ------------------------------------------ null, containing just (), is a subset of list list is almost a subset of pair, but () is a list, and not a pair Each subset, except datum, has a predicate associated with it: pair? list?, null?, vector?, symbol?, etc. *** other types on computer... if possible show examples of, and briefly indicate main operations **** strings ------------------------------------------ STRINGS Values + abstract: finite sequences of chars + printed: "a string", "\"hi\"" Operations + procedures: string, string-append, string-ref (0 based), string-length, string=?, string-ci=?, string?, string<=?, ... string-ci?, ... substring, ... + syntax of literals: "a string" ------------------------------------------ literals almost exactly as in C or C++ **** symbol like strings, but have constant time equality test, and can't be mutated ------------------------------------------ SYMBOLS Values: + abstract: nonempty finite sequences of characters + printed: a-symbol, mySymbol, ... Operations: + procedures: eq? (fast equality test) + special forms: quote, ' (quote a-symbol) 'a-symbol '+ 'if 'lambda 'quote ------------------------------------------ quotation even works for keywords, important as we'll be using these as symbols in what we do **** lists like singly linked lists, access at head '() cons list '(+ x 3) vs. (+ x 3) and (list (+ x 3)) append car, cdr cadr, caddr, caar, etc. Play with them, then summarize as follows ------------------------------------------ LISTS (list T) Values + abstract: finite sequences of T + printed: (), (a b), (c d e), ... Operations + procedures: cons, car, cdr, null? length, append, list, ... + syntax of literals: '(), '(a b), ... Examples: '(1 2) = (list 1 2) = (cons 1 (cons 2 '())) BOX AND POINTER VIEW !-----------! ! | ! !-----------! car cdr (cons 1 (cons 2 '())) ------------------------------------------ draw pictures like ... [ * | * ] [ * | * ] | \----^ | | v v v 1 2 () or [ 1 | *-]-->[ 2 | / ] so these are singly-linked lists **** pairs (improper lists) play with improper lists on-line a bit, same use of cons, car and cdr as lists ------------------------------------------ PAIRS vs. LISTS def: a *pair* (or cons cell) is def: A *list* is def: an *improper list* is a pair that is not ------------------------------------------ ... what is returned by cons. (cons 1 2) (cons 1 (cons 2 '())) ... either () or a pair whose cdr is a list. '() (cons 1 (cons 2 '())) ... a list. (cons 1 2) Q: So is list a subset of pair? no Q: Is null a subset of list? yes Q: is an improper list a list? no ------------------------------------------ DOT NOTATION Quotation '(a b c) = (list 'a 'b 'c) = '(1 . 2) = ------------------------------------------ ... = (cons 'a (cons 'b (cons 'c '()))) = '(a . (b . (c . ()))) .... (cons 1 2) This notation is used in the book as a notation for list and pair values. draw box and pointer diagrams for these ------------------------------------------ FOR YOU TO DO DOT NOTATION EQUIVALENTS value printed as expression dotted list ========================================= '() () () (cons 1 '()) (1 . ()) (1) (cons 1 (cons 2 '())) (cons 1 2) (cons (cons 1 '()) '()) ------------------------------------------ ... (1 . (2 . ())) (1 2) ... (1 . 2) ... ((1 . ()) . ()) ((1)) Q: What's the dot notation for: (cons 4 (cons 5 6)) (cons 'a 'b) ? Interpreter prints with dots only when it has to, otherwise uses list notation **** vectors random access (vs. lists, which are sequential) ------------------------------------------ VECTORS (vector T) Values + abstract: finite sequences of cells containing T objects + printed: #(), #(a b), #(c d e), ... Operations + procedures: vector, make-vector, vector-ref, vector-set!, vector-length, ... + syntax of literals: '#(), '#(a b),... Examples ------------------------------------------ ... '#(1 2 3) (vector 1 (+ 1 1) (+ 2 1)) - zero-based indexing (as in C++, but not Pascal) - may be heterogeneous (unlike C++, Pascal, like Smalltalk) - carry their length (convenient, unlike C/C++ length at run-time, unlike Pascal) ** type checking Q: What happens if you write (+ #t 3)? (car '())? ------------------------------------------ TYPE CHECKING AND TYPE ERRORS term: type error def: *dynamic type checking* means type errors are detected (in general) def: *static type checking* means type errors are detected ------------------------------------------ ... at run-time. ... before the program is run. Q: Which is better for users? Q: Which is more flexible? (+ 3 (if (zero? 0) 4 'done)) ; illegal in typed languages Q: Which suits interpreters better? Q: Which gives better performance?