University of California, Berkeley
EECS Department - Computer Science Division
CS3 OaW Lecture 12 : Advanced Lists
Announcements
- Today -- Advanced lists:
- Association lists
- Functions that take an arbitrary number of arguments
- Recursion on arbitrary sub-lists
- Tomorrow is a holiday ... would you like us to:
- 1. Cancel wednesday's labs too
- 2. Make wednesday's labs a drop-in "office hours" of sorts (and even add 3 hours to them)
- This week is Checkoff #1 for your final project
- Other admin questions?
Association Lists
- An association list is a data structure built on lists.
- The idea is to have a list of keys and data: ((key1 data2) (key2 data2) ...)
- (assoc key association-list)
- Find the key in the key-data pairs and return the first sublist whose key matches. If no match is found, return #f.
: (define my-a-list '((dan garcia) (george bush) (bill clinton) (bill cosby)))
: (assoc 'dan my-a-list)
(dan garcia)
: (assoc 'bill my-a-list)
(bill clinton)
: (assoc 'raoul my-a-list)
#f
Functions with an arbitray number of arguments
- A function often needs an arbitrary number of arguments
- You specify the mandatory arguments first, then a dot ".", then a variable which contains the list of other arguments
- In general, you can either accept a list as argument and process it that way or process the arguments themselves
- Example:
;; greetings
;;
;; INPUTS : A first name, last name and (optional) letters
;; RETURNS : A greeting for the person
(define (greetings first-name last-name . letters)
(append (list 'hello first-name last-name) letters))
: (greetings 'madonna)
*** Error:
too few arguments to: (greetings (quote madonna))
Current eval stack:
__________________
0 (greetings (quote madonna))
: (greetings 'barack 'obama)
(hello barack obama)
: (greetings 'maick 'griebenow 'md 'dds 'phd 'frcmfs)
(hello maick griebenow md dds phd frcmfs)
- In general, you can either accept a list as argument and process it that way or process the arguments themselves
Recursion on Arbitrary Structured Lists (car/cdr recursion)
;; deep-add1-carcdr1
;;
;; INPUTS : A simple list of numbers
;; REQUIRES : There be nothing but numbers (and lists of #s) in the list.
;; RETURNS : The original list with every number incremented by 1.
;; EXAMPLE : (deep-add1-carcdr1 '(1 2 3) ) ==> (2 3 4)
;; : (deep-add1-carcdr1 '((1 2 (3 4 5) 6) 7 (((8)))) )
;; : ==> ((2 3 (4 5 6) 7) 8 (((9))))
(define (deep-add1-carcdr1 L)
(cond ((null? L) L)
((not (list? (car L))) ;; no longer always true
(cons (1+ (car L))
(deep-add1-carcdr1 (cdr L))))
(else (cons (deep-add1-carcdr1 (car L))
(deep-add1-carcdr1 (cdr L))))))
: (deep-add1-carcdr1 '((1 2 (3 4 5) 6) 7 (((8)))) )
((2 3 (4 5 6) 7) 8 (((9))))
Let's take another look at how we could have written the car-cdr recursion
(define (deep-add1-carcdr2 Ln) ;; "Ln" because arg could be list OR number n
(cond ((null? Ln) Ln)
((not (list? Ln)) (+ Ln 1))
(else (cons (deep-add1-carcdr2 (car Ln))
(deep-add1-carcdr2 (cdr Ln))))))
: (deep-add1-carcdr2 '((1 2 (3 4 5) 6) 7 (((8)))) )
((2 3 (4 5 6) 7) 8 (((9))))
Now let's try to write some fun procedures:
- (flatten L), which flattens a complex list L into a simple one
- (deep-reduce f base L), which reduces a complex list L with function f and base case base just like reduce
Summary
- Today's big ideas: Lists are very powerful
- You can use association lists for a 'database' of information
- You can now write functions with an arbitray number of arguments
- You can now do things to deep lists!
In Lab this week you'll see...
- Advanced lists
- Time for your Gamesman project
In Life this week you'll see...
- The nation come to terms with its new President-Elect...