CSCI 301 L01 Notes

Lecture 1 - Notes

Note: italicized outcomes are the ones we didn’t get to during Lecture 1.

Outcomes:

Sets 1
Racket 1

Announcements

Outline

Syllabus
Sets 1
“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 A

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 B

(End of what we covered in Lecture 1)

**********************************************************
 [time check - skip C and move to Racket if time is short]
Subsets and Powersets

Definition: Suppose \(A\) and \(B\) are sets. If every element of \(A\) is also an element of \(B\), then \(A\) is a subset of \(B\); this is denoted \(A \subseteq B\).

For example, let:

Then the following are true:

  1. \(A \subseteq B\)
  2. \(B \subseteq A\)
  3. \(C \subseteq A\)
  4. \(C \not\in A\)
  5. \(A \not\subseteq C\)
  6. \(\{n^2 : n \in \mathbb{Z}\} \subseteq \mathbb{Z}\)

Aside: If \(A \subseteq B\) and \(A \ne B\), then \(A\) is a proper subset of \(B\), denoted \(A \subset B\).

Definition: The power set of a set \(A\) is the set of all subsets of \(A\). It is denoted \(\mathcal{P}(A)\), or sometimes \(2^A\). In other words, \(\mathcal{P}(A) = \{S : S \subseteq A\}\).

Do Exercises Part C

Racket 1
Defining global variables
(define base 4) ; syntax: (define <symbol> <value>)
(define height 2)
base

; area of a triangle with base 4 and height 2 = 1/2 * base * height
(/ (* base height) 2)
Functions (or procedures) are created with the lambda special form:
(lambda (x) (+ x 2)) ; evaluates to a function (or procedure)
((lambda (x) (+ x 2)) 4) ; applies the function to the argument 4

(define addxy (lambda (x y) (+ x  y))) ; store a function in a global variable
(addxy 4 8) ; apply the function to 2 arguments
Lexical Scope

Scope in Racket is lexical - variables are visible only within a certain segment of the program text. define creates global variables whose scope is the entire program.

Function parameters (x and y, in the addxy example above) are local variables. They’re only visible inside the procedure body, and they shadow any global variable with the same name.

(define x 1)
(addxy 4 5) ; should be 9, because the x inside addxy gets bound to 4, shadowing global x

If you want to create local variables that are not function parameters, you can do that with let:

(let ((var1 val1)
      (var2 val2)
      (var3 val3))
  (expression-using-variables))
  
; var1 and friends are not visible here
(define a 4)

(let ((a 1) ; shadows the global a
      (b 2))
  (+ a b))

The local variables initializations happen (conceptually, though not actually) in parallel. This means a later one can’t reference an earlier one:

(let ((a 1)
      (b (+ a 1))) ; error: a is undefined!
  (+ a b)) 

We could nest lets to make this work:

(let ((a 1))
  (let ((b (+ a 1)))
    (+ a b))) ; this works

There’s a shorthand syntax for this - let*:

(let* ((a 1)
       (b (+ a 1)))
  (+ a b))
Conditionals

(if test val-if-true val-if-false)

(define absolute-value ; abs is a builtin
  (lambda (x)
    (if (< x 0)
        (- x)
        x)))

Equivalent to your familiar if/elseif/elseif/else chain:

(cond ((test1 val-if-test1)
       (test2 val-if-test2)
       (else val-otherwise)))
(define sign
  (lambda (x)
    (cond ((< x 0) -1)
          ((= x 0) 0)
          (else 1))))

Do Exercises Part D - I recommend working through these at home this weekend if we don’t have time in class.