[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A special form is an expression that follows special evaluation rules. This chapter describes the basic Scheme special forms.
2.1 Lambda Expressions 2.2 Lexical Binding 2.3 Dynamic Binding 2.4 Definitions 2.5 Assignments 2.6 Quoting 2.7 Conditionals 2.8 Sequencing 2.9 Iteration 2.10 Structure Definitions
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
lambda
expression evaluates to a procedure. The environment in
effect when the lambda
expression is evaluated is remembered as
part of the procedure; it is called the closing environment. When
the procedure is later called with some arguments, the closing
environment is extended by binding the variables in the formal parameter
list to fresh locations, and the locations are filled with the arguments
according to rules about to be given. The new environment created by
this process is referred to as the invocation environment.
Once the invocation environment has been constructed, the
expressions in the body of the lambda
expression are
evaluated sequentially in it. This means that the region of the
variables bound by the lambda
expression is all of the
expressions in the body. The result of evaluating the last
expression in the body is returned as the result of the procedure
call.
Formals, the formal parameter list, is often referred to as a lambda list.
The process of matching up formal parameters with arguments is somewhat involved. There are three types of parameters, and the matching treats each in sequence:
condition-type:wrong-number-of-arguments
is signalled;
this error is also signalled if there are more arguments than required
parameters and there are no further parameters.
condition-type:wrong-number-of-arguments
is signalled.
The predicate default-object?
, which is true only of default
objects, can be used to determine which optional parameters were
supplied, and which were defaulted.
In Scheme, unlike some other Lisp implementations, the list to which a rest parameter is bound is always freshly allocated. It has infinite extent and may be modified without affecting the procedure's caller.
Specially recognized keywords divide the formals parameters into these three classes. The keywords used here are `#!optional', `.', and `#!rest'. Note that only `.' is defined by standard Scheme -- the other keywords are MIT Scheme extensions. `#!rest' has the same meaning as `.' in formals.
The use of these keywords is best explained by means of examples. The following are typical lambda lists, followed by descriptions of which parameters are required, optional, and rest. We will use `#!rest' in these examples, but anywhere it appears `.' could be used instead.
(a b c)
a
, b
, and c
are all required. The procedure must
be passed exactly three arguments.
(a b #!optional c)
a
and b
are required, c
is optional. The procedure
may be passed either two or three arguments.
(#!optional a b c)
a
, b
, and c
are all optional. The procedure may be
passed any number of arguments between zero and three, inclusive.
a
(#!rest a)
a
is a rest parameter. The
procedure may be passed any number of arguments. Note: this is the only
case in which `.' cannot be used in place of `#!rest'.
(a b #!optional c d #!rest e)
a
and b
are required, c
and d
are optional,
and e
is rest. The procedure may be passed two or more
arguments.
Some examples of lambda
expressions:
(lambda (x) (+ x x)) => #[compound-procedure 53] ((lambda (x) (+ x x)) 4) => 8 (define reverse-subtract (lambda (x y) (- y x))) (reverse-subtract 7 10) => 3 (define foo (let ((x 4)) (lambda (y) (+ x y)))) (foo 6) => 10 |
named-lambda
special form is similar to lambda
, except
that the first "required parameter" in formals is not a
parameter but the name of the resulting procedure; thus
formals must have at least one required parameter. This name has
no semantic meaning, but is included in the external representation of
the procedure, making it useful for debugging. In MIT Scheme,
lambda
is implemented as named-lambda
, with a special name
that means "unnamed".
(named-lambda (f x) (+ x x)) => #[compound-procedure 53 f] ((named-lambda (f x) (+ x x)) 4) => 8 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The three binding constructs let
, let*
, and letrec
,
give Scheme block structure. The syntax of the three constructs is
identical, but they differ in the regions they establish for their
variable bindings. In a let
expression, the initial values are
computed before any of the variables become bound. In a let*
expression, the evaluations and bindings are sequentially interleaved.
And in a letrec
expression, all the bindings are in effect while
the initial values are being computed (thus allowing mutually recursive
definitions).
MIT Scheme allows any of the inits to be omitted, in which case the corresponding variables are unassigned.
Note that the following are equivalent:
(let ((variable init) ...) expression expression ...) ((lambda (variable ...) expression expression ...) init ...) |
Some examples:
(let ((x 2) (y 3)) (* x y)) => 6 (let ((x 2) (y 3)) (let ((foo (lambda (z) (+ x y z))) (x 7)) (foo 4))) => 9 |
See section 2.9 Iteration, for information on "named let
".
let*
is similar to let
, but the bindings are performed
sequentially from left to right, and the region of a binding is that
part of the let*
expression to the right of the binding. Thus
the second binding is done in an environment in which the first binding
is visible, and so on.
Note that the following are equivalent:
(let* ((variable1 init1) (variable2 init2) ... (variableN initN)) expression expression ...) (let ((variable1 init1)) (let ((variable2 init2)) ... (let ((variableN initN)) expression expression ...) ...)) |
An example:
(let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))) => 70 |
letrec
expression as its region, making it possible to
define mutually recursive procedures.
MIT Scheme allows any of the inits to be omitted, in which case the corresponding variables are unassigned.
(letrec ((even? (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88)) => #t |
One restriction on letrec
is very important: it shall be possible
to evaluated each init without assigning or referring to the value
of any variable. If this restriction is violated, then it is an
error. The restriction is necessary because Scheme passes arguments by
value rather than by name. In the most common uses of letrec
,
all the inits are lambda
or delay
expressions and
the restriction is satisfied automatically.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The syntax of this special form is similar to that of let
, but
fluid-let
temporarily rebinds existing variables. Unlike
let
, fluid-let
creates no new bindings; instead it
assigns the value of each init to the binding (determined
by the rules of lexical scoping) of its corresponding variable.
MIT Scheme allows any of the inits to be omitted, in which case the corresponding variables are temporarily unassigned.
An error of type condition-type:unbound-variable
is signalled if
any of the variables are unbound. However, because
fluid-let
operates by means of side effects, it is valid for any
variable to be unassigned when the form is entered.
Here is an example showing the difference between fluid-let
and
let
. First see how let
affects the binding of a variable:
(define variable #t) (define (access-variable) variable) variable => #t (let ((variable #f)) (access-variable)) => #t variable => #t |
access-variable
returns #t
in this case because it
is defined in an environment with variable
bound to
#t
. fluid-let
, on the other hand, temporarily reuses an
existing variable:
variable => #t (fluid-let ((variable #f)) ;reuses old binding (access-variable)) => #f variable => #t |
The extent of a dynamic binding is defined to be the time period during which the variable contains the new value. Normally this time period begins when the body is entered and ends when it is exited; on a sequential machine it is normally a contiguous time period. However, because Scheme has first-class continuations, it is possible to leave the body and then reenter it, as many times as desired. In this situation, the extent becomes non-contiguous.
When the body is exited by invoking a continuation, the new value is saved, and the variable is set to the old value. Then, if the body is reentered by invoking a continuation, the old value is saved, and the variable is set to the new value. In addition, side effects to the variable that occur both inside and outside of body are preserved, even if continuations are used to jump in and out of body repeatedly.
Here is a complicated example that shows the interaction between dynamic binding and continuations:
(define (complicated-dynamic-binding) (let ((variable 1) (inside-continuation)) (write-line variable) (call-with-current-continuation (lambda (outside-continuation) (fluid-let ((variable 2)) (write-line variable) (set! variable 3) (call-with-current-continuation (lambda (k) (set! inside-continuation k) (outside-continuation #t))) (write-line variable) (set! inside-continuation #f)))) (write-line variable) (if inside-continuation (begin (set! variable 4) (inside-continuation #f))))) |
Evaluating `(complicated-dynamic-binding)' writes the following on the console:
1 2 1 3 4 |
Commentary: the first two values written are the initial binding of
variable
and its new binding after the fluid-let
's body is
entered. Immediately after they are written, variable
is set to
`3', and then outside-continuation
is invoked, causing us to
exit the body. At this point, `1' is written, demonstrating that
the original value of variable
has been restored, because we have
left the body. Then we set variable
to `4' and reenter the
body by invoking inside-continuation
. At this point, `3' is
written, indicating that the side effect that previously occurred within
the body has been preserved. Finally, we exit body normally, and write
`4', demonstrating that the side effect that occurred outside of
the body was also preserved.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
lambda
,
let
, let*
, letrec
, fluid-let
, or "procedure
define
" expression). A definition that occurs at the top level
of a program is called a top-level definition, and a definition
that occurs at the beginning of a body is called an internal
definition.
In the second form of define
(called "procedure
define
"), the component formals is identical to the
component of the same name in a named-lambda
expression. In
fact, these two expressions are equivalent:
(define (name1 name2 ...) expression expression ...) (define name1 (named-lambda (name1 name2 ...) expression expression ...)) |
2.4.1 Top-Level Definitions 2.4.2 Internal Definitions
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
(define variable expression) |
has essentially the same effect as this assignment expression, if variable is bound:
(set! variable expression) |
If variable is not bound, however, define
binds
variable to a new location in the current environment before
performing the assignment (it is an error to perform a set!
on an
unbound variable). If you omit expression, the variable becomes
unassigned; an attempt to reference such a variable is an error.
(define add3 (lambda (x) (+ x 3))) => unspecified (add3 3) => 6 (define first car) => unspecified (first '(1 2)) => 1 (define bar) => unspecified bar error--> Unassigned variable |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An internal definition is a definition that occurs at the
beginning of a body (that is, the body of a lambda
,
let
, let*
, letrec
, fluid-let
, or "procedure
define
" expression), rather than at the top level of a program.
The variable defined by an internal definition is local to the
body. That is, variable is bound rather than assigned, and
the region of the binding is the entire body. For example,
(let ((x 5)) (define foo (lambda (y) (bar x y))) (define bar (lambda (a b) (+ (* a b) a))) (foo (+ x 3))) => 45 |
A body containing internal definitions can always be converted
into a completely equivalent letrec
expression. For example, the
let
expression in the above example is equivalent to
(let ((x 5)) (letrec ((foo (lambda (y) (bar x y))) (bar (lambda (a b) (+ (* a b) a)))) (foo (+ x 3)))) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
set!
expression is unspecified.
Variable must be bound either in some region enclosing the
set!
expression, or at the top level. However, variable is
permitted to be unassigned when the set!
form is entered.
(define x 2) => unspecified (+ x 1) => 3 (set! x 4) => unspecified (+ x 1) => 5 |
Variable may be an access
expression
(see section 13. Environments). This allows you to assign variables in an
arbitrary environment. For example,
(define x (let ((y 0)) (the-environment))) (define y 'a) y => a (access y x) => 0 (set! (access y x) 1) => unspecified y => a (access y x) => 1 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the expressions that are used to modify or prevent the evaluation of objects.
(quote datum)
evaluates to datum. Datum may be
any external representation of a Scheme object
(see section 1.2.6 External Representations).
Use quote
to include literal constants in
Scheme code.
(quote a) => a (quote #(a b c)) => #(a b c) (quote (+ 1 2)) => (+ 1 2) |
(quote datum)
may be abbreviated as 'datum
.
The two notations are equivalent in all respects.
'a => a '#(a b c) => #(a b c) '(+ 1 2) => (+ 1 2) '(quote a) => (quote a) ''a => (quote a) |
Numeric constants, string constants, character constants, and boolean constants evaluate to themselves, so they don't need to be quoted.
'"abc" => "abc" "abc" => "abc" '145932 => 145932 145932 => 145932 '#t => #t #t => #t '#\a => #\a #\a => #\a |
`template
is
equivalent (in the sense of equal?
) to the result of evaluating
'template
. If a comma appears within the template,
however, the expression following the comma is evaluated ("unquoted")
and its result is inserted into the structure instead of the comma and
the expression. If a comma appears followed immediately by an at-sign
(@), then the following expression shall evaluate to a list; the
opening and closing parentheses of the list are then "stripped away"
and the elements of the list are inserted in place of the comma at-sign
expression sequence.
`(list ,(+ 1 2) 4) => (list 3 4) (let ((name 'a)) `(list ,name ',name)) => (list a 'a) `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b) => (a 3 4 5 6 b) `((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) => ((foo 7) . cons) `#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8) => #(10 5 2 4 3 8) `,(+ 2 3) => 5 |
Quasiquote forms may be nested. Substitutions are made only for unquoted components appearing at the same nesting level as the outermost backquote. The nesting level increases by one inside each successive quasiquotation, and decreases by one inside each unquotation.
`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) => (a `(b ,(+ 1 2) ,(foo 4 d) e) f) (let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e)) => (a `(b ,x ,'y d) e) |
The notations `template
and (quasiquote
template
) are identical in all respects.
,expression
is identical to (unquote
expression)
and ,@expression
is identical to
(unquote-splicing expression)
.
(quasiquote (list (unquote (+ 1 2)) 4)) => (list 3 4) '(quasiquote (list (unquote (+ 1 2)) 4)) => `(list ,(+ 1 2) 4) i.e., (quasiquote (list (unquote (+ 1 2)) 4)) |
Unpredictable behavior can result if any of the symbols
quasiquote
, unquote
, or unquote-splicing
appear in
a template in ways otherwise than as described above.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The behavior of the conditional expressions is determined by
whether objects are true or false. The conditional expressions count
only #f
as false. They count everything else, including
#t
, pairs, symbols, numbers, strings, vectors, and procedures as
true (but see section 1.2.5 True and False).
In the descriptions that follow, we say that an object has "a true value" or "is true" when the conditional expressions treat it as true, and we say that an object has "a false value" or "is false" when the conditional expressions treat it as false.
if
expression is evaluated as follows: first,
predicate is evaluated. If it yields a true value, then
consequent is evaluated and its value is returned. Otherwise
alternative is evaluated and its value is returned. If
predicate yields a false value and no alternative is
specified, then the result of the expression is unspecified.
An if
expression evaluates either consequent or
alternative, never both. Programs should not depend on the value
of an if
expression that has no alternative.
(if (> 3 2) 'yes 'no) => yes (if (> 2 3) 'yes 'no) => no (if (> 3 2) (- 3 2) (+ 3 2)) => 1 |
(predicate expression ...) |
where predicate is any expression. The last clause may be
an else
clause, which has the form:
(else expression expression ...) |
A cond
expression does the following:
cond
evaluates
the expressions in the associated clause in left to right
order, and returns the result of evaluating the last expression in
the clause as the result of the entire cond
expression.
If the selected clause contains only the predicate and no
expressions, cond
returns the value of the predicate
as the result.
else
clause, the result of the conditional expression is
unspecified; if there is an else
clause, cond
evaluates
its expressions (left to right) and returns the value of the last
one.
(cond ((> 3 2) 'greater) ((< 3 2) 'less)) => greater (cond ((> 3 3) 'greater) ((< 3 3) 'less) (else 'equal)) => equal |
Normally, programs should not depend on the value of a cond
expression that has no else
clause. However, some Scheme
programmers prefer to write cond
expressions in which at least
one of the predicates is always true. In this style, the final
clause is equivalent to an else
clause.
Scheme supports an alternative clause syntax:
(predicate => recipient) |
where recipient is an expression. If predicate evaluates to a true value, then recipient is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the predicate.
(cond ((assv 'b '((a 1) (b 2))) => cadr) (else #f)) => 2 |
((object ...) expression expression ...) |
No object is evaluated, and all the objects must be
distinct. The last clause may be an else
clause,
which has the form:
(else expression expression ...) |
A case
expression does the following:
eqv?
; see section 3. Equivalence Predicates) to an object,
case
evaluates the expressions in the corresponding
clause from left to right and returns the result of evaluating the
last expression in the clause as the result of the
case
expression.
else
clause, case
evaluates its expressions and returns the result of the last one
as the result of the case
expression. If there's no else
clause, case
returns an unspecified result. Programs should not
depend on the value of a case
expression that has no else
clause.
For example,
(case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) => composite (case (car '(c d)) ((a) 'a) ((b) 'b)) => unspecified (case (car '(c d)) ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) => consonant |
#t
is returned.
(and (= 2 2) (> 2 1)) => #t (and (= 2 2) (< 2 1)) => #f (and 1 2 'c '(f g)) => (f g) (and) => #t |
#f
is returned.
(or (= 2 2) (> 2 1)) => #t (or (= 2 2) (< 2 1)) => #t (or #f #f #f) => #f (or (memq 'b '(a b c)) (/ 3 0)) => (b c) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The begin
special form is used to evaluate expressions in a
particular order.
(define x 0) (begin (set! x 5) (+ x 1)) => 6 (begin (display "4 plus 1 equals ") (display (+ 4 1))) -| 4 plus 1 equals 5 => unspecified |
Often the use of begin
is unnecessary, because many special forms
already support sequences of expressions (that is, they have an implicit
begin
). Some of these special forms are:
case
cond
define ;``procedure |
The obsolete special form sequence
is identical to begin
.
It should not be used in new code.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The iteration expressions are: "named let
" and do
.
They are also binding expressions, but are more commonly referred to as
iteration expressions. Because Scheme is properly tail-recursive, you
don't need to use these special forms to express iteration; you can
simply use appropriately written "recursive" procedure calls.
let
called
"named let
" which provides a more general looping construct
than do
, and may also be used to express recursions.
Named let
has the same syntax and semantics as ordinary
let
except that name is bound within the expressions
to a procedure whose formal arguments are the variables and whose
body is the expressions. Thus the execution of the
expressions may be repeated by invoking the procedure named by
name.
MIT Scheme allows any of the inits to be omitted, in which case the corresponding variables are unassigned.
Note: the following expressions are equivalent:
(let name ((variable init) ...) expression expression ...) ((letrec ((name (named-lambda (name variable ...) expression expression ...))) name) init ...) |
Here is an example:
(let loop ((numbers '(3 -2 1 6 -5)) (nonneg '()) (neg '())) (cond ((null? numbers) (list nonneg neg)) ((>= (car numbers) 0) (loop (cdr numbers) (cons (car numbers) nonneg) neg)) (else (loop (cdr numbers) nonneg (cons (car numbers) neg))))) => ((6 1 3) (-5 -2)) |
do
is an iteration construct. It specifies a set of variables to
be bound, how they are to be initialized at the start, and how they are
to be updated on each iteration. When a termination condition is met,
the loop exits with a specified result value.
do
expressions are evaluated as follows: The init
expressions are evaluated (in some unspecified order), the
variables are bound to fresh locations, the results of the
init expressions are stored in the bindings of the
variables, and then the iteration phase begins.
Each iteration begins by evaluating test; if the result is false, then the command expressions are evaluated in order for effect, the step expressions are evaluated in some unspecified order, the variables are bound to fresh locations, the results of the steps are stored in the bindings of the variables, and the next iteration begins.
If test evaluates to a true value, then the expressions are
evaluated from left to right and the value of the last expression
is returned as the value of the do
expression. If no
expressions are present, then the value of the do
expression is unspecified in standard Scheme; in MIT Scheme, the
value of test is returned.
The region of the binding of a variable consists of the entire
do
expression except for the inits. It is an error for a
variable to appear more than once in the list of do
variables.
A step may be omitted, in which case the effect is the same as if
(variable init variable)
had been written
instead of (variable init)
.
(do ((vec (make-vector 5)) (i 0 (+ i 1))) ((= i 5) vec) (vector-set! vec i i)) => #(0 1 2 3 4) (let ((x '(1 3 5 7 9))) (do ((x x (cdr x)) (sum 0 (+ sum (car x)))) ((null? x) sum))) => 25 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section provides examples and describes the options and syntax of
define-structure
, an MIT Scheme macro that is very similar to
defstruct
in Common Lisp. The differences between them are
summarized at the end of this section. For more information, see
Steele's Common Lisp book.
slot-name (slot-name default-init [slot-option value]*) |
The fields name and slot-name must both be symbols. The field default-init is an expression for the initial value of the slot. It is evaluated each time a new instance is constructed. If it is not specified, the initial content of the slot is undefined. Default values are only useful with a BOA constructor with argument list or a keyword constructor (see below).
Evaluation of a define-structure
expression defines a structure
descriptor and a set of procedures to manipulate instances of the
structure. These instances are represented as records by default
(see section 10.4 Records) but may alternately be lists or vectors. The
accessors and modifiers are marked with compiler declarations so that
calls to them are automatically transformed into appropriate references.
Often, no options are required, so a simple call to
define-structure
looks like:
(define-structure foo a b c) |
This defines a type descriptor foo
, a constructor
make-foo
, a predicate foo?
, accessors foo-a
,
foo-b
, and foo-c
, and modifiers set-foo-a!
,
set-foo-b!
, and set-foo-c!
.
In general, if no options are specified, define-structure
defines
the following (using the simple call above as an example):
record-type?
.
"make-"
followed by the name of
the structure, e.g. `make-foo'. The number of arguments accepted
by the constructor is the same as the number of slots; the arguments are
the initial values for the slots, and the order of the arguments matches
the order of the slot definitions.
"?"
, e.g. `foo?'. The predicate is a procedure of one
argument, which returns #t
if its argument is a record of the
type defined by this structure definition, and #f
otherwise.
"set-"
, the name of the accessor, and
"!"
, e.g. `set-foo-a!'. The modifier is a procedure of
two arguments, the first of which must be a record of the type defined
by this structure definition, and the second of which may be any object.
The modifier modifies the contents of the corresponding slot in that
record to be that object, and returns an unspecified value.
When options are not supplied, (name)
may be abbreviated to
name. This convention holds equally for structure-options
and slot-options. Hence, these are equivalent:
(define-structure foo a b c) (define-structure (foo) (a) b (c)) |
as are
(define-structure (foo keyword-constructor) a b c) (define-structure (foo (keyword-constructor)) a b c) |
When specified as option values, false
and nil
are
equivalent to #f
, and true
and t
are equivalent to
#t
.
Possible slot-options are:
#f
, this specifies that no
modifier should be created for the slot.
Possible structure-options are:
#f
, the predicate
is not defined at all. Otherwise, name must be a symbol, and
the predicate is defined with that symbol as its name.
"copy-"
followed by the structure name (e.g.
`copy-foo'). If name is #f
, the copier is not
defined. Otherwise, name must be a symbol, and the copier is
defined with that symbol as its name.
set-record-type-unparser-method!
.
If name is not given, a constructor is defined with the default
name and arguments (see above). If name is #f
, no
constructor is defined; argument-list may not be specified in this
case. Otherwise, name must be a symbol, and a constructor is
defined with that symbol as its name. If name is a symbol,
argument-list is optionally allowed; if it is omitted, the
constructor accepts one argument for each slot in the structure
definition, in the same order in which the slots appear in the
definition. Otherwise, argument-list must be a lambda list
(see section 2.1 Lambda Expressions), and each of the parameters of the lambda
list must be the name of a slot in the structure. The arguments
accepted by the constructor are defined by this lambda list. Any slot
that is not specified by the lambda list is initialized to the
default-init as specified above; likewise for any slot specified
as an optional parameter when the corresponding argument is not
supplied.
If the constructor
option is specified, the default constructor
is not defined. Additionally, the constructor
option may be
specified multiple times to define multiple constructors with
different names and argument lists.
(define-structure (foo (constructor make-foo (#!optional a b))) (a 6 read-only #t) (b 9)) |
"make-"
followed by the name of the structure (e.g.
`make-foo'). Otherwise, name must be a symbol, and a keyword
constructor is defined with this symbol as its name.
If the keyword-constructor
option is specified, the default
constructor is not defined. Additionally, the
keyword-constructor
option may be specified multiple times to
define multiple keyword constructors; this is usually not done since
such constructors would all be equivalent.
(define-structure (foo (keyword-constructor make-bar)) a b) (foo-a (make-bar 'b 20 'a 19)) => 19 |
type
or named
options.
By default, structures are implemented as records. The name of the
structure is defined to hold the type descriptor of the record defined
by the structure. The type-descriptor
option specifies a
different name to hold the type descriptor.
(define-structure foo a b) foo => #[record-type 18] (define-structure (bar (type-descriptor bar-rtd)) a b) bar error--> Unbound variable: bar bar-rtd => #[record-type 19] |
conc-name
option can be
used to specify an alternative. If name is not given, the prefix
is the name of the structure followed by a hyphen (the default). If
name is #f
, the slot names are used directly, without
prefix. Otherwise, name must a symbol, and that symbol is used as
the prefix.
|
defines accessors moby/a
and moby/b
, and modifiers
set-moby/a!
and set-moby/b!
.
|
defines accessors a
and b
, and modifiers set-a!
and
set-b!
.
type-descriptor
option.
By default, structures are implemented as records. The type
option overrides this default, allowing the programmer to specify that
the structure be implemented using another data type. The option value
representation-type specifies the alternate data type; it is
allowed to be one of the symbols vector
or list
, and the
data type used is the one corresponding to the symbol.
If this option is given, and the named
option is not specified,
the representation will not be tagged, and neither a predicate nor a
type descriptor will be defined; also, the print-procedure
option may not be given.
(define-structure (foo (type list)) a b) (make-foo 1 2) => (1 2) |
type
option and
specifies that the structure instances be tagged to make them
identifiable as instances of this structure type. This option cannot be
used with the type-descriptor
option.
In the usual case, where expression is not given, the named
option causes a type descriptor and predicate to be defined for the
structure (recall that the type
option without named
suppresses their definition), and also defines a default unparser method
for the structure instances (which can be overridden by the
print-procedure
option). If the default unparser method is not
wanted then the print-procedure
option should be specified as
#F
. This causes the structure to be printed in its native
representation, as a list or vector, which includes the type descriptor.
The type descriptor is a unique object, not a record type, that
describes the structure instances and is additionally stored in the
structure instances to identify them: if the representation type is
vector
, the type descriptor is stored in the zero-th slot of the
vector, and if the representation type is list
, it is stored as
the first element of the list.
(define-structure (foo (type vector) named) a b c) (vector-ref (make-foo 1 2 3) 0) => #[structure-type 52] |
If expression is specified, it is an expression that is evaluated to yield a tag object. The expression is evaluated once when the structure definition is evaluated (to specify the unparser method), and again whenever a predicate or constructor is called. Because of this, expression is normally a variable reference or a constant. The value yielded by expression may be any object at all. That object is stored in the structure instances in the same place that the type descriptor is normally stored, as described above. If expression is specified, no type descriptor is defined, only a predicate.
(define-structure (foo (type vector) (named 'foo)) a b c) (vector-ref (make-foo 1 2 3) 0) => foo |
define-structure
. If safe-accessors
is not specified, or
if boolean is #f
, then the accessors are optimized for
speed at the expense of safety; when compiled, the accessors will turn
into very fast inline sequences, usually one to three machine
instructions in length. However, if safe-accessors
is specified
and boolean is either omitted or #t
, then the accessors are
optimized for safety, will check the type and structure of their
argument, and will be close-coded.
(define-structure (foo safe-accessors) a b c) |
type
option.
Offset must be an exact non-negative integer and specifies the
number of slots to leave open at the beginning of the structure instance
before the specified slots are allocated. Specifying an offset of
zero is equivalent to omitting the initial-offset
option.
If the named
option is specified, the structure tag appears in
the first slot, followed by the "offset" slots, and then the regular
slots. Otherwise, the "offset" slots come first, followed by the
regular slots.
(define-structure (foo (type vector) (initial-offset 3)) a b c) (make-foo 1 2 3) => #(() () () 1 2 3) |
The essential differences between MIT Scheme's define-structure
and Common Lisp's defstruct
are:
keyword-constructor
.
&aux
in Scheme lambda lists, this
functionality is not implemented.
copier
procedure is defined.
foo
is
given the name set-foo!
.
foo
instead of :foo
.
false
, nil
, true
, and t
are treated as if the appropriate boolean constant had been specified
instead.
print-function
option is named print-procedure
. Its
argument is a procedure of two arguments (the unparser state and the
structure instance) rather than three as in Common Lisp.
named
option may optionally take an argument, which is
normally the name of a variable (any expression may be used, but it is
evaluated whenever the tag name is needed). If used, structure
instances will be tagged with that variable's value. The variable must
be defined when define-structure
is evaluated.
type
option is restricted to the values vector
and
list
.
include
option is not implemented.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |