api-binding.texi 12 KB

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