api-binding.texi 9.1 KB

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