[ < ] [ > ]   [ << ] [ 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 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))
  (begin
    (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 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 …))

Note: to find out if a symbol has definition (global binding) in the current module, you can use global-variable-bound? (See section Module introspection).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on October, 7 2008 using texi2html 1.78.