Meeting 04 - Data Types

Bor-Yuh Evan Chang

Thursday, September 5, 2024

\(\newcommand{\TirName}[1]{\text{#1}} \newcommand{\inferrule}[3][]{ \let\and\qquad \begin{array}{@{}l@{}} \TirName{#1} \\ \displaystyle \frac{#2}{#3} \end{array} } \newcommand{\infer}[3][]{\inferrule[#1]{#2}{#3}} \)

Meeting 04 - Data Types

In-Class Slides
In-Class Jupyter
In-Class Jupyter of Homework 1
Book Chapter

Reminders

  • Recitation sections are lab sections — bring your laptop!
    • This Week: Finishing HW1

Announcements

  • HW 1 + Quiz 1 due Friday Monday 6pm (with the 24-hour grace period for when “stuff happens”)

Announcements

  • Bug in the filterPairsByBound (and the filterPairsByBoundLinearTime) "test1".
    • We’ll look at the fix in a moment and learn something from it.
    • If you accepted the assignment to create your homework repo after 12am this morning, you have the updated version.
    • If you have the old version, the easiest way to update is to download the new version from https://github.com/csci3155-f24/pppl-hw1 and then copy the cells that you’ve edited so far.
    • If you want to get experience merging with git, there are some details on Piazza.

Today

Your Questions?

  • Review:
    • What is static type checking? What is static versus dynamic?
    • What is a value environment?

Your Questions?

Lists

Nil and Cons

Nil and Cons

Because Lists are immutable, prepending is still a constant-time operation (i.e., \(O(1)\)). The consZero and consTen can share the same tail (i.e., numbers).

How many nodes are needed total to represent the lists numbers, consZero, and consTen? 5

Immutability and Efficiency

Lists and Pattern Matching

Lists and Pattern Matching

Linear-Time Append

Why must append ::: be a linear-time operation?

Iterators

Imperative Sum

defined function sum
res16_1: Int = 6

Functional Sum

Options

Options and Error Handling

Option is a collection

Maps

Maps are particularly useful data structures for storing associations between keys and values.

Extending Maps

Data Classes

Defining Option

Parametric Polymorphism

Observe that getOrElse (as well as the MyNone and MySome constructors) have a type parameter list (written brackets []) and a value parameter list (written in parentheses ()).

The getOrElse function is generic in the parametrized type A. Being generic, the getOrElse function is also called parametric polymorphic.