Lecture 1, June 19
The lecture notes I post serve two purposes: to remind you of the topics we discussed and to provide any interesting code examples I did during lecture. I would describe these notes as an outline, not a summary, of what I talked about during lecture. You are not expected to be able to learn the material simply from examining these notes, nor is reading these notes a reasonable substitute for attending lecture.
Types
Simple Types
Number
There are many different subtypes of numbers. You won't have to know all of them, but you should be familiar with a few common ones. In particular, Integer, Exact-Rational, Real, and Complex are all types you should be familiar with.
Examples: 3, pi, 7/9, -8
Boolean
In contrast to the Number type, which may have infinitely many values, the Boolean type has only two values, true and false. These are represented by true or #t and false or #f in DrRacket.
Examples: true, false
String
The String type is used for representing text.Examples: "Hello", "World", ""
Complex Types
Union
Union types are for expressions that can have different types. For example, if an expression could be either a string or a number, that expression has type (U String Number).
Function
Function types are how we describe the types that operations take as inputs and produce as outputs. We use the -> symbol to create function types from other types. An example would be something like (-> Number Number Number), which indicates a function that takes two numbers as inputs and produces a single number as output.
Common Operations
On Numbers
+, -, /, *, sqrt, sin, cos, expt, =, <, <=, >, >=, remainder, log, modulo
On Booleans
not, and, or
On Strings
string-length, string-append, string-ith, number->string, string->number
Using DrRacket
Prefix notation
Expressions are always built using prefix notation in DrRacket
Definitions
We can define variables using (define VAR EXPRESSION).
You can specify the type of a variable with (: VAR : TYPE). In this class you will always be expected to put a type declaration before every definition you write.
Other
Expression Trees
You can write a tree representation of any expression. These trees are evaluated from the bottom up.Terminology
- Node - Any constant value or operation in the tree. These are drawn as circles.
- Edge - A line connecting two nodes.
- Child - The inputs to an operation are its children. They are drawn below the operation and connected to it by edges.
- Parent - An operation that takes some inputs is the parent of those inputs. It is drawn above the inputs and connected to them by edges.
- Leaf node - A leaf node is a node with no children. These all represent simple constants.
- Internal node - A node that has children is an internal node. These all represent operations that take inputs.
- Root - A node with no parents. Every expression tree has exactly one root.