The Dylan Quick Reference
1. Notation
This is a brief summary of useful things you might want to know about Dylan. The more
complicated things are explained in handouts, this is just a quick reference.
Notation used in this handout:
- Things in fixed font are things that are typed in.
- Things in italics are parameters.
- "expr ..." means zero or more expressions.
- "expr1 expr2 ..." means one or more expressions.
- "[...]" means an optional syntax.
2. Identifiers
You can use letters, numbers and "=", "-", ">",
"<", "/", "*" and "+"
in identifiers (names). Numbers may not appear as the first character in an identifier.
3. Types
By convention, all types begin with a "<" and end with a ">".
There is a hierarchy of types, with various mechanisms for user defined classes. Dylan
provides many built in types, to handle numbers, strings, collections of objects, and
more:
- <object>
- <boolean>
- <character>
- <collection>
- <keyword>
- <error>
- <simple-error>
- <type-error>
- <function>
- <generic-function>
- <macro>
- <method>
- <number>
- <symbol>
- <type>
4. Numbers
There are two built-in number classes, <float> and <integer>.
<integer> provides integer values in the range -263...263-1.
5. Variables
Variable names can either be specified by an identifier or an (identifier
type) pair. The latter is used to force the variable referenced by identifier
to be of type type, and is the preferable form to use in Dylan. Specifying no type
is the same as the using (identifier <object>).
6. Booleans
- #t means true
- #f means false
In addition, any object other than #f means true as well. It is good
programming style however to use #t when you explicitly mean true.
7. Lists
- A <pair> is anything that can be taken apart with the head and tail
functions (see below).
- The constant "()" is the empty list constant.
- A <list> is either a <pair> or the empty list ().
8. Special Forms
Special forms are expressions that do not follow standard evaluation rules:
9. Equality Testing
- (id? x y): True if x and y reference the
same object.
- (= x y): True if x and y are
"congruent"; which roughly means if they print the same.
10. List Structure Operators
- (null? x): Returns #t if x is the the empty
list () and #f for all other objects.
- (pair x y): Returns a <pair> whose head
is x and whose tail is y.
- (head l): Returns the first element of the list l.
- (tail l): Returns the tail of the list l.
- (head-setter obj pair): set!s the head
of pair. Don't use until presented in class.
- (tail-setter obj pair): set!s the tail
of pair. Don't use until presented in class.
- (list x1 ...): Evalutes x1,
... and puts them into a list.
- (length l): Returns the length of the list l.
- (append list1 ...): Appends the lists
together.
- (reverse l): Turns the list l around.
- (member? obj l): Searches the list l for
something = to obj, returning #t if found, else #f.
11. Type Checking Operations
- (instance? obj class): Is obj an instance of
class class?
- (object-class obj): Returns the class of obj.
- (super-class? c1 c2): Is c1
a superclass of c2?
12. Arithmetic and Numeric Operators
- (= x y), (< x y), (>
x y), (<= x y), (>= x
y): Various forms of numerical comparison.
- (max x1 ...), (min x1
...): Return the maximum (minimum) of all x's.
- (+ x1 x2): Addition.
- (* x1 x2): Multiplication.
- (- x1 x2): Subtraction.
- (/ x1 x2): Division - result type
depends on the input argument types.
- (^ x1 x2): Power operator. Raises x1
to the x2 power.
- (- x): Negation -x.
- (/ x): Reciprocal 1/x.
- (remainder n1 n2): Remainder after
integer division.
- (modulo n1 n2): Clock arithmetic, n1
mod n2.
- (zero? x), (positive? x), (negative?
x): Checks whether x=0, x>0 or x<0.
- (even? x), (odd? x): Checks whether x
is even or odd
- (inc x), (dec x): Shorthand for (+
x 1) and (- x 1)
- (abs x): Absolute value of x.
- (truncate x): Integer closest to x whose absolute value
<= |x|.
- (sqrt x): Square root.
- (sin x), (cos x), (tan x):
Trigonometric functions.
13. Vector Operators
- (n-vector n): Creates a new vector of n elements (the
initial values of the elements is undefined), indexed from 0 to n-1.
- (vector x1 ...): Evalutes x1,
... and puts them into a vector.
- (length vec): Returns the length of the vector vec,
which is the number of elements it can store, (using constant time).
- (index vec i): Get i'th element of vec (in
constant time).
- (index-setter! i vec val): Store val at
element i of vec (in constant time).
14. Higher Order Functions
- (apply proc arg-list): Call proc with arguments
as arg-list.
- (map func list1 ...): Applies func
to the elements of list1, ... (that is, the first of each, the
second of each, etc.) and collects the results in a list. The order it does it is not
specified and probably not the one you want, but the order they appear in the result list
is the right one.
- (filter pred l): Returns the list of elements from l
which satisfy pred (it returns true when applied to them.
Last modified 4/5/98 by AN