api-evaluation.texi 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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, 2005, 2006, 2009, 2010, 2011, 2012
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Read/Load/Eval/Compile
  7. @section Reading and Evaluating Scheme Code
  8. This chapter describes Guile functions that are concerned with reading,
  9. loading, evaluating, and compiling Scheme code at run time.
  10. @menu
  11. * Scheme Syntax:: Standard and extended Scheme syntax.
  12. * Scheme Read:: Reading Scheme code.
  13. * Scheme Write:: Writing Scheme values to a port.
  14. * Fly Evaluation:: Procedures for on the fly evaluation.
  15. * Compilation:: How to compile Scheme files and procedures.
  16. * Loading:: Loading Scheme code from file.
  17. * Load Paths:: Where Guile looks for code.
  18. * Character Encoding of Source Files:: Loading non-ASCII Scheme code from file.
  19. * Delayed Evaluation:: Postponing evaluation until it is needed.
  20. * Local Evaluation:: Evaluation in a local lexical environment.
  21. * Local Inclusion:: Compile-time inclusion of one file in another.
  22. @end menu
  23. @node Scheme Syntax
  24. @subsection Scheme Syntax: Standard and Guile Extensions
  25. @menu
  26. * Expression Syntax::
  27. * Comments::
  28. * Block Comments::
  29. * Case Sensitivity::
  30. * Keyword Syntax::
  31. * Reader Extensions::
  32. @end menu
  33. @node Expression Syntax
  34. @subsubsection Expression Syntax
  35. An expression to be evaluated takes one of the following forms.
  36. @table @nicode
  37. @item @var{symbol}
  38. A symbol is evaluated by dereferencing. A binding of that symbol is
  39. sought and the value there used. For example,
  40. @example
  41. (define x 123)
  42. x @result{} 123
  43. @end example
  44. @item (@var{proc} @var{args}@dots{})
  45. A parenthesised expression is a function call. @var{proc} and each
  46. argument are evaluated, then the function (which @var{proc} evaluated
  47. to) is called with those arguments.
  48. The order in which @var{proc} and the arguments are evaluated is
  49. unspecified, so be careful when using expressions with side effects.
  50. @example
  51. (max 1 2 3) @result{} 3
  52. (define (get-some-proc) min)
  53. ((get-some-proc) 1 2 3) @result{} 1
  54. @end example
  55. The same sort of parenthesised form is used for a macro invocation,
  56. but in that case the arguments are not evaluated. See the
  57. descriptions of macros for more on this (@pxref{Macros}, and
  58. @pxref{Syntax Rules}).
  59. @item @var{constant}
  60. Number, string, character and boolean constants evaluate ``to
  61. themselves'', so can appear as literals.
  62. @example
  63. 123 @result{} 123
  64. 99.9 @result{} 99.9
  65. "hello" @result{} "hello"
  66. #\z @result{} #\z
  67. #t @result{} #t
  68. @end example
  69. Note that an application must not attempt to modify literal strings,
  70. since they may be in read-only memory.
  71. @item (quote @var{data})
  72. @itemx '@var{data}
  73. @findex quote
  74. @findex '
  75. Quoting is used to obtain a literal symbol (instead of a variable
  76. reference), a literal list (instead of a function call), or a literal
  77. vector. @nicode{'} is simply a shorthand for a @code{quote} form.
  78. For example,
  79. @example
  80. 'x @result{} x
  81. '(1 2 3) @result{} (1 2 3)
  82. '#(1 (2 3) 4) @result{} #(1 (2 3) 4)
  83. (quote x) @result{} x
  84. (quote (1 2 3)) @result{} (1 2 3)
  85. (quote #(1 (2 3) 4)) @result{} #(1 (2 3) 4)
  86. @end example
  87. Note that an application must not attempt to modify literal lists or
  88. vectors obtained from a @code{quote} form, since they may be in
  89. read-only memory.
  90. @item (quasiquote @var{data})
  91. @itemx `@var{data}
  92. @findex quasiquote
  93. @findex `
  94. Backquote quasi-quotation is like @code{quote}, but selected
  95. sub-expressions are evaluated. This is a convenient way to construct
  96. a list or vector structure most of which is constant, but at certain
  97. points should have expressions substituted.
  98. The same effect can always be had with suitable @code{list},
  99. @code{cons} or @code{vector} calls, but quasi-quoting is often easier.
  100. @table @nicode
  101. @item (unquote @var{expr})
  102. @itemx ,@var{expr}
  103. @findex unquote
  104. @findex ,
  105. Within the quasiquote @var{data}, @code{unquote} or @code{,} indicates
  106. an expression to be evaluated and inserted. The comma syntax @code{,}
  107. is simply a shorthand for an @code{unquote} form. For example,
  108. @example
  109. `(1 2 ,(* 9 9) 3 4) @result{} (1 2 81 3 4)
  110. `(1 (unquote (+ 1 1)) 3) @result{} (1 2 3)
  111. `#(1 ,(/ 12 2)) @result{} #(1 6)
  112. @end example
  113. @item (unquote-splicing @var{expr})
  114. @itemx ,@@@var{expr}
  115. @findex unquote-splicing
  116. @findex ,@@
  117. Within the quasiquote @var{data}, @code{unquote-splicing} or
  118. @code{,@@} indicates an expression to be evaluated and the elements of
  119. the returned list inserted. @var{expr} must evaluate to a list. The
  120. ``comma-at'' syntax @code{,@@} is simply a shorthand for an
  121. @code{unquote-splicing} form.
  122. @example
  123. (define x '(2 3))
  124. `(1 ,@@x 4) @result{} (1 2 3 4)
  125. `(1 (unquote-splicing (map 1+ x))) @result{} (1 3 4)
  126. `#(9 ,@@x 9) @result{} #(9 2 3 9)
  127. @end example
  128. Notice @code{,@@} differs from plain @code{,} in the way one level of
  129. nesting is stripped. For @code{,@@} the elements of a returned list
  130. are inserted, whereas with @code{,} it would be the list itself
  131. inserted.
  132. @end table
  133. @c
  134. @c FIXME: What can we say about the mutability of a quasiquote
  135. @c result? R5RS doesn't seem to specify anything, though where it
  136. @c says backquote without commas is the same as plain quote then
  137. @c presumably the "fixed" portions of a quasiquote expression must be
  138. @c treated as immutable.
  139. @c
  140. @end table
  141. @node Comments
  142. @subsubsection Comments
  143. @c FIXME::martin: Review me!
  144. Comments in Scheme source files are written by starting them with a
  145. semicolon character (@code{;}). The comment then reaches up to the end
  146. of the line. Comments can begin at any column, and the may be inserted
  147. on the same line as Scheme code.
  148. @lisp
  149. ; Comment
  150. ;; Comment too
  151. (define x 1) ; Comment after expression
  152. (let ((y 1))
  153. ;; Display something.
  154. (display y)
  155. ;;; Comment at left margin.
  156. (display (+ y 1)))
  157. @end lisp
  158. It is common to use a single semicolon for comments following
  159. expressions on a line, to use two semicolons for comments which are
  160. indented like code, and three semicolons for comments which start at
  161. column 0, even if they are inside an indented code block. This
  162. convention is used when indenting code in Emacs' Scheme mode.
  163. @node Block Comments
  164. @subsubsection Block Comments
  165. @cindex multiline comments
  166. @cindex block comments
  167. @cindex #!
  168. @cindex !#
  169. @c FIXME::martin: Review me!
  170. In addition to the standard line comments defined by R5RS, Guile has
  171. another comment type for multiline comments, called @dfn{block
  172. comments}. This type of comment begins with the character sequence
  173. @code{#!} and ends with the characters @code{!#}, which must appear on a
  174. line of their own. These comments are compatible with the block
  175. comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell
  176. (scsh)}). The characters @code{#!} were chosen because they are the
  177. magic characters used in shell scripts for indicating that the name of
  178. the program for executing the script follows on the same line.
  179. Thus a Guile script often starts like this.
  180. @lisp
  181. #! /usr/local/bin/guile -s
  182. !#
  183. @end lisp
  184. More details on Guile scripting can be found in the scripting section
  185. (@pxref{Guile Scripting}).
  186. @cindex R6RS block comments
  187. @cindex SRFI-30 block comments
  188. Similarly, Guile (starting from version 2.0) supports nested block
  189. comments as specified by R6RS and
  190. @url{http://srfi.schemers.org/srfi-30/srfi-30.html, SRFI-30}:
  191. @lisp
  192. (+ #| this is a #| nested |# block comment |# 2)
  193. @result{} 3
  194. @end lisp
  195. For backward compatibility, this syntax can be overridden with
  196. @code{read-hash-extend} (@pxref{Reader Extensions,
  197. @code{read-hash-extend}}).
  198. There is one special case where the contents of a comment can actually
  199. affect the interpretation of code. When a character encoding
  200. declaration, such as @code{coding: utf-8} appears in one of the first
  201. few lines of a source file, it indicates to Guile's default reader
  202. that this source code file is not ASCII. For details see @ref{Character
  203. Encoding of Source Files}.
  204. @node Case Sensitivity
  205. @subsubsection Case Sensitivity
  206. @c FIXME::martin: Review me!
  207. Scheme as defined in R5RS is not case sensitive when reading symbols.
  208. Guile, on the contrary is case sensitive by default, so the identifiers
  209. @lisp
  210. guile-whuzzy
  211. Guile-Whuzzy
  212. @end lisp
  213. are the same in R5RS Scheme, but are different in Guile.
  214. It is possible to turn off case sensitivity in Guile by setting the
  215. reader option @code{case-insensitive}. For more information on reader
  216. options, @xref{Scheme Read}.
  217. @lisp
  218. (read-enable 'case-insensitive)
  219. @end lisp
  220. Note that this is seldom a problem, because Scheme programmers tend not
  221. to use uppercase letters in their identifiers anyway.
  222. @node Keyword Syntax
  223. @subsubsection Keyword Syntax
  224. @node Reader Extensions
  225. @subsubsection Reader Extensions
  226. @deffn {Scheme Procedure} read-hash-extend chr proc
  227. @deffnx {C Function} scm_read_hash_extend (chr, proc)
  228. Install the procedure @var{proc} for reading expressions
  229. starting with the character sequence @code{#} and @var{chr}.
  230. @var{proc} will be called with two arguments: the character
  231. @var{chr} and the port to read further data from. The object
  232. returned will be the return value of @code{read}.
  233. Passing @code{#f} for @var{proc} will remove a previous setting.
  234. @end deffn
  235. @node Scheme Read
  236. @subsection Reading Scheme Code
  237. @rnindex read
  238. @deffn {Scheme Procedure} read [port]
  239. @deffnx {C Function} scm_read (port)
  240. Read an s-expression from the input port @var{port}, or from
  241. the current input port if @var{port} is not specified.
  242. Any whitespace before the next token is discarded.
  243. @end deffn
  244. The behaviour of Guile's Scheme reader can be modified by manipulating
  245. its read options.
  246. @cindex options - read
  247. @cindex read options
  248. @deffn {Scheme Procedure} read-options [setting]
  249. Display the current settings of the read options. If @var{setting} is
  250. omitted, only a short form of the current read options is printed.
  251. Otherwise if @var{setting} is the symbol @code{help}, a complete options
  252. description is displayed.
  253. @end deffn
  254. The set of available options, and their default values, may be had by
  255. invoking @code{read-options} at the prompt.
  256. @smalllisp
  257. scheme@@(guile-user)> (read-options)
  258. (square-brackets keywords #f positions)
  259. scheme@@(guile-user)> (read-options 'help)
  260. copy no Copy source code expressions.
  261. positions yes Record positions of source code expressions.
  262. case-insensitive no Convert symbols to lower case.
  263. keywords #f Style of keyword recognition: #f, 'prefix or 'postfix.
  264. r6rs-hex-escapes no Use R6RS variable-length character and string hex escapes.
  265. square-brackets yes Treat `[' and `]' as parentheses, for R6RS compatibility.
  266. hungry-eol-escapes no In strings, consume leading whitespace after an
  267. escaped end-of-line.
  268. @end smalllisp
  269. The boolean options may be toggled with @code{read-enable} and
  270. @code{read-disable}. The non-boolean @code{keywords} option must be set
  271. using @code{read-set!}.
  272. @deffn {Scheme Procedure} read-enable option-name
  273. @deffnx {Scheme Procedure} read-disable option-name
  274. @deffnx {Scheme Syntax} read-set! option-name value
  275. Modify the read options. @code{read-enable} should be used with boolean
  276. options and switches them on, @code{read-disable} switches them off.
  277. @code{read-set!} can be used to set an option to a specific value. Due
  278. to historical oddities, it is a macro that expects an unquoted option
  279. name.
  280. @end deffn
  281. For example, to make @code{read} fold all symbols to their lower case
  282. (perhaps for compatibility with older Scheme code), you can enter:
  283. @lisp
  284. (read-enable 'case-insensitive)
  285. @end lisp
  286. For more information on the effect of the @code{r6rs-hex-escapes} and
  287. @code{hungry-eol-escapes} options, see (@pxref{String Syntax}).
  288. @node Scheme Write
  289. @subsection Writing Scheme Values
  290. Any scheme value may be written to a port. Not all values may be read
  291. back in (@pxref{Scheme Read}), however.
  292. @rnindex write
  293. @rnindex print
  294. @deffn {Scheme Procedure} write obj [port]
  295. Send a representation of @var{obj} to @var{port} or to the current
  296. output port if not given.
  297. The output is designed to be machine readable, and can be read back
  298. with @code{read} (@pxref{Scheme Read}). Strings are printed in
  299. double quotes, with escapes if necessary, and characters are printed in
  300. @samp{#\} notation.
  301. @end deffn
  302. @rnindex display
  303. @deffn {Scheme Procedure} display obj [port]
  304. Send a representation of @var{obj} to @var{port} or to the current
  305. output port if not given.
  306. The output is designed for human readability, it differs from
  307. @code{write} in that strings are printed without double quotes and
  308. escapes, and characters are printed as per @code{write-char}, not in
  309. @samp{#\} form.
  310. @end deffn
  311. As was the case with the Scheme reader, there are a few options that
  312. affect the behavior of the Scheme printer.
  313. @cindex options - print
  314. @cindex print options
  315. @deffn {Scheme Procedure} print-options [setting]
  316. Display the current settings of the read options. If @var{setting} is
  317. omitted, only a short form of the current read options is
  318. printed. Otherwise if @var{setting} is the symbol @code{help}, a
  319. complete options description is displayed.
  320. @end deffn
  321. The set of available options, and their default values, may be had by
  322. invoking @code{print-options} at the prompt.
  323. @smalllisp
  324. scheme@@(guile-user)> (print-options)
  325. (quote-keywordish-symbols reader highlight-suffix "@}" highlight-prefix "@{")
  326. scheme@@(guile-user)> (print-options 'help)
  327. highlight-prefix @{ The string to print before highlighted values.
  328. highlight-suffix @} The string to print after highlighted values.
  329. quote-keywordish-symbols reader How to print symbols that have a colon
  330. as their first or last character. The
  331. value '#f' does not quote the colons;
  332. '#t' quotes them; 'reader' quotes them
  333. when the reader option 'keywords' is
  334. not '#f'.
  335. escape-newlines yes Render newlines as \n when printing
  336. using `write'.
  337. @end smalllisp
  338. These options may be modified with the print-set! syntax.
  339. @deffn {Scheme Syntax} print-set! option-name value
  340. Modify the print options. Due to historical oddities, @code{print-set!}
  341. is a macro that expects an unquoted option name.
  342. @end deffn
  343. @node Fly Evaluation
  344. @subsection Procedures for On the Fly Evaluation
  345. Scheme has the lovely property that its expressions may be represented
  346. as data. The @code{eval} procedure takes a Scheme datum and evaluates
  347. it as code.
  348. @rnindex eval
  349. @c ARGFIXME environment/environment specifier
  350. @deffn {Scheme Procedure} eval exp module_or_state
  351. @deffnx {C Function} scm_eval (exp, module_or_state)
  352. Evaluate @var{exp}, a list representing a Scheme expression,
  353. in the top-level environment specified by @var{module_or_state}.
  354. While @var{exp} is evaluated (using @code{primitive-eval}),
  355. @var{module_or_state} is made the current module. The current module
  356. is reset to its previous value when @code{eval} returns.
  357. XXX - dynamic states.
  358. Example: (eval '(+ 1 2) (interaction-environment))
  359. @end deffn
  360. @rnindex interaction-environment
  361. @deffn {Scheme Procedure} interaction-environment
  362. @deffnx {C Function} scm_interaction_environment ()
  363. Return a specifier for the environment that contains
  364. implementation--defined bindings, typically a superset of those
  365. listed in the report. The intent is that this procedure will
  366. return the environment in which the implementation would
  367. evaluate expressions dynamically typed by the user.
  368. @end deffn
  369. @xref{Environments}, for other environments.
  370. One does not always receive code as Scheme data, of course, and this is
  371. especially the case for Guile's other language implementations
  372. (@pxref{Other Languages}). For the case in which all you have is a
  373. string, we have @code{eval-string}. There is a legacy version of this
  374. procedure in the default environment, but you really want the one from
  375. @code{(ice-9 eval-string)}, so load it up:
  376. @example
  377. (use-modules (ice-9 eval-string))
  378. @end example
  379. @deffn {Scheme Procedure} eval-string string [module=#f] [file=#f] [line=#f] [column=#f] [lang=(current-language)] [compile?=#f]
  380. Parse @var{string} according to the current language, normally Scheme.
  381. Evaluate or compile the expressions it contains, in order, returning the
  382. last expression.
  383. If the @var{module} keyword argument is set, save a module excursion
  384. (@pxref{Module System Reflection}) and set the current module to
  385. @var{module} before evaluation.
  386. The @var{file}, @var{line}, and @var{column} keyword arguments can be
  387. used to indicate that the source string begins at a particular source
  388. location.
  389. Finally, @var{lang} is a language, defaulting to the current language,
  390. and the expression is compiled if @var{compile?} is true or there is no
  391. evaluator for the given language.
  392. @end deffn
  393. @deffn {C Function} scm_eval_string (string)
  394. @deffnx {C Function} scm_eval_string_in_module (string, module)
  395. These C bindings call @code{eval-string} from @code{(ice-9
  396. eval-string)}, evaluating within @var{module} or the current module.
  397. @end deffn
  398. @deftypefn {C Function} SCM scm_c_eval_string (const char *string)
  399. @code{scm_eval_string}, but taking a C string in locale encoding instead
  400. of an @code{SCM}.
  401. @end deftypefn
  402. @deffn {Scheme Procedure} apply proc arg @dots{} arglst
  403. @deffnx {C Function} scm_apply_0 (proc, arglst)
  404. @deffnx {C Function} scm_apply_1 (proc, arg1, arglst)
  405. @deffnx {C Function} scm_apply_2 (proc, arg1, arg2, arglst)
  406. @deffnx {C Function} scm_apply_3 (proc, arg1, arg2, arg3, arglst)
  407. @deffnx {C Function} scm_apply (proc, arg, rest)
  408. @rnindex apply
  409. Call @var{proc} with arguments @var{arg} @dots{} and the
  410. elements of the @var{arglst} list.
  411. @code{scm_apply} takes parameters corresponding to a Scheme level
  412. @code{(lambda (proc arg1 . rest) ...)}. So @var{arg1} and all but the
  413. last element of the @var{rest} list make up @var{arg} @dots{}, and the
  414. last element of @var{rest} is the @var{arglst} list. Or if @var{rest}
  415. is the empty list @code{SCM_EOL} then there's no @var{arg} @dots{}, and
  416. (@var{arg1}) is the @var{arglst}.
  417. @var{arglst} is not modified, but the @var{rest} list passed to
  418. @code{scm_apply} is modified.
  419. @end deffn
  420. @deffn {C Function} scm_call_0 (proc)
  421. @deffnx {C Function} scm_call_1 (proc, arg1)
  422. @deffnx {C Function} scm_call_2 (proc, arg1, arg2)
  423. @deffnx {C Function} scm_call_3 (proc, arg1, arg2, arg3)
  424. @deffnx {C Function} scm_call_4 (proc, arg1, arg2, arg3, arg4)
  425. @deffnx {C Function} scm_call_5 (proc, arg1, arg2, arg3, arg4, arg5)
  426. @deffnx {C Function} scm_call_6 (proc, arg1, arg2, arg3, arg4, arg5, arg6)
  427. @deffnx {C Function} scm_call_7 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  428. @deffnx {C Function} scm_call_8 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  429. @deffnx {C Function} scm_call_9 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  430. Call @var{proc} with the given arguments.
  431. @end deffn
  432. @deffn {C Function} scm_call (proc, ...)
  433. Call @var{proc} with any number of arguments. The argument list must be
  434. terminated by @code{SCM_UNDEFINED}. For example:
  435. @example
  436. scm_call (scm_c_public_ref ("guile", "+"),
  437. scm_from_int (1),
  438. scm_from_int (2),
  439. SCM_UNDEFINED);
  440. @end example
  441. @end deffn
  442. @deffn {C Function} scm_call_n (proc, argv, nargs)
  443. Call @var{proc} with the array of arguments @var{argv}, as a
  444. @code{SCM*}. The length of the arguments should be passed in
  445. @var{nargs}, as a @code{size_t}.
  446. @end deffn
  447. @deffn {Scheme Procedure} apply:nconc2last lst
  448. @deffnx {C Function} scm_nconc2last (lst)
  449. @var{lst} should be a list (@var{arg1} @dots{} @var{argN}
  450. @var{arglst}), with @var{arglst} being a list. This function returns
  451. a list comprising @var{arg1} to @var{argN} plus the elements of
  452. @var{arglst}. @var{lst} is modified to form the return. @var{arglst}
  453. is not modified, though the return does share structure with it.
  454. This operation collects up the arguments from a list which is
  455. @code{apply} style parameters.
  456. @end deffn
  457. @deffn {Scheme Procedure} primitive-eval exp
  458. @deffnx {C Function} scm_primitive_eval (exp)
  459. Evaluate @var{exp} in the top-level environment specified by
  460. the current module.
  461. @end deffn
  462. @node Compilation
  463. @subsection Compiling Scheme Code
  464. The @code{eval} procedure directly interprets the S-expression
  465. representation of Scheme. An alternate strategy for evaluation is to
  466. determine ahead of time what computations will be necessary to
  467. evaluate the expression, and then use that recipe to produce the
  468. desired results. This is known as @dfn{compilation}.
  469. While it is possible to compile simple Scheme expressions such as
  470. @code{(+ 2 2)} or even @code{"Hello world!"}, compilation is most
  471. interesting in the context of procedures. Compiling a lambda expression
  472. produces a compiled procedure, which is just like a normal procedure
  473. except typically much faster, because it can bypass the generic
  474. interpreter.
  475. Functions from system modules in a Guile installation are normally
  476. compiled already, so they load and run quickly.
  477. @cindex automatic compilation
  478. Note that well-written Scheme programs will not typically call the
  479. procedures in this section, for the same reason that it is often bad
  480. taste to use @code{eval}. By default, Guile automatically compiles any
  481. files it encounters that have not been compiled yet (@pxref{Invoking
  482. Guile, @code{--auto-compile}}). The compiler can also be invoked
  483. explicitly from the shell as @code{guild compile foo.scm}.
  484. (Why are calls to @code{eval} and @code{compile} usually in bad taste?
  485. Because they are limited, in that they can only really make sense for
  486. top-level expressions. Also, most needs for ``compile-time''
  487. computation are fulfilled by macros and closures. Of course one good
  488. counterexample is the REPL itself, or any code that reads expressions
  489. from a port.)
  490. Automatic compilation generally works transparently, without any need
  491. for user intervention. However Guile does not yet do proper dependency
  492. tracking, so that if file @file{@var{a}.scm} uses macros from
  493. @file{@var{b}.scm}, and @var{@var{b}.scm} changes, @code{@var{a}.scm}
  494. would not be automatically recompiled. To forcibly invalidate the
  495. auto-compilation cache, pass the @code{--fresh-auto-compile} option to
  496. Guile, or set the @code{GUILE_AUTO_COMPILE} environment variable to
  497. @code{fresh} (instead of to @code{0} or @code{1}).
  498. For more information on the compiler itself, see @ref{Compiling to the
  499. Virtual Machine}. For information on the virtual machine, see @ref{A
  500. Virtual Machine for Guile}.
  501. The command-line interface to Guile's compiler is the @command{guild
  502. compile} command:
  503. @deffn {Command} {guild compile} [@option{option}...] @var{file}...
  504. Compile @var{file}, a source file, and store bytecode in the compilation cache
  505. or in the file specified by the @option{-o} option. The following options are
  506. available:
  507. @table @option
  508. @item -L @var{dir}
  509. @itemx --load-path=@var{dir}
  510. Add @var{dir} to the front of the module load path.
  511. @item -o @var{ofile}
  512. @itemx --output=@var{ofile}
  513. Write output bytecode to @var{ofile}. By convention, bytecode file
  514. names end in @code{.go}. When @option{-o} is omitted, the output file
  515. name is as for @code{compile-file} (see below).
  516. @item -W @var{warning}
  517. @itemx --warn=@var{warning}
  518. @cindex warnings, compiler
  519. Emit warnings of type @var{warning}; use @code{--warn=help} for a list
  520. of available warnings and their description. Currently recognized
  521. warnings include @code{unused-variable}, @code{unused-toplevel},
  522. @code{unbound-variable}, @code{arity-mismatch}, and @code{format}.
  523. @item -f @var{lang}
  524. @itemx --from=@var{lang}
  525. Use @var{lang} as the source language of @var{file}. If this option is omitted,
  526. @code{scheme} is assumed.
  527. @item -t @var{lang}
  528. @itemx --to=@var{lang}
  529. Use @var{lang} as the target language of @var{file}. If this option is omitted,
  530. @code{objcode} is assumed.
  531. @item -T @var{target}
  532. @itemx --target=@var{target}
  533. Produce bytecode for @var{target} instead of @var{%host-type}
  534. (@pxref{Build Config, %host-type}). Target must be a valid GNU triplet,
  535. such as @code{armv5tel-unknown-linux-gnueabi} (@pxref{Specifying Target
  536. Triplets,,, autoconf, GNU Autoconf Manual}).
  537. @end table
  538. Each @var{file} is assumed to be UTF-8-encoded, unless it contains a
  539. coding declaration as recognized by @code{file-encoding}
  540. (@pxref{Character Encoding of Source Files}).
  541. @end deffn
  542. The compiler can also be invoked directly by Scheme code using the procedures
  543. below:
  544. @deffn {Scheme Procedure} compile exp [env=#f] [from=(current-language)] [to=value] [opts=()]
  545. Compile the expression @var{exp} in the environment @var{env}. If
  546. @var{exp} is a procedure, the result will be a compiled procedure;
  547. otherwise @code{compile} is mostly equivalent to @code{eval}.
  548. For a discussion of languages and compiler options, @xref{Compiling to
  549. the Virtual Machine}.
  550. @end deffn
  551. @deffn {Scheme Procedure} compile-file file [output-file=#f] @
  552. [from=(current-language)] [to='objcode] @
  553. [env=(default-environment from)] [opts='()] @
  554. [canonicalization 'relative]
  555. Compile the file named @var{file}.
  556. Output will be written to a @var{output-file}. If you do not supply an
  557. output file name, output is written to a file in the cache directory, as
  558. computed by @code{(compiled-file-name @var{file})}.
  559. @var{from} and @var{to} specify the source and target languages.
  560. @xref{Compiling to the Virtual Machine}, for more information on these
  561. options, and on @var{env} and @var{opts}.
  562. As with @command{guild compile}, @var{file} is assumed to be
  563. UTF-8-encoded unless it contains a coding declaration.
  564. @end deffn
  565. @deffn {Scheme Procedure} compiled-file-name file
  566. Compute a cached location for a compiled version of a Scheme file named
  567. @var{file}.
  568. This file will usually be below the @file{$HOME/.cache/guile/ccache}
  569. directory, depending on the value of the @env{XDG_CACHE_HOME}
  570. environment variable. The intention is that @code{compiled-file-name}
  571. provides a fallback location for caching auto-compiled files. If you
  572. want to place a compile file in the @code{%load-compiled-path}, you
  573. should pass the @var{output-file} option to @code{compile-file},
  574. explicitly.
  575. @end deffn
  576. @defvr {Scheme Variable} %auto-compilation-options
  577. This variable contains the options passed to the @code{compile-file}
  578. procedure when auto-compiling source files. By default, it enables
  579. useful compilation warnings. It can be customized from @file{~/.guile}.
  580. @end defvr
  581. @node Loading
  582. @subsection Loading Scheme Code from File
  583. @rnindex load
  584. @deffn {Scheme Procedure} load filename [reader]
  585. Load @var{filename} and evaluate its contents in the top-level
  586. environment.
  587. @var{reader} if provided should be either @code{#f}, or a procedure with
  588. the signature @code{(lambda (port) @dots{})} which reads the next
  589. expression from @var{port}. If @var{reader} is @code{#f} or absent,
  590. Guile's built-in @code{read} procedure is used (@pxref{Scheme Read}).
  591. The @var{reader} argument takes effect by setting the value of the
  592. @code{current-reader} fluid (see below) before loading the file, and
  593. restoring its previous value when loading is complete. The Scheme code
  594. inside @var{filename} can itself change the current reader procedure on
  595. the fly by setting @code{current-reader} fluid.
  596. If the variable @code{%load-hook} is defined, it should be bound to a
  597. procedure that will be called before any code is loaded. See
  598. documentation for @code{%load-hook} later in this section.
  599. @end deffn
  600. @deffn {Scheme Procedure} load-compiled filename
  601. Load the compiled file named @var{filename}.
  602. Compiling a source file (@pxref{Read/Load/Eval/Compile}) and then
  603. calling @code{load-compiled} on the resulting file is equivalent to
  604. calling @code{load} on the source file.
  605. @end deffn
  606. @deffn {Scheme Procedure} primitive-load filename
  607. @deffnx {C Function} scm_primitive_load (filename)
  608. Load the file named @var{filename} and evaluate its contents in the
  609. top-level environment. @var{filename} must either be a full pathname or
  610. be a pathname relative to the current directory. If the variable
  611. @code{%load-hook} is defined, it should be bound to a procedure that
  612. will be called before any code is loaded. See the documentation for
  613. @code{%load-hook} later in this section.
  614. @end deffn
  615. @deftypefn {C Function} SCM scm_c_primitive_load (const char *filename)
  616. @code{scm_primitive_load}, but taking a C string instead of an
  617. @code{SCM}.
  618. @end deftypefn
  619. @defvar current-reader
  620. @code{current-reader} holds the read procedure that is currently being
  621. used by the above loading procedures to read expressions (from the file
  622. that they are loading). @code{current-reader} is a fluid, so it has an
  623. independent value in each dynamic root and should be read and set using
  624. @code{fluid-ref} and @code{fluid-set!} (@pxref{Fluids and Dynamic
  625. States}).
  626. Changing @code{current-reader} is typically useful to introduce local
  627. syntactic changes, such that code following the @code{fluid-set!} call
  628. is read using the newly installed reader. The @code{current-reader}
  629. change should take place at evaluation time when the code is evaluated,
  630. or at compilation time when the code is compiled:
  631. @findex eval-when
  632. @example
  633. (eval-when (compile eval)
  634. (fluid-set! current-reader my-own-reader))
  635. @end example
  636. The @code{eval-when} form above ensures that the @code{current-reader}
  637. change occurs at the right time.
  638. @end defvar
  639. @defvar %load-hook
  640. A procedure to be called @code{(%load-hook @var{filename})} whenever a
  641. file is loaded, or @code{#f} for no such call. @code{%load-hook} is
  642. used by all of the loading functions (@code{load} and
  643. @code{primitive-load}, and @code{load-from-path} and
  644. @code{primitive-load-path} documented in the next section).
  645. For example an application can set this to show what's loaded,
  646. @example
  647. (set! %load-hook (lambda (filename)
  648. (format #t "Loading ~a ...\n" filename)))
  649. (load-from-path "foo.scm")
  650. @print{} Loading /usr/local/share/guile/site/foo.scm ...
  651. @end example
  652. @end defvar
  653. @deffn {Scheme Procedure} current-load-port
  654. @deffnx {C Function} scm_current_load_port ()
  655. Return the current-load-port.
  656. The load port is used internally by @code{primitive-load}.
  657. @end deffn
  658. @node Load Paths
  659. @subsection Load Paths
  660. The procedure in the previous section look for Scheme code in the file
  661. system at specific location. Guile also has some procedures to search
  662. the load path for code.
  663. @cindex @env{GUILE_LOAD_PATH}
  664. @defvar %load-path
  665. List of directories which should be searched for Scheme modules and
  666. libraries. @code{%load-path} is initialized when Guile starts up to
  667. @code{(list (%site-dir) (%library-dir) (%package-data-dir))}, prepended
  668. with the contents of the @env{GUILE_LOAD_PATH} environment variable, if
  669. it is set. @xref{Build Config}, for more on @code{%site-dir} and
  670. related procedures.
  671. @end defvar
  672. @deffn {Scheme Procedure} load-from-path filename
  673. Similar to @code{load}, but searches for @var{filename} in the load
  674. paths. Preferentially loads a compiled version of the file, if it is
  675. available and up-to-date.
  676. @end deffn
  677. A user can extend the load path by calling @code{add-to-load-path}.
  678. @deffn {Scheme Syntax} add-to-load-path dir
  679. Add @var{dir} to the load path.
  680. @end deffn
  681. For example, a script might include this form to add the directory that
  682. it is in to the load path:
  683. @example
  684. (add-to-load-path (dirname (current-filename)))
  685. @end example
  686. It's better to use @code{add-to-load-path} than to modify
  687. @code{%load-path} directly, because @code{add-to-load-path} takes care
  688. of modifying the path both at compile-time and at run-time.
  689. @deffn {Scheme Procedure} primitive-load-path filename [exception-on-not-found]
  690. @deffnx {C Function} scm_primitive_load_path (filename)
  691. Search @code{%load-path} for the file named @var{filename} and
  692. load it into the top-level environment. If @var{filename} is a
  693. relative pathname and is not found in the list of search paths,
  694. an error is signalled. Preferentially loads a compiled version of the
  695. file, if it is available and up-to-date.
  696. By default or if @var{exception-on-not-found} is true, an exception is
  697. raised if @var{filename} is not found. If @var{exception-on-not-found}
  698. is @code{#f} and @var{filename} is not found, no exception is raised and
  699. @code{#f} is returned. For compatibility with Guile 1.8 and earlier,
  700. the C function takes only one argument, which can be either a string
  701. (the file name) or an argument list.
  702. @end deffn
  703. @deffn {Scheme Procedure} %search-load-path filename
  704. @deffnx {C Function} scm_sys_search_load_path (filename)
  705. Search @code{%load-path} for the file named @var{filename}, which must
  706. be readable by the current user. If @var{filename} is found in the list
  707. of paths to search or is an absolute pathname, return its full pathname.
  708. Otherwise, return @code{#f}. Filenames may have any of the optional
  709. extensions in the @code{%load-extensions} list; @code{%search-load-path}
  710. will try each extension automatically.
  711. @end deffn
  712. @defvar %load-extensions
  713. A list of default file extensions for files containing Scheme code.
  714. @code{%search-load-path} tries each of these extensions when looking for
  715. a file to load. By default, @code{%load-extensions} is bound to the
  716. list @code{("" ".scm")}.
  717. @end defvar
  718. As mentioned above, when Guile searches the @code{%load-path} for a
  719. source file, it will also search the @code{%load-compiled-path} for a
  720. corresponding compiled file. If the compiled file is as new or newer
  721. than the source file, it will be loaded instead of the source file,
  722. using @code{load-compiled}.
  723. @defvar %load-compiled-path
  724. Like @code{%load-path}, but for compiled files. By default, this path
  725. has two entries: one for compiled files from Guile itself, and one for
  726. site packages.
  727. @end defvar
  728. When @code{primitive-load-path} searches the @code{%load-compiled-path}
  729. for a corresponding compiled file for a relative path it does so by
  730. appending @code{.go} to the relative path. For example, searching for
  731. @code{ice-9/popen} could find
  732. @code{/usr/lib/guile/2.0/ccache/ice-9/popen.go}, and use it instead of
  733. @code{/usr/share/guile/2.0/ice-9/popen.scm}.
  734. If @code{primitive-load-path} does not find a corresponding @code{.go}
  735. file in the @code{%load-compiled-path}, or the @code{.go} file is out of
  736. date, it will search for a corresponding auto-compiled file in the
  737. fallback path, possibly creating one if one does not exist.
  738. @xref{Installing Site Packages}, for more on how to correctly install
  739. site packages. @xref{Modules and the File System}, for more on the
  740. relationship between load paths and modules. @xref{Compilation}, for
  741. more on the fallback path and auto-compilation.
  742. Finally, there are a couple of helper procedures for general path
  743. manipulation.
  744. @deffn {Scheme Procedure} parse-path path [tail]
  745. @deffnx {C Function} scm_parse_path (path, tail)
  746. Parse @var{path}, which is expected to be a colon-separated string, into
  747. a list and return the resulting list with @var{tail} appended. If
  748. @var{path} is @code{#f}, @var{tail} is returned.
  749. @end deffn
  750. @deffn {Scheme Procedure} search-path path filename [extensions [require-exts?]]
  751. @deffnx {C Function} scm_search_path (path, filename, rest)
  752. Search @var{path} for a directory containing a file named
  753. @var{filename}. The file must be readable, and not a directory. If we
  754. find one, return its full filename; otherwise, return @code{#f}. If
  755. @var{filename} is absolute, return it unchanged. If given,
  756. @var{extensions} is a list of strings; for each directory in @var{path},
  757. we search for @var{filename} concatenated with each @var{extension}. If
  758. @var{require-exts?} is true, require that the returned file name have
  759. one of the given extensions; if @var{require-exts?} is not given, it
  760. defaults to @code{#f}.
  761. For compatibility with Guile 1.8 and earlier, the C function takes only
  762. three arguments.
  763. @end deffn
  764. @node Character Encoding of Source Files
  765. @subsection Character Encoding of Source Files
  766. @cindex source file encoding
  767. @cindex primitive-load
  768. @cindex load
  769. Scheme source code files are usually encoded in ASCII, but, the
  770. built-in reader can interpret other character encodings. The
  771. procedure @code{primitive-load}, and by extension the functions that
  772. call it, such as @code{load}, first scan the top 500 characters of the
  773. file for a coding declaration.
  774. A coding declaration has the form @code{coding: XXXXXX}, where
  775. @code{XXXXXX} is the name of a character encoding in which the source
  776. code file has been encoded. The coding declaration must appear in a
  777. scheme comment. It can either be a semicolon-initiated comment or a block
  778. @code{#!} comment.
  779. The name of the character encoding in the coding declaration is
  780. typically lower case and containing only letters, numbers, and hyphens,
  781. as recognized by @code{set-port-encoding!} (@pxref{Ports,
  782. @code{set-port-encoding!}}). Common examples of character encoding
  783. names are @code{utf-8} and @code{iso-8859-1},
  784. @url{http://www.iana.org/assignments/character-sets, as defined by
  785. IANA}. Thus, the coding declaration is mostly compatible with Emacs.
  786. However, there are some differences in encoding names recognized by
  787. Emacs and encoding names defined by IANA, the latter being essentially a
  788. subset of the former. For instance, @code{latin-1} is a valid encoding
  789. name for Emacs, but it's not according to the IANA standard, which Guile
  790. follows; instead, you should use @code{iso-8859-1}, which is both
  791. understood by Emacs and dubbed by IANA (IANA writes it uppercase but
  792. Emacs wants it lowercase and Guile is case insensitive.)
  793. For source code, only a subset of all possible character encodings can
  794. be interpreted by the built-in source code reader. Only those
  795. character encodings in which ASCII text appears unmodified can be
  796. used. This includes @code{UTF-8} and @code{ISO-8859-1} through
  797. @code{ISO-8859-15}. The multi-byte character encodings @code{UTF-16}
  798. and @code{UTF-32} may not be used because they are not compatible with
  799. ASCII.
  800. @cindex read
  801. @cindex encoding
  802. @cindex port encoding
  803. @findex set-port-encoding!
  804. There might be a scenario in which one would want to read non-ASCII
  805. code from a port, such as with the function @code{read}, instead of
  806. with @code{load}. If the port's character encoding is the same as the
  807. encoding of the code to be read by the port, not other special
  808. handling is necessary. The port will automatically do the character
  809. encoding conversion. The functions @code{setlocale} or by
  810. @code{set-port-encoding!} are used to set port encodings
  811. (@pxref{Ports}).
  812. If a port is used to read code of unknown character encoding, it can
  813. accomplish this in three steps. First, the character encoding of the
  814. port should be set to ISO-8859-1 using @code{set-port-encoding!}.
  815. Then, the procedure @code{file-encoding}, described below, is used to
  816. scan for a coding declaration when reading from the port. As a side
  817. effect, it rewinds the port after its scan is complete. After that,
  818. the port's character encoding should be set to the encoding returned
  819. by @code{file-encoding}, if any, again by using
  820. @code{set-port-encoding!}. Then the code can be read as normal.
  821. @deffn {Scheme Procedure} file-encoding port
  822. @deffnx {C Function} scm_file_encoding (port)
  823. Scan the port for an Emacs-like character coding declaration near the
  824. top of the contents of a port with random-accessible contents
  825. (@pxref{Recognize Coding, how Emacs recognizes file encoding,, emacs,
  826. The GNU Emacs Reference Manual}). The coding declaration is of the form
  827. @code{coding: XXXXX} and must appear in a Scheme comment. Return a
  828. string containing the character encoding of the file if a declaration
  829. was found, or @code{#f} otherwise. The port is rewound.
  830. @end deffn
  831. @node Delayed Evaluation
  832. @subsection Delayed Evaluation
  833. @cindex delayed evaluation
  834. @cindex promises
  835. Promises are a convenient way to defer a calculation until its result
  836. is actually needed, and to run such a calculation only once.
  837. @deffn syntax delay expr
  838. @rnindex delay
  839. Return a promise object which holds the given @var{expr} expression,
  840. ready to be evaluated by a later @code{force}.
  841. @end deffn
  842. @deffn {Scheme Procedure} promise? obj
  843. @deffnx {C Function} scm_promise_p (obj)
  844. Return true if @var{obj} is a promise.
  845. @end deffn
  846. @rnindex force
  847. @deffn {Scheme Procedure} force p
  848. @deffnx {C Function} scm_force (p)
  849. Return the value obtained from evaluating the @var{expr} in the given
  850. promise @var{p}. If @var{p} has previously been forced then its
  851. @var{expr} is not evaluated again, instead the value obtained at that
  852. time is simply returned.
  853. During a @code{force}, an @var{expr} can call @code{force} again on
  854. its own promise, resulting in a recursive evaluation of that
  855. @var{expr}. The first evaluation to return gives the value for the
  856. promise. Higher evaluations run to completion in the normal way, but
  857. their results are ignored, @code{force} always returns the first
  858. value.
  859. @end deffn
  860. @node Local Evaluation
  861. @subsection Local Evaluation
  862. Guile includes a facility to capture a lexical environment, and later
  863. evaluate a new expression within that environment. This code is
  864. implemented in a module.
  865. @example
  866. (use-modules (ice-9 local-eval))
  867. @end example
  868. @deffn syntax the-environment
  869. Captures and returns a lexical environment for use with
  870. @code{local-eval} or @code{local-compile}.
  871. @end deffn
  872. @deffn {Scheme Procedure} local-eval exp env
  873. @deffnx {C Function} scm_local_eval (exp, env)
  874. @deffnx {Scheme Procedure} local-compile exp env [opts=()]
  875. Evaluate or compile the expression @var{exp} in the lexical environment
  876. @var{env}.
  877. @end deffn
  878. Here is a simple example, illustrating that it is the variable
  879. that gets captured, not just its value at one point in time.
  880. @example
  881. (define e (let ((x 100)) (the-environment)))
  882. (define fetch-x (local-eval '(lambda () x) e))
  883. (fetch-x)
  884. @result{} 100
  885. (local-eval '(set! x 42) e)
  886. (fetch-x)
  887. @result{} 42
  888. @end example
  889. While @var{exp} is evaluated within the lexical environment of
  890. @code{(the-environment)}, it has the dynamic environment of the call to
  891. @code{local-eval}.
  892. @code{local-eval} and @code{local-compile} can only evaluate
  893. expressions, not definitions.
  894. @example
  895. (local-eval '(define foo 42)
  896. (let ((x 100)) (the-environment)))
  897. @result{} syntax error: definition in expression context
  898. @end example
  899. Note that the current implementation of @code{(the-environment)} only
  900. captures ``normal'' lexical bindings, and pattern variables bound by
  901. @code{syntax-case}. It does not currently capture local syntax
  902. transformers bound by @code{let-syntax}, @code{letrec-syntax} or
  903. non-top-level @code{define-syntax} forms. Any attempt to reference such
  904. captured syntactic keywords via @code{local-eval} or
  905. @code{local-compile} produces an error.
  906. @node Local Inclusion
  907. @subsection Local Inclusion
  908. This section has discussed various means of linking Scheme code
  909. together: fundamentally, loading up files at run-time using @code{load}
  910. and @code{load-compiled}. Guile provides another option to compose
  911. parts of programs together at expansion-time instead of at run-time.
  912. @deffn {Scheme Syntax} include file-name
  913. Open @var{file-name}, at expansion-time, and read the Scheme forms that
  914. it contains, splicing them into the location of the @code{include},
  915. within a @code{begin}.
  916. @end deffn
  917. If you are a C programmer, if @code{load} in Scheme is like
  918. @code{dlopen} in C, consider @code{include} to be like the C
  919. preprocessor's @code{#include}. When you use @code{include}, it is as
  920. if the contents of the included file were typed in instead of the
  921. @code{include} form.
  922. Because the code is included at compile-time, it is available to the
  923. macroexpander. Syntax definitions in the included file are available to
  924. later code in the form in which the @code{include} appears, without the
  925. need for @code{eval-when}. (@xref{Eval When}.)
  926. For the same reason, compiling a form that uses @code{include} results
  927. in one compilation unit, composed of multiple files. Loading the
  928. compiled file is one @code{stat} operation for the compilation unit,
  929. instead of @code{2*@var{n}} in the case of @code{load} (once for each
  930. loaded source file, and once each corresponding compiled file, in the
  931. best case).
  932. Unlike @code{load}, @code{include} also works within nested lexical
  933. contexts. It so happens that the optimizer works best within a lexical
  934. context, because all of the uses of bindings in a lexical context are
  935. visible, so composing files by including them within a @code{(let ()
  936. ...)} can sometimes lead to important speed improvements.
  937. On the other hand, @code{include} does have all the disadvantages of
  938. early binding: once the code with the @code{include} is compiled, no
  939. change to the included file is reflected in the future behavior of the
  940. including form.
  941. Also, the particular form of @code{include}, which requires an absolute
  942. path, or a path relative to the current directory at compile-time, is
  943. not very amenable to compiling the source in one place, but then
  944. installing the source to another place. For this reason, Guile provides
  945. another form, @code{include-from-path}, which looks for the source file
  946. to include within a load path.
  947. @deffn {Scheme Syntax} include-from-path file-name
  948. Like @code{include}, but instead of expecting @code{file-name} to be an
  949. absolute file name, it is expected to be a relative path to search in
  950. the @code{%load-path}.
  951. @end deffn
  952. @code{include-from-path} is more useful when you want to install all of
  953. the source files for a package (as you should!). It makes it possible
  954. to evaluate an installed file from source, instead of relying on the
  955. @code{.go} file being up to date.
  956. @c Local Variables:
  957. @c TeX-master: "guile.texi"
  958. @c End: