meeting -*- Outline -*- * data types in Scheme Students should start reading chapter 1 and section 2.2 of SICP and chapters 1-2 of The Little Schemer Also, refer to the Revised^5 Report on Scheme for details Don't go into details that the students can look up when needed. Note: This studies Scheme from the perspective of languages in general, not just to program in Scheme. Students: note both the - terminology & perspectives - facts Keep drawing analogies to other languages (me too!) will help think about languages in general ** 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. ** characteristics Q: What characterizes (specifies) an Abstract Data Type (ADT)? values and operations ------------------------------------------ CHARACTERISTICS OF DATA TYPES IN PROGRAMMING LANGUAGES Characteristics - Values + abstract + external (printed form) - Operations + procedures + syntax of literals + special forms ------------------------------------------ *** booleans (quickly) ------------------------------------------ EXAMPLE, THE BOOLEANS - Values - Operations + procedures: not, eqv? + syntax of literals: #t, #f + special forms: if, cond, and, or ------------------------------------------ Don't usually distinguish the external (output) form from the syntax of literals (input) Special forms could be defined by macros in programming ... + abstract: true, false + external: #t, #f *** numbers See the R5RS for procedures on numbers, such as: + - * / zero? positive? negative? odd? even? < > <= >= abs quotient remainder modulo max min floor ceiling truncate round expt log Example: write a procedure "average" that takes two numbers and returns their average (define average (lambda (x y) (/ (+ x y) 2))) Q: Can you write a procedure "mod7" that returns the result of taking its argument modulo 7? examples: (mod7 8) ==> 1 (mod7 23) ==> 2 *** strings on computer... if possible show examples of, and briefly indicate main operations ------------------------------------------ STRINGS Values + abstract: finite sequences of chars + printed: "a string", "\"hi\"" Operations + procedures: string, string-append, string-ref (0 based), string-length, string=?, ... string "a s" (substring "a string" 1 3) ==> " s" (substring "a string" 2 3) ==> "s" (substring "a string" 3 3) ==> "" (substring "a string" 2 5) ==> "str" *** symbol ------------------------------------------ SYMBOLS like strings, but: - have constant time equality test, and - can't be mutated Values: + abstract: nonempty finite sequences of characters + printed: a-symbol, mySymbol, +, ... Operations: + procedures: eqv?, 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 (so also like stacks) '() 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 **** summary of lists ------------------------------------------ LISTS (list-of 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 DOT NOTATION VIEW !-----------! ( . ) ! | ! car cdr !-----------! car cdr (cons 1 (cons 2 '())) ------------------------------------------ draw pictures like ... [ * | *-]--->[ * | * ] (1 . (2 . ())) | | | v v v 1 2 () the following is acceptable, but prefer the one above [ 1 | *-]-->[ 2 | / ] so these are singly-linked lists *** pairs (improper lists) (quickly) 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 Can use $PUB/lib/dot-notation.scm to see what the dot-notation equivalent is. **** proper list examples Q: can you write a procedure to return the second element of a list? Q: how would you test that? Q: can you write a procedure to return the third element of a list? (define second (lambda (ls) (cadr ls))) Q: Can you write yodaize? (yodaize '(you will study)) ==> (study you will) (yodaize '(you must eat)) ==> (eat you must) *** vectors (quickly) random access (vs. lists, which are sequential) ------------------------------------------ VECTORS (vector-of 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/C++, Java, but not Pascal) - may be heterogeneous (unlike C++, Pascal, like Smalltalk) - carry their length (convenient, unlike C/C++, like Java, Smalltalk) - checked bounds (unlike C/C++, like Pascal, Java, Smalltalk) ** equality predicates See Revised^5 Report on Scheme, section on Equality predicates Play with eq? vs. equal? for lists, vectors, big numbers. --------------------------------------------------------- EQUALITY PREDICATES > (eq? (cons 1 '()) (cons 1 '())) #f > (equal? (cons 1 '()) (cons 1 '())) #t > (equal? (vector 1 2 3) (vector 1 2 3)) #t > (eq? (vector 1 2 3) (vector 1 2 3)) #f > (eq? 9876543210 9876543210) #f > (equal? 9876543210 9876543210) #t ---------------------------------------------------------