CS212 Handouts
Swindle Quick Reference
Swindle is an extension to the Scheme language that adds object oriented and
typing features. The features used for CS212 are listed below -- more will be
added as you learn them. Consult the Scheme
Quick Reference for inquiries about the Scheme language.
Notation
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.
- Things colored in red are meta-variables
(place holders).
- Things colored in blue are examples.
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. Swindle provides many built in types, to
handle numbers, strings, collections of objects, and more:
- <top>
- <object>
- <class>
- <struct>
- <generic>
- <method>
- <primitive>
- <sequence>
- <symbol>
- <boolean>
- <char>
- <number>
- <inexact>
- <complex>
- <exact>
- <void>
- <procedure>
Note: the types under <primitive> represent Scheme's primitive types,
many of them will not be used.
Class Special Forms
- (defstruct <structure>
slot1...)
OPTIONAL: (defstruct (<structure>
<superclass>) slot1...)
Defines a new new structure <structure>
(a class derived from <struct>) with given slots.
The slots must be a sequence of symbols or symbols with classes. If the
optional syntax is used, <structure>
inherits the slots from <superclass>.
<superclass>'s slots appear, in
order, before the slots defined for <structure>.
Also, creates auxiliary functions:
- (structure? x)
returns #t if and only if x
is an instance-of? <structure>
- (make-structure val1...)
creates a new instance of <structure>.
Each vali must
correspond to <structure>'s
slots as they were defined (and be of the right type if specified).
- (get-structure-slot
x) returns the value stored
in the slot slot of x.
x must be an instance of <structure>.
- (structure-slot
x) is the accessor for the
slot slot of x.
CS212 students may not use this form until defclass
is covered in class.
(defstruct <point> color (x
<integer>) (y <integer>))
Defines <point> as a structure, with the constructor make-point
that requires three arguments - the last two must be integers. Also, defines
point? as predicate that identifies <point>
instances. Also get accessor functions get-point-color, get-point-x,
and get-point-y.
- (defgeneric (name arg...))
Defines a generic function name with
arguments arg. The argument list for defgeneric
provides information about how many there are. defgeneric only
defines the new function symbol with a new generic object, and a specific
number of arguments.
- (defmethod (name arg...)
body...)
Creates a method with the specified types from the arg
list and adds it to the generic function name.
The argument list specifies the argument types that will make the generic
choose this method. NOTE: if you use defmethod without
previously evaluating a defgeneric for the symbol name,
then Swindle will automatically create a defgeneric using name
and args from the defmethod.
- (lambda (arg...) body...)
Returns a procedure that takes values as arguments. Each arg
is either a symbol or (symbol <type>) -- if no type is specified,
<top> is assumed. When applied to values, the values are checked to
see if they are the correct type, and if so, evaluates body.
- (defclass <class-name>
(super...)
(slot1 options)
...
(slotn options))
Creates a new type, <class-name>,
that inherits from the list of super
classes. If the list of super classes
is left empty, <class-name>
inherits from <top> only. <class-name>
holds information in slot1
through slotn, as well as
the slots that it inherits from the super
class list. Each slot comes with options:
- :type - this is constrains the values that can be in this
slot
- :initarg - when making a slot, use this to give the slot an
initial value
- :initvalue - if we don't specify a value in make, this is the
default
- :initializer - a function that takes a value, usually
ignored, and returns an init value
- :reader - creates a reader method that reads the contents of
a slot
- :writer - creates a writer method that overwrites the
contents of a slot (essentially set!)
- :accessor - This gives you both a reader and a writer method,
the writer takes the form set-x!
where x is the argument to this
option, also allows the use of (set! x val).
- (make <class-name>
:initarg1 val1
...
:initargn valn)
This creates an instance of an object of type <class-name>.
For any slot that used the :initarg option-word, you can use that :initarg
option-word to give that slot in this instance of <class-name> a
value. The order of the :initarg does not matter.
- (singleton x)
This identifies a class that only x is
an instance of, in other word, it specifies all objects that are eq?
to x. This is most useful when defining
the argument type pairs in a defmethod. For example, the argument
pair could be (y (singleton 1)), so
the generic function will only be executed if y
is 1. A singleton class is the most
specific class that any object can be an instance of, in other words, a
class cannot inherit from a singleton class. For argument type pairs in a defmethod,
(y = 1) is an alternative form to
writing (y (singleton 1)).
Other Special Forms
- (defmacro (name args...)
body...)
Defines a macro, name, that when
applied, transforms one S-expression to another. body
is simply a list of symbols with args
placed appropriately, such that it represents the resulting S-expression.
(defmacro (inc! i)
(list 'set! i (list '1+ i)))
Defines a macro that destructively increments a variable i by one.
Additional Functions
- (equals? x y)
Compares x and y,
returning #t if they are congruent. The default method uses equal?,
so it will return #f for two different (not eq?) objects
unless you define a method.
- (instance-of? x <class-name>)
Returns #t if x is an instance
of <class-name> or is a subclass
of <class-name>, and returns #f
otherwise. This should rarely be used since generic functions with method
dispatch should handle this.
- (call-next-method)
This is an argumentless function call, used within a defmethod that
calls the next best fitting defmethod with the same arguments as
the defmethod that evaluated call-next-method.
- (inc! x)
Destructively increments x by 1.
- (dec! x)
Destructively increments x by 1.
Echo Functions
- (echo expr...)
Displays the result of evaluating each expr
separated by spaces. There is a trailing newline. The return value is
unspecified.
- (echos expr...)
Returns a string with the results of evaluating each expr
separated by spaces. There is no newline.
- (echon expr...)
Same as echo except there is no trailing newline.
- (echo-ns expr...)
Same as echo, except the result of evaluating expr
is not separated by spaces.
- (echos-ns expr...)
Same as echos, except the result of evaluating expr
is not separated by spaces
- (echon-ns expr...)
Same as echon, except the result of evaluating expr
is not separated by spaces
Streams
- (cons-stream x y)
Returns a stream where x is evaluate
and the evaluation of y is delayed.
Note that cons-stream is a special form and does not evaluate y.
- (head-stream s)
Returns the head of the stream s.
- (tail-stream s)
Forces the evaluation of s's tail, and
returns the result.
- (null-stream? s)
Predicate that returns #t if s
is the null-stream, and #f otherwise.


© 1999 Cornell University Computer Science
Written by Brandon Bray and Eli Barzilay