2 @node Binding Constructs
3 @chapter Definitions and Variable Bindings
5 @c FIXME::martin: Review me!
7 Scheme supports the definition of variables in different contexts.
8 Variables can be defined at the top level, so that they are visible in
9 the entire program, and variables can be defined locally to procedures
10 and expressions. This is important for modularity and data abstraction.
13 * Top Level:: Top level variable definitions.
14 * Local Bindings:: Local variable bindings.
15 * Internal Definitions:: Internal definitions.
16 * Binding Reflection:: Querying variable bindings.
21 @section Top Level Variable Definitions
23 @cindex variable definition
25 On the top level of a program (i.e. when not inside the body of a
26 procedure definition or a @code{let}, @code{let*} or @code{letrec}
27 expression), a definition of the form
30 (define a @var{value})
34 defines a variable called @code{a} and sets it to the value @var{value}.
36 If the variable already exists, because it has already been created by a
37 previous @code{define} expression with the same name, its value is
38 simply changed to the new @var{value}. In this case, then, the above
39 form is completely equivalent to
46 This equivalence means that @code{define} can be used interchangeably
47 with @code{set!} to change the value of variables at the top level of
48 the REPL or a Scheme source file. It is useful during interactive
49 development when reloading a Scheme file that you have modified, because
50 it allows the @code{define} expressions in that file to work as expected
51 both the first time that the file is loaded and on subsequent occasions.
53 Note, though, that @code{define} and @code{set!} are not always
54 equivalent. For example, a @code{set!} is not allowed if the named
55 variable does not already exist, and the two expressions can behave
56 differently in the case where there are imported variables visible from
59 @deffn {Scheme Syntax} define name value
60 Create a top level variable named @var{name} with value @var{value}.
61 If the named variable already exists, just change its value. The return
62 value of a @code{define} expression is unspecified.
65 The C API equivalents of @code{define} are @code{scm_define} and
66 @code{scm_c_define}, which differ from each other in whether the
67 variable name is specified as a @code{SCM} symbol or as a
68 null-terminated C string.
70 @deffn {C Function} scm_define (sym, value)
71 @deffnx {C Function} scm_c_define (const char *name, value)
72 C equivalents of @code{define}, with variable name specified either by
73 @var{sym}, a symbol, or by @var{name}, a null-terminated C string. Both
74 variants return the new or preexisting variable object.
77 @code{define} (when it occurs at top level), @code{scm_define} and
78 @code{scm_c_define} all create or set the value of a variable in the top
79 level environment of the current module. If there was not already a
80 variable with the specified name belonging to the current module, but a
81 similarly named variable from another module was visible through having
82 been imported, the newly created variable in the current module will
83 shadow the imported variable, such that the imported variable is no
86 Attention: Scheme definitions inside local binding constructs
87 (@pxref{Local Bindings}) act differently (@pxref{Internal Definitions}).
91 @section Local Variable Bindings
93 @c FIXME::martin: Review me!
95 @cindex local bindings
96 @cindex local variables
98 As opposed to definitions at the top level, which are visible in the
99 whole program (or current module, when Guile modules are used), it is
100 also possible to define variables which are only visible in a
101 well-defined part of the program. Normally, this part of a program
102 will be a procedure or a subexpression of a procedure.
104 With the constructs for local binding (@code{let}, @code{let*} and
105 @code{letrec}), the Scheme language has a block structure like most
106 other programming languages since the days of @sc{Algol 60}. Readers
107 familiar to languages like C or Java should already be used to this
108 concept, but the family of @code{let} expressions has a few properties
109 which are well worth knowing.
111 The first local binding construct is @code{let}. The other constructs
112 @code{let*} and @code{letrec} are specialized versions for usage where
113 using plain @code{let} is a bit inconvenient.
115 @deffn syntax let bindings body
116 @var{bindings} has the form
119 ((@var{variable1} @var{init1}) @dots{})
122 that is zero or more two-element lists of a variable and an arbitrary
123 expression each. All @var{variable} names must be distinct.
125 A @code{let} expression is evaluated as follows.
129 All @var{init} expressions are evaluated.
132 New storage is allocated for the @var{variables}.
135 The values of the @var{init} expressions are stored into the variables.
138 The expressions in @var{body} are evaluated in order, and the value of
139 the last expression is returned as the value of the @code{let}
143 The storage for the @var{variables} is freed.
146 The @var{init} expressions are not allowed to refer to any of the
150 @deffn syntax let* bindings body
151 Similar to @code{let}, but the variable bindings are performed
152 sequentially, that means that all @var{init} expression are allowed to
153 use the variables defined on their left in the binding list.
155 A @code{let*} expression can always be expressed with nested @code{let}
168 @deffn syntax letrec bindings body
169 Similar to @code{let}, but it is possible to refer to the @var{variable}
170 from lambda expression created in any of the @var{inits}. That is,
171 procedures created in the @var{init} expression can recursively refer to
172 the defined variables.
191 There is also an alternative form of the @code{let} form, which is used
192 for expressing iteration. Because of the use as a looping construct,
193 this form (the @dfn{named let}) is documented in the section about
194 iteration (@pxref{while do, Iteration})
196 @node Internal Definitions
197 @section Internal definitions
199 @c FIXME::martin: Review me!
201 A @code{define} form which appears inside the body of a @code{lambda},
202 @code{let}, @code{let*}, @code{letrec} or equivalent expression is
203 called an @dfn{internal definition}. An internal definition differs
204 from a top level definition (@pxref{Top Level}), because the definition
205 is only visible inside the complete body of the enclosing form. Let us
206 examine the following example.
209 (let ((frumble "froz"))
210 (define banana (lambda () (apple 'peach)))
211 (define apple (lambda (x) x))
217 Here the enclosing form is a @code{let}, so the @code{define}s in the
218 @code{let}-body are internal definitions. Because the scope of the
219 internal definitions is the @strong{complete} body of the
220 @code{let}-expression, the @code{lambda}-expression which gets bound
221 to the variable @code{banana} may refer to the variable @code{apple},
222 even though it's definition appears lexically @emph{after} the definition
223 of @code{banana}. This is because a sequence of internal definition
224 acts as if it were a @code{letrec} expression.
238 (letrec ((a 1) (b 2))
242 Another noteworthy difference to top level definitions is that within
243 one group of internal definitions all variable names must be distinct.
244 That means where on the top level a second define for a given variable
245 acts like a @code{set!}, an exception is thrown for internal definitions
246 with duplicate bindings.
248 @c FIXME::martin: The following is required by R5RS, but Guile does not
249 @c signal an error. Document it anyway, saying that Guile is sloppy?
251 @c Internal definitions are only allowed at the beginning of the body of an
252 @c enclosing expression. They may not be mixed with other expressions.
262 @node Binding Reflection
263 @section Querying variable bindings
265 Guile provides a procedure for checking whether a symbol is bound in the
266 top level environment.
268 @c NJFIXME explain [env]
269 @deffn {Scheme Procedure} defined? sym [env]
270 @deffnx {C Function} scm_defined_p (sym, env)
271 Return @code{#t} if @var{sym} is defined in the lexical environment @var{env}. When @var{env} is not specified, look in the top-level environment as defined by the current module.
276 @c TeX-master: "guile.texi"