Update vm.texi's "Instruction Set" section.
[bpt/guile.git] / doc / ref / api-binding.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
c5f30c4c 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010, 2011
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
07d83abe
MV
7@node Binding Constructs
8@section Definitions and Variable Bindings
9
07d83abe
MV
10Scheme supports the definition of variables in different contexts.
11Variables can be defined at the top level, so that they are visible in
12the entire program, and variables can be defined locally to procedures
13and expressions. This is important for modularity and data abstraction.
14
15@menu
16* Top Level:: Top level variable definitions.
17* Local Bindings:: Local variable bindings.
18* Internal Definitions:: Internal definitions.
19* Binding Reflection:: Querying variable bindings.
20@end menu
21
22
23@node Top Level
24@subsection Top Level Variable Definitions
25
26@cindex variable definition
27
935c7aca 28At the top level of a program (i.e., not nested within any other
07d83abe
MV
29expression), a definition of the form
30
31@lisp
32(define a @var{value})
33@end lisp
34
35@noindent
36defines a variable called @code{a} and sets it to the value @var{value}.
37
935c7aca
AW
38If the variable already exists in the current module, because it has
39already been created by a previous @code{define} expression with the
40same name, its value is simply changed to the new @var{value}. In this
41case, then, the above form is completely equivalent to
07d83abe
MV
42
43@lisp
44(set! a @var{value})
45@end lisp
46
47@noindent
48This equivalence means that @code{define} can be used interchangeably
49with @code{set!} to change the value of variables at the top level of
50the REPL or a Scheme source file. It is useful during interactive
51development when reloading a Scheme file that you have modified, because
52it allows the @code{define} expressions in that file to work as expected
53both the first time that the file is loaded and on subsequent occasions.
54
55Note, though, that @code{define} and @code{set!} are not always
56equivalent. For example, a @code{set!} is not allowed if the named
57variable does not already exist, and the two expressions can behave
58differently in the case where there are imported variables visible from
59another module.
60
61@deffn {Scheme Syntax} define name value
62Create a top level variable named @var{name} with value @var{value}.
63If the named variable already exists, just change its value. The return
64value of a @code{define} expression is unspecified.
65@end deffn
66
67The C API equivalents of @code{define} are @code{scm_define} and
68@code{scm_c_define}, which differ from each other in whether the
69variable name is specified as a @code{SCM} symbol or as a
70null-terminated C string.
71
72@deffn {C Function} scm_define (sym, value)
73@deffnx {C Function} scm_c_define (const char *name, value)
74C equivalents of @code{define}, with variable name specified either by
75@var{sym}, a symbol, or by @var{name}, a null-terminated C string. Both
76variants return the new or preexisting variable object.
77@end deffn
78
79@code{define} (when it occurs at top level), @code{scm_define} and
80@code{scm_c_define} all create or set the value of a variable in the top
81level environment of the current module. If there was not already a
82variable with the specified name belonging to the current module, but a
83similarly named variable from another module was visible through having
84been imported, the newly created variable in the current module will
85shadow the imported variable, such that the imported variable is no
86longer visible.
87
88Attention: Scheme definitions inside local binding constructs
89(@pxref{Local Bindings}) act differently (@pxref{Internal Definitions}).
90
c5f30c4c
AW
91Many people end up in a development style of adding and changing
92definitions at runtime, building out their program without restarting
93it. (You can do this using @code{reload-module}, the @code{reload} REPL
94command, the @code{load} procedure, or even just pasting code into a
95REPL.) If you are one of these people, you will find that sometimes you
96there are some variables that you @emph{don't} want to redefine all the
97time. For these, use @code{define-once}.
98
99@fnindex defvar
100@deffn {Scheme Syntax} define-once name value
101Create a top level variable named @var{name} with value @var{value}, but
102only if @var{name} is not already bound in the current module.
103@end deffn
104
105Old Lispers probably know @code{define-once} under its Lisp name,
106@code{defvar}.
107
07d83abe
MV
108
109@node Local Bindings
110@subsection Local Variable Bindings
111
07d83abe
MV
112@cindex local bindings
113@cindex local variables
114
935c7aca
AW
115As opposed to definitions at the top level, which creates bindings that
116are visible to all code in a module, it is also possible to define
117variables which are only visible in a well-defined part of the program.
118Normally, this part of a program will be a procedure or a subexpression
119of a procedure.
07d83abe 120
935c7aca
AW
121With the constructs for local binding (@code{let}, @code{let*},
122@code{letrec}, and @code{letrec*}), the Scheme language has a block
123structure like most other programming languages since the days of
124@sc{Algol 60}. Readers familiar to languages like C or Java should
125already be used to this concept, but the family of @code{let}
126expressions has a few properties which are well worth knowing.
07d83abe 127
935c7aca 128The most basic local binding construct is @code{let}.
07d83abe
MV
129
130@deffn syntax let bindings body
131@var{bindings} has the form
132
133@lisp
134((@var{variable1} @var{init1}) @dots{})
135@end lisp
136
137that is zero or more two-element lists of a variable and an arbitrary
138expression each. All @var{variable} names must be distinct.
139
140A @code{let} expression is evaluated as follows.
141
142@itemize @bullet
143@item
144All @var{init} expressions are evaluated.
145
146@item
147New storage is allocated for the @var{variables}.
148
149@item
150The values of the @var{init} expressions are stored into the variables.
151
152@item
153The expressions in @var{body} are evaluated in order, and the value of
154the last expression is returned as the value of the @code{let}
155expression.
07d83abe
MV
156@end itemize
157
158The @var{init} expressions are not allowed to refer to any of the
159@var{variables}.
160@end deffn
161
ecb87335 162The other binding constructs are variations on the same theme: making new
935c7aca
AW
163values, binding them to variables, and executing a body in that new,
164extended lexical context.
165
07d83abe
MV
166@deffn syntax let* bindings body
167Similar to @code{let}, but the variable bindings are performed
168sequentially, that means that all @var{init} expression are allowed to
169use the variables defined on their left in the binding list.
170
171A @code{let*} expression can always be expressed with nested @code{let}
172expressions.
173
174@lisp
175(let* ((a 1) (b a))
176 b)
177@equiv{}
178(let ((a 1))
179 (let ((b a))
180 b))
181@end lisp
182@end deffn
183
184@deffn syntax letrec bindings body
185Similar to @code{let}, but it is possible to refer to the @var{variable}
186from lambda expression created in any of the @var{inits}. That is,
187procedures created in the @var{init} expression can recursively refer to
188the defined variables.
189
190@lisp
935c7aca
AW
191(letrec ((even? (lambda (n)
192 (if (zero? n)
193 #t
194 (odd? (- n 1)))))
195 (odd? (lambda (n)
196 (if (zero? n)
197 #f
198 (even? (- n 1))))))
07d83abe
MV
199 (even? 88))
200@result{}
201#t
202@end lisp
935c7aca
AW
203
204Note that while the @var{init} expressions may refer to the new
205variables, they may not access their values. For example, making the
206@code{even?} function above creates a closure (@pxref{About Closure})
207referencing the @code{odd?} variable. But @code{odd?} can't be called
208until after execution has entered the body.
209@end deffn
210
211@deffn syntax letrec* bindings body
212Similar to @code{letrec}, except the @var{init} expressions are bound to
213their variables in order.
214
215@code{letrec*} thus relaxes the letrec restriction, in that later
216@var{init} expressions may refer to the values of previously bound
217variables.
218
219@lisp
220(letrec ((a 42)
51607806 221 (b (+ a 10))) ;; Illegal access
935c7aca 222 (* a b))
51607806 223;; The behavior of the expression above is unspecified
935c7aca
AW
224
225(letrec* ((a 42)
226 (b (+ a 10)))
227 (* a b))
228@result{} 2184
229@end lisp
07d83abe
MV
230@end deffn
231
232There is also an alternative form of the @code{let} form, which is used
233for expressing iteration. Because of the use as a looping construct,
234this form (the @dfn{named let}) is documented in the section about
235iteration (@pxref{while do, Iteration})
236
237@node Internal Definitions
238@subsection Internal definitions
239
240@c FIXME::martin: Review me!
241
242A @code{define} form which appears inside the body of a @code{lambda},
935c7aca
AW
243@code{let}, @code{let*}, @code{letrec}, @code{letrec*} or equivalent
244expression is called an @dfn{internal definition}. An internal
245definition differs from a top level definition (@pxref{Top Level}),
246because the definition is only visible inside the complete body of the
247enclosing form. Let us examine the following example.
07d83abe
MV
248
249@lisp
250(let ((frumble "froz"))
251 (define banana (lambda () (apple 'peach)))
252 (define apple (lambda (x) x))
253 (banana))
254@result{}
255peach
256@end lisp
257
258Here the enclosing form is a @code{let}, so the @code{define}s in the
259@code{let}-body are internal definitions. Because the scope of the
260internal definitions is the @strong{complete} body of the
935c7aca
AW
261@code{let}-expression, the @code{lambda}-expression which gets bound to
262the variable @code{banana} may refer to the variable @code{apple}, even
263though its definition appears lexically @emph{after} the definition of
264@code{banana}. This is because a sequence of internal definition acts
265as if it were a @code{letrec*} expression.
07d83abe
MV
266
267@lisp
268(let ()
269 (define a 1)
270 (define b 2)
271 (+ a b))
272@end lisp
273
274@noindent
275is equivalent to
276
277@lisp
278(let ()
935c7aca 279 (letrec* ((a 1) (b 2))
07d83abe
MV
280 (+ a b)))
281@end lisp
282
935c7aca
AW
283Internal definitions are only allowed at the beginning of the body of an
284enclosing expression. They may not be mixed with other expressions.
285
07d83abe
MV
286Another noteworthy difference to top level definitions is that within
287one group of internal definitions all variable names must be distinct.
288That means where on the top level a second define for a given variable
289acts like a @code{set!}, an exception is thrown for internal definitions
290with duplicate bindings.
291
935c7aca
AW
292As a historical note, it used to be that internal bindings were expanded
293in terms of @code{letrec}, not @code{letrec*}. This was the situation
294for the R5RS report and before. However with the R6RS, it was recognized
295that sequential definition was a more intuitive expansion, as in the
296following case:
07d83abe 297
935c7aca
AW
298@lisp
299(let ()
300 (define a 1)
301 (define b (+ a a))
302 (+ a b))
303@end lisp
304
305@noindent
306Guile decided to follow the R6RS in this regard, and now expands
307internal definitions using @code{letrec*}.
07d83abe 308
07d83abe
MV
309
310@node Binding Reflection
311@subsection Querying variable bindings
312
313Guile provides a procedure for checking whether a symbol is bound in the
314top level environment.
315
dab1ed37
LC
316@deffn {Scheme Procedure} defined? sym [module]
317@deffnx {C Function} scm_defined_p (sym, module)
318Return @code{#t} if @var{sym} is defined in the module @var{module} or
319the current module when @var{module} is not specified; otherwise return
320@code{#f}.
07d83abe
MV
321@end deffn
322
323
324@c Local Variables:
325@c TeX-master: "guile.texi"
326@c End: