# CS3 OaW Lecture 12 : Advanced Lists

## Announcements

• Association lists
• Functions that take an arbitrary number of arguments
• Recursion on arbitrary sub-lists
• This week is Checkoff #1 for your final project

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

*** Error:
too few arguments to: (greetings (quote madonna))
Current eval stack:
__________________

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

• 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.
```;; 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)
(cond ((null? L) L)
((not (list? (car L))) ;; Always true since simple list
(cons (1+ (car 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))))
(cond ((null? L) L)
((not (list? (car L))) ;; no longer always true
(cons (1+ (car 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))

: (deep-add1-carcdr2 '((1 2 (3 4 5) 6) 7 (((8)))) )
((2 3 (4 5 6) 7) 8 (((9))))```
• Let's take yet another look at how we could have written the solution, this time using HOFs
• ```(define (deep-add1-hof Ln) ;; "Ln" because arg could be list OR number n
(if (number? Ln)
(+ Ln 1)

: (deep-add1-hof '((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...

• Time for your Gamesman project

## In Life this week you'll see...

• Masters winner celebrating
• The Dow trying to stay above 8K