University of California, Berkeley
EECS Department - Computer Science Division

CS3L OaW Lecture 10 : CAL (Cons Append List)

Announcements


Lists, aka "CAL : Cons, Append & List"

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  ;; list OR sentence
==> (1 2 3 4)
 
: (define complex '(a (b c) () ((d)) ) ) 
==> complex
 
: complex  ;; only lists can be 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)
: (cons 'cs3 '())
==> (cs3)
 
: (cons 'love (cons 'cs3 '()))
==> (love cs3)
 
: (cons 'i (cons 'love (cons 'cs3 '())))
==> (i 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))
==> (1 2 3 4)
 
: (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)
: (map list '(a b c d))
==> ((a) (b) (c) (d))
 
: (reduce append '((a) (b) (c) (d)) )
==> (a b c d)

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

Example (hard) problem : deep-add-1

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

 

 


Summary

In Lab this week you'll see...

In Life this week you'll see...