[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.10 Definitions

Special Form: define variable expression
Special Form: define (variable . formals) body ...
[R5RS] This form has different meanings in the toplevel (without no local bindings) or inside a local scope.

On toplevel, it defines a global binding to a symbol variable. In the first form, it globally binds a symbol variable to the value of expression, in the current module.
(define x (+ 1 2))
x => 3
(define y (lambda (a) (* a 2)))
(y 8) => 16

The second form is a syntactic sugar of defining a procedure. It is equivalent to the following form.
(define (name . args) body ...)
  == (define name (lambda args body ...))

If the form appears inside a local scope (internal define), this introduce a local binding of the variable.

Note that begin (See section 4.7 Sequencing) doesn't introduce a new scope. Defines in the begin act as if begin and surrounding parenthesis are not there. Thus these two forms are equivalent.
(let ((x 0))
    (define (foo y) (+ x y)))
  (foo 3))
(let ((x 0))
  (define (foo y) (+ x y))
  (foo 3))

Macro: define-values (var ...) expr
Expr is evaluated, and it should return as many values as vars. Then each value of the results are bound to each variable in var .... See section 10.6 srfi-11 - Let-values.
(define-values (lo hi) (min&max 3 -1 15 2))

lo => -1
hi => 15

Special Form: define-constant variable expression
Special Form: define-constant (variable . formals) body ...
Like define, but that the compiler assumes the value of variable won't change and generates optimized code.

An error is signalled when you use set! to change the value of variable. It is allowed to redefine variable, but a warning is printed.

Special Form: define-in-module module variable expression
Special Form: define-in-module module (variable . formals) body ...
This form creates a global binding of variable in module, which must be either a symbol of the module name or a module object. If module is a symbol, the named module must exist.

Expression is evaluated in the current module.

The second form is merely a syntactic sugar of:
(define-in-module module variable (lambda formals body ...))

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html