Lecture 8
Composite Data Objects - Representing the Real World
Logistics and Week Ahead
For some - Ethics Module 3 - Accountability
Reminders:
Quiz 1 - 10/13 Monday
Boolean expressions
Boolean objects - A new data type
Boolean constants
A new kind of expression: if
(if test consequent alternative)
Checks if a and b are the same number
Compares numbers
;; absolute-value: num -> num
;; Removes the sign from a num
> (define absolute-value
(λ (n)
(if (> n 0)
n
(- n))))
> (absolute-value 5)
5
> (absolute-value -5)
5
Note: absolute value is already in Racket and is called abs. We just use it here because it’s a simple example.
new rules of execution
Rules of computation in Racket
Look at the expression
Special cases: Rules of computation in Racket
If it starts with define
(define name value-expression)
If it starts with λ or lambda:
(λ (name1 … namelast) result-expression)
If it starts with the the word local
(local [(define name1 value1) � …� (define namelast valuelast)]� result-expression)
If it starts with if:
(if test consequent alternative)
Some other built-in tests
Tests what kind of data value is
Tests whether a number is odd or even
Combines tests into more complicated tests
Note: Racket sometimes calls "functions" a different word: procedures. Functionally, they're the same in our class, but this function here, which tests to see if the given value is a function is actually named procedure? Rather than function?
Examples
Suppose we say:
(define a 7)
(define b 8)
(define c 9.5)
What are the values of:
> (> a b)
>
> (< a b)
>
> (not (= b c))
>
> (integer? c)
>
> (odd? a)
>
> (and (< 5 a)
(< a 10))
>
Examples
Suppose we say:
(define a 7)
(define b 8)
(define c 9.5)
What are the values of:
> (> a b)
> #false
> (< a b)
> #true
> (not (= b c))
> #true
> (integer? c)
> #false
> (odd? a)
> #true
> (and (< 5 a)
(< a 10))
> #true
Executing a Boolean expression
Evaluating tests is really just normal function* execution
(and (< 5 a)� (< a 10))
*Truth in advertising: and isn’t actually a function, it’s a special form. So this slide is a lie. But it’s a useful lie.
Predicates (question answer-ers)
User-defined predicates
;; perfect-square?: number -> Boolean
;; True if arg is the square of an
;; integer
> (define perfect-square?
(λ (n)
(integer? (sqrt n))))
> (perfect-square? 4)
#true
> (perfect-square? 3)
#false
Testing equality
As with other languages, Racket has a few different notions of two objects being the same
So it has a few different functions for testing equality.
In general DON’T USE THESE ONES
(eq? a b)�The two arguments are literally the same object in memory
(eqv? a b)�Arguments are the same object or equal numbers.
(equal? a b)�The two arguments are equivalent objects. Works for numbers, strings, and some compound data.
Testing equality
As with other languages, Racket has a few different notions of two objects being the same
So it has a few different functions for testing equality.
Instead focus on THESE ONES
(= number1 number2)�True when the numbers are the same and throws an exception if they aren’t numbers.
(string=? string1 string2)�True when the strings are the same and throws an exception if they aren’t strings
(if time - probs Wednesday next week)
Got a lot of cases?
That's what cond is for!
(if C1 R1 R2) ; if C1 is true, then R1 otherwise R2
(if (> x 5) "big" "small")
(cond [C1 R1] ; if C1 is true, then R1
[C2 R2] ; otherwise if C2 is true, then R2
…
[Cn Rn]); otherwise if Cn is true, then Rn
(cond [(> x 5) "big"] ; if x > 5, then "big"
[(= x 5) "medium"] ; otherwise if x = 5, "medium"
[else "small"]) ; otherwise "small"
Compound data
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)