Lecture 7
Composite Data Objects - Representing the Real World
Programming as com/position
Representing a Student in CAESAR
What sort of information do we need to define a "student" in CAESAR?
Representing a Student in CAESAR
3.75
"Connor"
"Bain"
"Computer Science"
"McCormick"
28
gpa
first-name
last-name
major
school
credits
Representing a Student in CAESAR
3.75
"Connor"
"Bain"
"Computer Science"
"McCormick"
28
gpa
first-name
last-name
major
school
credits
first
second
third
fourth
fifth
sixth
Representing a Student in CAESAR
3.75
"Connor"
"Bain"
"Computer Science"
"McCormick"
28
gpa
first-name
last-name
major
school
credits
first
second
third
fourth
fifth
sixth
list structure (numbered)
record structure
(named)
Data Composition
Creating and Extract from Composite Data Objects
list (numbered) structures
Aka “structs”
Lists
Simple list constructors
(list item1 item2 … itemn)�;; list: T … -> (listof T)
Creates a list containing the specified items, in order
(append list1 list2 … listn)�;; append: (listof T) … -> (listof T)�
Creates a new list with all the items from the input lists, in order
Examples
> (define my-list (list 1 2 3))
(list 1 2 3)
> (append my-list (list 4 5 6))
(list 1 2 3 4 5 6)
> (rest (append my-list
(list 4 5 6)))
(list 2 3 4 5 6)
> (list)
'()
Simple list accessors
(length list)�;; length: (listof T) -> number��Returns the number of items in the list
(list-ref list index)�;; list-ref: (listof T) number -> T
�Extracts the element at position index (a number)
Simple list accessors
(first list), (second list), …, (eighth list)�;; first, etc. : (listof T) -> T��Extracts the specified element of list
(rest list)�;; rest: (listof T) -> (listof T)
�Returns all but the first element of list
CORRECTION: This only goes up to eighth!
Lists can…
> (list)
'()
> (length (list))
0
> (list "a" "b" "c" 1 2 3)
(list "a" "b" "c" 1 2 3)
> (list "a" (list "b" "c"))
(list "a" (list "b" "c"))
Examples
> (length (list "Lists can have"
(list "other" "lists")
"as elements"))
3
> (length (list "other" "lists"))
2
Examples
> (first (list 1 2 3))
1
> (list-ref (list 1 2 3) 0)
1
> (list-ref (list 1 2 3) 1)
2
> (first (list))
first: expects a non-empty list; given: '()
> (list-ref (list 1 2 3 4) 4)
list-ref: index too large
index: 4
in: (list 1 2 3 4)
Representing a Student in CAESAR
3.75
"Connor"
"Bain"
"Computer Science"
"McCormick"
28
gpa
first-name
last-name
major
school
credits
first (index 0)
second (index 1)
third (index 2)
fourth (index 3)
fifth (index 4)
sixth (index 5)
list structure (numbered)
record structure
(named)
record (named) structures
Aka “structs”
Looking inside record structures
Rectangle
Width: 10
Height: 10
Ellipse
Width: 15
Height: 10
Function
Name: iterated-overlay
Arguments: proc count
Body: [apply group� [up-to count
Color
R: 240
G: 220
B: 0
A new kind of CUSTOM object: the album
Note, in Exercise 3 we work with tracks instead of albums
Making new data types
(define-struct typename (field-names …))
Example: Making the album type
(define-struct album (title artist genre))
This defines 5 new functions for us automagically:
Note, in Exercise 3 we work with tracks instead of albums
Making an Album object
> (make-album "Montero"
"Lil Nas X"
"Pop rap")
(make-album "Montero"
"Lil Nas X"
"Pop rap")
Note, in Exercise 3 we work with tracks instead of albums
Accessing album fields
> (define-struct album
(title artist genre))
> (define my-album
(make-album "Midnights"
"Taylor Swift"
"Synth-pop"))
(make-album "Midnights"
"Taylor Swift"
"Synth-pop"))
> (album-title my-album)
"Midnights"
> (album-artist my-album)
"Taylor Swift"
Note, in Exercise 3 we work with tracks instead of albums
Writing our own Predicates
And we can write functions to work with our albums. For example, a predicate to test if a given album is a Beatles album:
;; Beatles?: album -> Boolean
;; Determines whether an album is by the Beatles
(define Beatles? (lambda (album)
(string=? (album-artist album)
"The Beatles")))
(check-expect (Beatles? (make-album "Title"
"The Beatles"
"Genre"
))
true)
(check-expect (Beatles? (make-album "Title"
"MS MR"
"Genre"))
false)