Use proper types for hash/assoc functions in `hashtab.h'.
[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.
dab1ed37 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@page
8@node Binding Constructs
9@section Definitions and Variable Bindings
10
11@c FIXME::martin: Review me!
12
13Scheme supports the definition of variables in different contexts.
14Variables can be defined at the top level, so that they are visible in
15the entire program, and variables can be defined locally to procedures
16and expressions. This is important for modularity and data abstraction.
17
18@menu
19* Top Level:: Top level variable definitions.
20* Local Bindings:: Local variable bindings.
21* Internal Definitions:: Internal definitions.
22* Binding Reflection:: Querying variable bindings.
23@end menu
24
25
26@node Top Level
27@subsection Top Level Variable Definitions
28
29@cindex variable definition
30
31On the top level of a program (i.e. when not inside the body of a
32procedure definition or a @code{let}, @code{let*} or @code{letrec}
33expression), a definition of the form
34
35@lisp
36(define a @var{value})
37@end lisp
38
39@noindent
40defines a variable called @code{a} and sets it to the value @var{value}.
41
42If the variable already exists, because it has already been created by a
43previous @code{define} expression with the same name, its value is
44simply changed to the new @var{value}. In this case, then, the above
45form is completely equivalent to
46
47@lisp
48(set! a @var{value})
49@end lisp
50
51@noindent
52This equivalence means that @code{define} can be used interchangeably
53with @code{set!} to change the value of variables at the top level of
54the REPL or a Scheme source file. It is useful during interactive
55development when reloading a Scheme file that you have modified, because
56it allows the @code{define} expressions in that file to work as expected
57both the first time that the file is loaded and on subsequent occasions.
58
59Note, though, that @code{define} and @code{set!} are not always
60equivalent. For example, a @code{set!} is not allowed if the named
61variable does not already exist, and the two expressions can behave
62differently in the case where there are imported variables visible from
63another module.
64
65@deffn {Scheme Syntax} define name value
66Create a top level variable named @var{name} with value @var{value}.
67If the named variable already exists, just change its value. The return
68value of a @code{define} expression is unspecified.
69@end deffn
70
71The C API equivalents of @code{define} are @code{scm_define} and
72@code{scm_c_define}, which differ from each other in whether the
73variable name is specified as a @code{SCM} symbol or as a
74null-terminated C string.
75
76@deffn {C Function} scm_define (sym, value)
77@deffnx {C Function} scm_c_define (const char *name, value)
78C equivalents of @code{define}, with variable name specified either by
79@var{sym}, a symbol, or by @var{name}, a null-terminated C string. Both
80variants return the new or preexisting variable object.
81@end deffn
82
83@code{define} (when it occurs at top level), @code{scm_define} and
84@code{scm_c_define} all create or set the value of a variable in the top
85level environment of the current module. If there was not already a
86variable with the specified name belonging to the current module, but a
87similarly named variable from another module was visible through having
88been imported, the newly created variable in the current module will
89shadow the imported variable, such that the imported variable is no
90longer visible.
91
92Attention: Scheme definitions inside local binding constructs
93(@pxref{Local Bindings}) act differently (@pxref{Internal Definitions}).
94
95
96@node Local Bindings
97@subsection Local Variable Bindings
98
99@c FIXME::martin: Review me!
100
101@cindex local bindings
102@cindex local variables
103
104As opposed to definitions at the top level, which are visible in the
105whole program (or current module, when Guile modules are used), it is
106also possible to define variables which are only visible in a
107well-defined part of the program. Normally, this part of a program
108will be a procedure or a subexpression of a procedure.
109
110With the constructs for local binding (@code{let}, @code{let*} and
111@code{letrec}), the Scheme language has a block structure like most
112other programming languages since the days of @sc{Algol 60}. Readers
113familiar to languages like C or Java should already be used to this
114concept, but the family of @code{let} expressions has a few properties
115which are well worth knowing.
116
117The first local binding construct is @code{let}. The other constructs
118@code{let*} and @code{letrec} are specialized versions for usage where
119using plain @code{let} is a bit inconvenient.
120
121@deffn syntax let bindings body
122@var{bindings} has the form
123
124@lisp
125((@var{variable1} @var{init1}) @dots{})
126@end lisp
127
128that is zero or more two-element lists of a variable and an arbitrary
129expression each. All @var{variable} names must be distinct.
130
131A @code{let} expression is evaluated as follows.
132
133@itemize @bullet
134@item
135All @var{init} expressions are evaluated.
136
137@item
138New storage is allocated for the @var{variables}.
139
140@item
141The values of the @var{init} expressions are stored into the variables.
142
143@item
144The expressions in @var{body} are evaluated in order, and the value of
145the last expression is returned as the value of the @code{let}
146expression.
147
148@item
149The storage for the @var{variables} is freed.
150@end itemize
151
152The @var{init} expressions are not allowed to refer to any of the
153@var{variables}.
154@end deffn
155
156@deffn syntax let* bindings body
157Similar to @code{let}, but the variable bindings are performed
158sequentially, that means that all @var{init} expression are allowed to
159use the variables defined on their left in the binding list.
160
161A @code{let*} expression can always be expressed with nested @code{let}
162expressions.
163
164@lisp
165(let* ((a 1) (b a))
166 b)
167@equiv{}
168(let ((a 1))
169 (let ((b a))
170 b))
171@end lisp
172@end deffn
173
174@deffn syntax letrec bindings body
175Similar to @code{let}, but it is possible to refer to the @var{variable}
176from lambda expression created in any of the @var{inits}. That is,
177procedures created in the @var{init} expression can recursively refer to
178the defined variables.
179
180@lisp
181(letrec ((even?
182 (lambda (n)
183 (if (zero? n)
184 #t
185 (odd? (- n 1)))))
186 (odd?
187 (lambda (n)
188 (if (zero? n)
189 #f
190 (even? (- n 1))))))
191 (even? 88))
192@result{}
193#t
194@end lisp
195@end deffn
196
197There is also an alternative form of the @code{let} form, which is used
198for expressing iteration. Because of the use as a looping construct,
199this form (the @dfn{named let}) is documented in the section about
200iteration (@pxref{while do, Iteration})
201
202@node Internal Definitions
203@subsection Internal definitions
204
205@c FIXME::martin: Review me!
206
207A @code{define} form which appears inside the body of a @code{lambda},
208@code{let}, @code{let*}, @code{letrec} or equivalent expression is
209called an @dfn{internal definition}. An internal definition differs
210from a top level definition (@pxref{Top Level}), because the definition
211is only visible inside the complete body of the enclosing form. Let us
212examine the following example.
213
214@lisp
215(let ((frumble "froz"))
216 (define banana (lambda () (apple 'peach)))
217 (define apple (lambda (x) x))
218 (banana))
219@result{}
220peach
221@end lisp
222
223Here the enclosing form is a @code{let}, so the @code{define}s in the
224@code{let}-body are internal definitions. Because the scope of the
225internal definitions is the @strong{complete} body of the
226@code{let}-expression, the @code{lambda}-expression which gets bound
227to the variable @code{banana} may refer to the variable @code{apple},
228even though it's definition appears lexically @emph{after} the definition
229of @code{banana}. This is because a sequence of internal definition
230acts as if it were a @code{letrec} expression.
231
232@lisp
233(let ()
234 (define a 1)
235 (define b 2)
236 (+ a b))
237@end lisp
238
239@noindent
240is equivalent to
241
242@lisp
243(let ()
244 (letrec ((a 1) (b 2))
245 (+ a b)))
246@end lisp
247
248Another noteworthy difference to top level definitions is that within
249one group of internal definitions all variable names must be distinct.
250That means where on the top level a second define for a given variable
251acts like a @code{set!}, an exception is thrown for internal definitions
252with duplicate bindings.
253
254@c FIXME::martin: The following is required by R5RS, but Guile does not
255@c signal an error. Document it anyway, saying that Guile is sloppy?
256
257@c Internal definitions are only allowed at the beginning of the body of an
258@c enclosing expression. They may not be mixed with other expressions.
259
260@c @lisp
261@c (let ()
262@c (define a 1)
263@c a
264@c (define b 2)
265@c b)
266@c @end lisp
267
268@node Binding Reflection
269@subsection Querying variable bindings
270
271Guile provides a procedure for checking whether a symbol is bound in the
272top level environment.
273
dab1ed37
LC
274@deffn {Scheme Procedure} defined? sym [module]
275@deffnx {C Function} scm_defined_p (sym, module)
276Return @code{#t} if @var{sym} is defined in the module @var{module} or
277the current module when @var{module} is not specified; otherwise return
278@code{#f}.
279
280Up to Guile 1.8, the second optional argument had to be @dfn{lexical
281environment} as returned by @code{the-environment}, for example. The
282behavior of this function remains unchanged when the second argument is
283omitted.
07d83abe
MV
284@end deffn
285
286
287@c Local Variables:
288@c TeX-master: "guile.texi"
289@c End: