Definitions: define in Guide: Racket introduces definitions.
(define id expr) |
(define (head args) body ...+) |
|
head | | = | | id | | | | | | (head args) | | | | | | args | | = | | arg ... | | | | | | arg ... . rest-id | | | | | | arg | | = | | arg-id | | | | | | [arg-id default-expr] | | | | | | keyword arg-id | | | | | | keyword [arg-id default-expr] |
|
The first form
binds
id to the result of
expr, and the second form
binds
id to a
procedure. In the second case, the generated procedure is
(CVT (head args) body ...+), using the
CVT meta-function
defined as follows:
(CVT (id . kw-formals) . datum) = (lambda kw-formals . datum) |
(CVT (head . kw-formals) . datum) = (lambda kw-formals expr) |
if (CVT head . datum) = expr |
At the top level, the top-level binding id is created after
evaluating expr, if it does not exist already, and the
top-level mapping of id (in the namespace linked
with the compiled definition) is set to the binding at the same time.
|
|
> ((f 10) 30) | 40 | > ((f 10)) | 30 |
|
Evaluates the
expr, and
binds the results to the
ids, in order, if the number of results matches the number of
ids; if
expr produces a different number of results,
the
exn:fail:contract exception is raised.
At the top level, the top-level binding for each id is
created after evaluating expr, if it does not exist already,
and the top-level mapping of each id (in the
namespace linked with the compiled definition) is set to
the binding at the same time.
The second form is a shorthand the same as for define; it
expands to a definition of the first form where the expr is a
lambda form.
Examples: |
|
> (foo 1 2 3 4) |
(1 2 3 4) |
|
> (bar 1 2 3 4) |
(1 2 3 4) |
Like
define-syntax, but creates a
transformer binding
for each
id. The
expr should produce as many values
as
ids, and each value is bound to the corresponding
id.
When expr produces zero values for a top-level
define-syntaxes (i.e., not in a module or internal-definition
position), then the ids are effectively declared without
binding; see Macro-Introduced Bindings.
Examples: |
|
> (foo1) |
1 |
> (foo2) |
2 |
> (foo3) |
3 |
Examples: |
> (define-for-syntax helper 2) |
|
> (make-two) |
helper is 2 |
2 |
; `helper' is not bound in the runtime phase |
> helper |
reference to undefined identifier: helper |
|
|
> (let ([a 1] [b 2] [c 3]) | (show-variables a 5 2 b c)) |
|
'(1 2 3) |
The first form is like
define-syntax, but for a
require sub-form. The
proc-expr must produce a
procedure that accepts and returns a syntax object representing a
require sub-form.
This form expands to define-syntax with a use of
make-require-transformer; see require Transformers for
more information.
The second form is a shorthand the same as for define-syntax; it
expands to a definition of the first form where the expr is a
lambda form.
The first form is like
define-syntax, but for a
provide sub-form. The
proc-expr must produce a
procedure that accepts and returns a syntax object representing a
provide sub-form.
This form expands to define-syntax with a use of
make-provide-transformer; see provide Transformers for
more information.
The second form is a shorthand the same as for define-syntax; it
expands to a definition of the first form where the expr is a
lambda form.