Lecture 9
Manipulating Composite Data - Processing the Real World
Example: an album database
We can represent our music collection as a list of album objects from lecture 8
> (define database
(list (make-album "The white album"
"The Beatles"
"Rock")
(make-album "Sgt. Pepper’s Lonely Hearts Club Band"
"The Beatles"
"Rock")
(make-album "Pod"
"The Breeders"
"Rock")
(make-album "Dummy"
"Portishead"
"Triphop")))
> database
(list (make-album "The white album" "The Beatles" "Rock")
(make-album "Sgt. Pepper’s Lonely Hearts Club Band"
"The Beatles" "Rock")
(make-album "Pod" "The Breeders" "Rock")
(make-album "Dummy" "Portishead" "Triphop"))
list iteration:�searching lists
Searching (filtering)
(filter predicate list)�;; filter: (T -> Boolean) (listof T) -> (listof T)�
Returns all the list items satisfying predicate.
Examples
> (filter odd? (list 1 2 3 4 5 6))
(list 1 3 5)
> (filter number? (list 1 2 "three"))
(list 1 2)
> (filter string? (list 1 2 "three"))
(list "three")
filter in Action
5
0
1
2
3
4
6
Input List
filter in Action
5
0
1
2
3
4
6
even?
Output List
filter in Action
5
0
1
2
3
4
6
even?
0
Output List
filter in Action
5
0
1
2
3
4
6
even?
0
Output List
filter in Action
5
0
1
2
3
4
6
even?
0
Output List
filter in Action
5
0
1
2
3
4
6
even?
0
Output List
2
filter in Action
5
0
1
2
3
4
6
0
Output List
2
4
6
Finding all the Beatles albums
How do we find out what all the Beatles albums are in our database?
> ???
Finding all the Beatles albums
How do we find out what all the Beatles albums are in our database?
What if we filter using our Beatles? predicate from last time?
> ???
Finding all the Beatles albums
How do we find out what all the Beatles albums are in our database?
What if we filter using our Beatles? predicate from last time?
;; Beatles?: album -> Boolean
;; Determines whether an album is by the
;; Beatles
> (define Beatles? (lambda (album)
(string=? (album-artist album)
"The Beatles"))
> (filter Beatles? database)
(list (make-album "The white album"
"The Beatles"
"Rock")
(make-album "Sgt. Pepper’s Lonely Hearts Club Band"
"The Beatles"
"Rock"))
Is this any different?
> (filter (λ (album)
(string=? (album-artist album)
"The Beatles"))
database)
(list (make-album "The white album"
"The Beatles"
"Rock")
(make-album "Sgt. Pepper’s Lonely Hearts Club Band"
"The Beatles"
"Rock"))
Is this any different?
> (filter (λ (album)
(string=? (album-artist album)
"The Beatles"))
database)
(list (make-album "The white album"
"The Beatles"
"Rock")
(make-album "Sgt. Pepper’s Lonely Hearts Club Band"
"The Beatles"
"Rock"))
Now we can get a list of Beatles albums
What other questions can we ask?
Now we can get a list of Beatles albums
What other questions can we ask?
How about, how many Beatles albums do we have in the database?
Now we can get a list of Beatles albums
What other questions can we ask?
How about, how many Beatles albums do we have in the database?
Step 1. Get all the Beatles Albums
Step 2. Count how many there are.
How do you count the Beatles albums?
> (length (filter Beatles? database))
2
Reminder: you don’t have to memorize all these functions!
list iteration:�transforming lists
Mapping
(map function list)�;; map: (In -> Out) (listof In) -> (listof Out)
Runs function on every element and Outputs their return values as a new list
i.e. (map func (list a b c)) is equivalent to� (list (func a) (func b) (func c))
Examples�> (map (lambda (num) (* -1 num)) (list 1 2 3 4))
(list -1 -2 -3 -4)
map in Action
0
1
2
3
even?
Output List
map in Action
Output List
true
0
1
2
3
even?
map in Action
Output List
true
0
1
2
3
even?
false
map in Action
Output List
true
0
1
2
3
false
true
even?
map in Action
Output List
true
0
1
2
3
false
true
false
Getting the genres of all albums
> (map album-genre database)
(list "Rock" "Rock" "Rock"
"Triphop")
> (remove-duplicates
(map album-genre database))
(list "Rock" "Triphop")
Note: remove-duplicates takes a list and returns a new
copy of it with any duplicate elements removed.
Note 2: It’s not included in the student languages for Racket so instead we'll provide it for you in a separate RKT file (like iterated-images) for Tutorial 2 and Exercise 3
(define database
(list (make-album "The white album"
"The Beatles"
"Rock")
(make-album
"Sgt. Pepper’s Lonely Hearts"
"The Beatles"
"Rock")
(make-album "Pod"
"The Breeders"
"Rock")
(make-album "Dummy"
"Portishead"
"Triphop")))
list iteration:�aggregating lists
Using foldl and foldr
Folding
(foldl func start-value list)
(foldr func start-value list)
;; foldl/foldr : (X Y -> Y) Y (listof X) -> Y
Aggregates (or folds together) all the values of list together using func
(foldl func start (list a b c)) is equivalent to:� (func c (func b (func a start)))
(foldr func start (list a b c) is equivalent to
(func a (func b (func c start))
foldl and foldr in Action
Output Value
0
1
2
3
+
(foldl + 0 (list 1 2 3))
foldl and foldr in Action
Output Value
1
2
3
+
(foldl + 0 (list 1 2 3))
(+ 1 0)
foldl and foldr in Action
Output Value
1
2
3
+
(foldl + 0 (list 1 2 3))
(+ 1 0)
foldl and foldr in Action
Output Value
1
2
3
+
(foldl + 0 (list 1 2 3))
(+ 2 (+ 1 0))
foldl and foldr in Action
Output Value
1
2
3
+
(foldl + 0 (list 1 2 3))
(+ 3 (+ 2 (+ 1 0)))
foldl and foldr in Action
Output Value
1
2
3
(foldl + 0 (list 1 2 3))
6
foldl and foldr in Action
Output Value
0
1
2
3
+
(foldr + 0 (list 1 2 3))
foldl and foldr in Action
Output Value
(+ 3 0)
1
2
3
+
(foldr + 0 (list 1 2 3))
foldl and foldr in Action
Output Value
(+ 2 (+ 3 0))
1
2
3
+
(foldr + 0 (list 1 2 3))
foldl and foldr in Action
Output Value
(+ 1 (+ 2 (+ 3 0)))
1
2
3
+
(foldr + 0 (list 1 2 3))
foldl and foldr in Action
Output Value
6
1
2
3
(foldr + 0 (list 1 2 3))
Averaging using foldl
;; sum: (listof number) -> number
;; The of the numbers in the list
(define sum
(λ (list) (foldl + 0 list)))
(check-expect (sum (list 1 2 3))
6)
;; mean: (listof number) -> number
;; The average of the numbers in the list
(define mean
(λ (list)
(∕ (sum list)
(length list))))
(check-expect (mean (list 1 2 3))
2)
misc list functions
Testing items in a list
(ormap predicate list)�;; ormap: (T -> Boolean)�;; (listof T)�;; -> boolean�
True when any item satisfies predicate
(andmap predicate list)�;; andmap: (T -> Boolean)�;; (listof T)�;; -> boolean
�True when every item in satisfies predicate
(define database
(list (make-album
"The white album"
"The Beatles"
"Rock")
(make-album
"Sgt. Pepper’s Lonely Hearts"
"The Beatles"
"Rock”)
(make-album "Pod"
“The Breeders”
“Rock”)
(make-album “Dummy”
“Portishead”
“Triphop”)))
(ormap Beatles? database)
#true
(andmap Beatles? database)
#false
Building arbitrary lists
(build-list n function)
Creates a list of n numbers by applying function to the integers from 0 to n - 1 in order.
> (build-list 5 (lambda (x) (* x x)))
(list 0 1 4 9 16)
Last, but not least, the apply function
(apply function list)
Calls function and passes it the elements of list as inputs
> (apply + (list 1 2 3 4 5))
15
> (apply max (list 1 2 3 4 5 1))
5
> (apply list-ref
(list (list 1 2 3 4 5)
2))
3
> (apply append
(list (list 1 2)
(list 3 4)
(list 5 6)))
(list 1 2 3 4 5 6)
The mystery revealed
(define iterated-overlay
(λ (func count)� (apply overlay� (build-list count func))))
A cookbook
How do I search for something?
How do I find all the elements of a list that have some property?
How do I search for something?
How do I get info about elements of a list?
How do I find some attribute of every element of a list?
How do I get info about elements of a list?
How do I summarize a list?
How do I aggregate all the items in a list into one item (e.g. the sum, product or max of the list)?
How do I summarize a list?