Held: Friday, 23 January 2004
Today we consider many of the core types in Scheme, particularly
symbols, lists, and numbers.
- Please choose the subjects of your email messages carefully. In particular, if you have a question, title it something like "Question" or "Help".
- Like most programming languages, Scheme provides different types.
- What is a type? There are two key perspectives:
- Data-driven: A type is a set of values.
- Purpose-driven: A type provides information on the valid
operations that may be applied to a piece of data.
- We will alternate between the two definitions.
- Many languages (particularly the ones you've reported being familiar
with) require you to assign a type to a variable when you declare
- Scheme does not require you to assign types ot variables; it checks
the type of each operand when it executes a procedure.
- Scheme also provides procedures that let you determine the type
of a value.
- What are the advantages and disadvantages of Scheme's solution?
- Scheme provides a number of core types:
- Primitive types: Numbers, Symbols, Characters, Truth Values
- Compound types: Lists, Vectors (like Arrays), Strings,
- A key aspect of Scheme.
- The easiest way to build a symbol is with the quote operation.
reference to undefined identifier: cat
> (quote cat)
- Useful when you want to represent something atomically (as an idea
that can't be pulled apart).
- The core of Scheme.
- Dynamic: Lists can
- More precisely: You can build larger or smaller lists from other lists.
- The simplest list: the empty list is
- You extend a list at the front (or, more precisely, build a new list) with
(cons thing-for-the-front rest-of-the-list)
- For example,
> (cons 'a null)
> (cons 'b (cons 'a null))
- You can also create lists with
> (list 'a 'b 'c)
(a b c)
- You extract parts of lists with three operations (we'll reflect on
the naming later in the semester):
car: get the initial element of the list.
cdr: get all but the initial element of the list.
list-ref: get the ith element of the list.
- One disadvantage: Scheme lists look a lot like procedure
applications, so it's sometimes hard for beginning programmers
to tell what's going on.
- You can always tell by context, but it takes some time to
get used to contexts.
- Scheme provides a hierarchy of numeric types. include complex,
real, rational, and integer.
- You've already done some exploration of numbers, so there's not
much more to say.
- Any questions?
- Do the types lab.
- We'll try to use the last five minutes of class to reflect.
- You may have to complete the remainder of the lab on your own.
- You have a laboratory writeup
to complete on this lab.