University of California, Berkeley
EECS Department - Computer Science Division

CS3 Lecture 19 : Lists


Overview of today's lecture


Review

Tic Tac Toe project


Abstraction

Overview

Abstraction is one of the REALLY BIG IDEAS of this course!

Lists

Overview

: (define an-illegal-sentence
    '(cs3 is great (but that is my (dans) opinion) and fun))

: (define another-illegal-sentence
    '(#f #t #t))
: '(yo whassup GoBears CS3isNumber 1)
==> (yo whassup gobears cs3isnumber 1
: (+ 3 5)
==> 8

: (+ 1 (/ 4 2) (+ (- 5 1) (remainder (truncate 4.9) 3))) 
==> 8
: (define simple '(1 2 3 4) )
==> simple

: simple
==> (1 2 3 4)

: (define complex '(a (b c) ((d)) ) )
==> complex

: complex
==> (a (b c) ((d)))

Data Structures

: (define meals 
          '((breakfast (3 rasberry poptarts)
                       (7 eggs)
                       (5 mango smoothies))
            (lunch     (2 advil)
                       (3 burritos)
                       (4 yoo-hoos))
            (dinner    (3 pepto-bismol)
                       (4 advil)
                       (2 nyquil)
                       (1 pearl milk tea))))

: meals
==> ((breakfast (3 rasberry poptarts) 
                (7 eggs)
                (5 mango smoothies))
     (lunch     (2 advil)
                (3 burritos)
                (4 yoo-hoos))
     (dinner    (3 pepto-bismol)
                (4 advil)
                (2 nyquil)
                (1 pearl milk tea)))

Selectors and Constructors Overview

Constructors

: '()
==> ()
: '(this is a list)
==> (this is a list)s
: (cons 'cs3 '())
==> (cs3)

: (car '(cs3))
==> cs3

: (cdr '(cs3))
==> ()

: (cons 'love (cons 'cs3 '()))
==> (love cs3)

: (cons 'i (cons 'love (cons 'cs3 '())))
==> (1 love cs3)

: (cons '(1 2) '(3 4))
==> ((1 2) 3 4)

: (list 1 2 5 'three-sir 3)
==> (1 2 5 three-sir 3)

: (list 1 2 5 '(3 sir) 3)
==> (1 2 5 (3 sir) 3)

: (append '(1) '(2 (3)) '() '(4 5 6) '(((7))) )
==> (1 2 (3) 4 5 6 ((7)))

Selectors

: (define my-list
    '(cs3 is great (but that is my (dans) opinion) and fun))

: my-list
==> (cs3 is great (but that is my (dans) opinion) and fun)

: (car my-list)
==> cs3

: (cdr my-list)
==> (is great (but that is my (dans) opinion) and fun)

: (cdr (cdr my-list))
==> (great (but that is my (dans) opinion) and fun)

: (cdr (cdr (cdr my-list)))
==> ((but that is my (dans) opinion) and fun)

: (cdr (cdr (cdr (cdr my-list))))
==> (and fun)
: (car '())
*** ERROR -- PAIR expected
(car '())

: (cdr '())
*** ERROR -- PAIR expected
(cdr '())
: my-list
==> (cs3 is great (but that is my (dans) opinion) and fun)

: (car (cdr (cdr (cdr my-list))))
==> (but that is my (dans) opinion)

: (cadddr my-list)
==> (but that is my (dans) opinion

Programming with Lists

: (define (are-you-a-list l)
    (if (null? l) '()
        (cons (list? (car l))
              (are-you-a-list (cdr l)))))

: (are-you-a-list '(a (b c) () ((d))) )
==> (#f #t #t #t)
: (define (are-you-a-list-broken l)
    (if (null? l) '()
        (list (list? (car l))
              (are-you-a-list-broken (cdr l)))))

: (are-you-a-list-broken '(a (b c) () ((d))) )
==> (#f (#t (#t (#t ()))))

The Truth about Sentences

Higher-Order Functions

: (define (are-you-a-list-hof l)
    (map list? l))

: (are-you-a-list-hof '(a (b c) () ((d))) )
==> (#f #t #t #t)
: (filter list? '(a (b c) () ((d))) )
==> ((b c) () ((d)))

: (filter word? '(a (b c) () ((d))) )
==> (a)
: (reduce (lambda (x y) 
            (if (> (length x) (length y)) x y))
          '(a (b c) () ((d))) )
==> (b c)

Other Primitives for Lists: length, null?, list?, list-ref, equal?, member

length

: (length '(1 2 3 4) )
==> 4

: (length '(a (b c) () ((d))) )
==> 4

: (length '())
==> 0

null?

: (null? '(1 2 3 4) )
==> #f

: (null? '() )
==> #t

list?

: (list? '(1 2 3 4) )
==> #t

: (list? '() )
==> #t

: (list? 'cs3 )
==> #f

list-ref

: (list-ref '(a b c d) 1)
==> b

: (list-ref '(a b c d) 5)
*** ERROR -- PAIR expected
(list-ref '(a b c d) 5)

: (list-ref '(a (b c) () ((d))) 3)
==> ((d))

: (list-ref '() 0)
*** ERROR -- PAIR expected
(list-ref '(a b c d) 5)

equal?

: (equal? '(a (b c) () ((d))) (a (b c) () ((d))) )
==> #t

member

: (member '(b c) '(a (b c) () ((d))) )
==> ((b c) () ((d)))

: (member 'a-new-word '(a (b c) () ((d))) )
==> #f

Association Lists

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

Recursion on Arbitrary Structured Lists

;; deep-add-1
;;
;; INPUTS       : A deep list of numbers
;; REQUIRES     : There be nothing but numbers (and lists of #s) in the list.
;; SIDE-EFFECTS : None
;; RETURNS      : The original list with every number incremented by 1.
;; EXAMPLE      : (deep-add-1 '(1 5 2) ) ==> (2 6 3)
;;              : (deep-add-1 '((1 2 (3 4 5) 6) 7 (((8)))) )
;;              : ==> ((2 3 (4 5 6) 7) 8 (((9))))

: (define (deep-add-1 l)
    (cond ((null? l) '())
          ((number? l) (+ l 1))
          (else (cons (deep-add-1 (car l))
                      (deep-add-1 (cdr l))))))

: (deep-add-1 '(1 (2 ((4)) 3) (5)) )
==> (2 (3 ((5)) 4) (6))


Summary

Next Time

Puzzle : Unmarked Clock [courtesy Yeh-Kai Tung (yktung@physics.Berkeley.EDU)]

Game : Dots and Boxes ["Pentagames" by Pentagram, Fireside Publishing, 1990]

o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o