CSCI 301 L02 Notes

Lecture 2 - Notes

Outcomes

Sets 1

Announcements

Recursive Problem Solving in Racket: Recursive Helpers

See L02.rkt.

The key idea is to pass information down the recursive rabbit hole using arguments.

As an example, we can compute the average in two passes like:

(/ (sum lst) (length lst))

But we can compute it in one using a helper function such as:

(define avg-helper
  (lambda (lst sum-so-far length-so-far)
    ; ...

because when the list is empty, we have the running sum and length already so we can simply divide them and return.

Set Basics

A set is an unordered collection of unique items. The items in a set are its elements.

This membership relationship is denoted \(a \in S\), read “\(a\) is an element of \(S\).”, or “\(a\) is in \(S\).” If \(a\) is not in \(S\), we write \(a \not\in S\).

You can write a set by listing its elements in curly braces (this is the “roster method”):

For example: If \(S = \{1, 2, 3, 4\}\), then \(1 \in S\) and \(2 \in S\), but \(0 \not\in S\).

Sets are unordered: changing the order you list the elements does not change the set:

\(S = \{2, 3, 4, 1\} = \{1, 2, 3, 4\}\), and so on.

Elements of a set must be unique: listing an element more than once does not change the set:

\(S = \{1, 2, 3, 4\} = \{1, 1, 2, 3, 4, 4\}\), and so on.

If the meaning is obvious, you can use ellipses (\(\ldots\)) to describe a set without listing all of its members:

\(A = \{\text{a}, \text{b}, \text{c}, \ldots, \text{z}\}\) describes the English alphabet.

Sets Important Enough to Have a Dedicated Symbol

Do Exercises Part A

“Set builder” notation

We saw the roster method last time. You can also specify the members of a set using “set-builder” notation: \[ \{\text{expression} : \text{membership test}\} \] For example, the set (written using the roster method) \(\{1, 3, 5, 7, 9\}\) could also be written in any of the following ways:

The colon is read “such that”, or “where”. Sometimes you’ll see a vertical bar (\(|\)) used instead of a colon (\(:\)).

Do Exercises Part B

Cardinality, Sets of Sets

Definition: If there are exactly \(n\) distinct elements in \(S\), and \(n\) is a nonnegative integer, then \(S\) is finite. Otherwise, \(S\) is infinite.

Definition: The cardinality of a finite set \(A\), denoted \(|A\)|, is the number of (distinct) elements in \(A\).

Examples:

Fact that can give rise to confusion: elements of a set can be sets themselves.

For example, let $ S = {{1, 2}, {2, 3}, }$. Some interesting facts:

Do Exercises Part C