Lecture 7 Slides - Lecture Slides

1 of 26

Lecture 7

Composite Data Objects - Representing the Real World

2 of 26

Programming as com/position

  • Expression composition
    • Primitive expressions: Constants, variables
    • Compound expressions: Function calls, special forms
  • Function composition
    • Primitive functions:�+,-,*,/, rectangle, overlay, etc.
    • Compound functions:�Created by λ expressions
  • Image composition
    • Primitive images: Rectangles, ellipses
    • Compound images: Overlays, iterated-overlays

3 of 26

Representing a Student in CAESAR

What sort of information do we need to define a "student" in CAESAR?

4 of 26

Representing a Student in CAESAR

3.75

"Connor"

"Bain"

"Computer Science"

"McCormick"

28

​

gpa

first-name

last-name

major

school

credits

​

5 of 26

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

​

6 of 26

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)

7 of 26

Data Composition

  • Primitive data types
    • Numbers, integers, strings, Booleans
  • Composite data types (structures)
    • List structures: numbered fields
    • Record structures: named fields (the fields available in an object are determined by its type)

8 of 26

Creating and Extract from Composite Data Objects

  • When we have functions that create composite data objects, we call these constructors
  • We'll also have functions that extract the constituent parts of composite data objects, we call these accessors

9 of 26

list (numbered) structures

Aka “structs”

10 of 26

Lists

  • Lists are ordered sequences of data objects
    • Any length
    • Any type of data object
    • Different types can be mixed in the same list
    • Notated as (listof type) in type signatures, where type is the type of data appearing in the list
    • In many procedures the type might just be a variable like T or X, meaning the procedure can handle lists of different kinds of elements
  • Scheme and Racket use a particular kind of list called a linked list
    • We’ll talk more about this in a couple weeks
  • Example: a NetID database
    • A list of string objects

11 of 26

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

12 of 26

Examples

  • Notice that like record structures, lists print as (list data …)
  • The one exception is the empty list (the list with no elements)
    • We’ll explain why later
    • which prints as '()

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

'()

13 of 26

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)

    • Index counts from zero
    • So for an 5-element list, the elements are numbered 0, 1, 2, 3, 4

​

14 of 26

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!

15 of 26

Lists can…

  • Be empty (no elements)
  • Mix different types of data
  • Have other lists inside them

> (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"))

16 of 26

Examples

  • The length of a list is the number of elements in it
  • When a list has a list within it, the sublist only counts as one element

> (length (list "Lists can have"

(list "other" "lists")

"as elements"))

3

​

> (length (list "other" "lists"))

2

​

17 of 26

Examples

  • List items are numbered from zero
    • First item = item 0
    • Second item = item 1
    • Third item = item 2
    • …
    • Last item = item (length list)-1
  • Asking for an element past the end of the list gives an IndexOutOfRangeException

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

​

18 of 26

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)

19 of 26

record (named) structures

Aka “structs”

20 of 26

Looking inside record structures

  • Data objects are like forms
    • They have fields
    • Filled in by values
  • The fields
    • Have names (width, height)
    • The fields are filled with other data objects
  • The object’s type (rectangle, ellipse) determines what fields it has

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

21 of 26

A new kind of CUSTOM object: the album

  • Let’s say we want to catalog our music collection
  • We’ll use a new kind of data object, the album
  • An album will have three fields, each holding a string:
    • Its title
      • "The Wall","The Fame Monster"
    • The artist who recorded it
      • "Frank Ocean","Frank Sinatra"
    • The album’s genre
      • "rock","hip hop","jazz","classical","comedy"

Note, in Exercise 3 we work with tracks instead of albums

22 of 26

Making new data types

(define-struct typename (field-names …))

  • Define-struct is a new kind of special form that makes new data types
  • Defines a set of new functions automagically:
    • (make-typename field-values …)�Makes a new object of the type, given values for its field
    • (typename? object)�Predicate that tests whether object is of the specified type
    • (typename-fieldname object)�Returns the value of the specified field of the object

23 of 26

Example: Making the album type

(define-struct album (title artist genre))

This defines 5 new functions for us automagically:

  • (make-album title artist genre)�;; make-album: string string string -> album��Makes a new album with the specified title, artist, and genre. (constructor)
  • (album? object)�;; album?: any -> Boolean��Returns true if object is an album, otherwise false
  • (album-title album), (album-artist album), (album-genre album)�;; album-title/artist/genre: album -> string ��Returns the title of the specified album object (accessors)

Note, in Exercise 3 we work with tracks instead of albums

24 of 26

Making an Album object

  • You make an album object by calling its constructor function, make-album
  • Its inputs are
    • The title of the album
    • Its artist
    • Its genre
  • Racket prints the result in the form:�(make-album title artist genre)

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

25 of 26

Accessing album fields

  • You can read the data out of the fields of the object using accessor functions
  • The convention for naming accessor functions is (again): type-field
  • So album-artist is the accessor that gives you the artist field of an album

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

26 of 26

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)

​