EECS Department - Computer Science Division

**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?

- 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
in the`key``key-data`pairs and return the first sublist whosematches. If no match is found, return`key``#f`.

- Find the

:(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

- 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

- This can be confusing, do lots of examples to study.
- We start by writing
`shallow-add1`, then modify it to go deep. - All the higher-order functions (
`map, filter, reduce`) only work on the top-level, they do not go deep. - Let's start with
`shallow-add1-recur`(recursive version)*;; shallow-add1-recur ;; ;; INPUTS : A simple list of numbers ;; REQUIRES : There be nothing but numbers in the list. ;; RETURNS : The original list with every number incremented by 1. ;; EXAMPLE : (shallow-add1 '(1 2 3) ) ==> (2 3 4)***(define (shallow-add1-recur L) (cond ((null? L) L) ((not (list? (car L)))***;; Always true since simple list***(cons (1+ (car L)) (shallow-add1-recur (cdr L))))))** - Now let's modify it to go deep!

;; 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))))

(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))))

`(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`

**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!

- Advanced lists
- Time for your Gamesman project

- The nation come to terms with its new President-Elect...