Commit | Line | Data |
---|---|---|
07d83abe MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
165b10dd | 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 | ||
e4955559 AW |
7 | @node Procedures |
8 | @section Procedures | |
07d83abe MV |
9 | |
10 | @menu | |
11 | * Lambda:: Basic procedure creation using lambda. | |
12 | * Primitive Procedures:: Procedures defined in C. | |
00ce5125 | 13 | * Compiled Procedures:: Scheme procedures can be compiled. |
07d83abe | 14 | * Optional Arguments:: Handling keyword, optional and rest arguments. |
f916cbc4 | 15 | * Case-lambda:: One function, multiple arities. |
18f06db9 | 16 | * Higher-Order Functions:: Function that take or return functions. |
07d83abe MV |
17 | * Procedure Properties:: Procedure properties and meta-information. |
18 | * Procedures with Setters:: Procedures with setters. | |
165b10dd | 19 | * Inlinable Procedures:: Procedures that can be inlined. |
07d83abe MV |
20 | @end menu |
21 | ||
22 | ||
23 | @node Lambda | |
24 | @subsection Lambda: Basic Procedure Creation | |
25 | @cindex lambda | |
26 | ||
07d83abe MV |
27 | A @code{lambda} expression evaluates to a procedure. The environment |
28 | which is in effect when a @code{lambda} expression is evaluated is | |
29 | enclosed in the newly created procedure, this is referred to as a | |
30 | @dfn{closure} (@pxref{About Closure}). | |
31 | ||
32 | When a procedure created by @code{lambda} is called with some actual | |
33 | arguments, the environment enclosed in the procedure is extended by | |
34 | binding the variables named in the formal argument list to new locations | |
35 | and storing the actual arguments into these locations. Then the body of | |
2e4ef7ed | 36 | the @code{lambda} expression is evaluated sequentially. The result of |
07d83abe MV |
37 | the last expression in the procedure body is then the result of the |
38 | procedure invocation. | |
39 | ||
40 | The following examples will show how procedures can be created using | |
41 | @code{lambda}, and what you can do with these procedures. | |
42 | ||
43 | @lisp | |
44 | (lambda (x) (+ x x)) @result{} @r{a procedure} | |
45 | ((lambda (x) (+ x x)) 4) @result{} 8 | |
46 | @end lisp | |
47 | ||
48 | The fact that the environment in effect when creating a procedure is | |
49 | enclosed in the procedure is shown with this example: | |
50 | ||
51 | @lisp | |
52 | (define add4 | |
53 | (let ((x 4)) | |
54 | (lambda (y) (+ x y)))) | |
55 | (add4 6) @result{} 10 | |
56 | @end lisp | |
57 | ||
58 | ||
59 | @deffn syntax lambda formals body | |
60 | @var{formals} should be a formal argument list as described in the | |
61 | following table. | |
62 | ||
63 | @table @code | |
64 | @item (@var{variable1} @dots{}) | |
65 | The procedure takes a fixed number of arguments; when the procedure is | |
66 | called, the arguments will be stored into the newly created location for | |
67 | the formal variables. | |
68 | @item @var{variable} | |
69 | The procedure takes any number of arguments; when the procedure is | |
70 | called, the sequence of actual arguments will converted into a list and | |
71 | stored into the newly created location for the formal variable. | |
72 | @item (@var{variable1} @dots{} @var{variablen} . @var{variablen+1}) | |
73 | If a space-delimited period precedes the last variable, then the | |
74 | procedure takes @var{n} or more variables where @var{n} is the number | |
75 | of formal arguments before the period. There must be at least one | |
76 | argument before the period. The first @var{n} actual arguments will be | |
77 | stored into the newly allocated locations for the first @var{n} formal | |
78 | arguments and the sequence of the remaining actual arguments is | |
79 | converted into a list and the stored into the location for the last | |
80 | formal argument. If there are exactly @var{n} actual arguments, the | |
81 | empty list is stored into the location of the last formal argument. | |
82 | @end table | |
83 | ||
84 | The list in @var{variable} or @var{variablen+1} is always newly | |
85 | created and the procedure can modify it if desired. This is the case | |
86 | even when the procedure is invoked via @code{apply}, the required part | |
87 | of the list argument there will be copied (@pxref{Fly Evaluation,, | |
88 | Procedures for On the Fly Evaluation}). | |
89 | ||
90 | @var{body} is a sequence of Scheme expressions which are evaluated in | |
91 | order when the procedure is invoked. | |
92 | @end deffn | |
93 | ||
94 | @node Primitive Procedures | |
95 | @subsection Primitive Procedures | |
96 | @cindex primitives | |
97 | @cindex primitive procedures | |
98 | ||
99 | Procedures written in C can be registered for use from Scheme, | |
100 | provided they take only arguments of type @code{SCM} and return | |
101 | @code{SCM} values. @code{scm_c_define_gsubr} is likely to be the most | |
102 | useful mechanism, combining the process of registration | |
103 | (@code{scm_c_make_gsubr}) and definition (@code{scm_define}). | |
104 | ||
105 | @deftypefun SCM scm_c_make_gsubr (const char *name, int req, int opt, int rst, fcn) | |
106 | Register a C procedure @var{FCN} as a ``subr'' --- a primitive | |
107 | subroutine that can be called from Scheme. It will be associated with | |
108 | the given @var{name} but no environment binding will be created. The | |
109 | arguments @var{req}, @var{opt} and @var{rst} specify the number of | |
110 | required, optional and ``rest'' arguments respectively. The total | |
111 | number of these arguments should match the actual number of arguments | |
4adf9a7e | 112 | to @var{fcn}, but may not exceed 10. The number of rest arguments should be 0 or 1. |
07d83abe MV |
113 | @code{scm_c_make_gsubr} returns a value of type @code{SCM} which is a |
114 | ``handle'' for the procedure. | |
115 | @end deftypefun | |
116 | ||
117 | @deftypefun SCM scm_c_define_gsubr (const char *name, int req, int opt, int rst, fcn) | |
118 | Register a C procedure @var{FCN}, as for @code{scm_c_make_gsubr} | |
119 | above, and additionally create a top-level Scheme binding for the | |
120 | procedure in the ``current environment'' using @code{scm_define}. | |
121 | @code{scm_c_define_gsubr} returns a handle for the procedure in the | |
122 | same way as @code{scm_c_make_gsubr}, which is usually not further | |
123 | required. | |
124 | @end deftypefun | |
125 | ||
00ce5125 AW |
126 | @node Compiled Procedures |
127 | @subsection Compiled Procedures | |
128 | ||
7545ddd4 AW |
129 | The evaluation strategy given in @ref{Lambda} describes how procedures |
130 | are @dfn{interpreted}. Interpretation operates directly on expanded | |
131 | Scheme source code, recursively calling the evaluator to obtain the | |
132 | value of nested expressions. | |
f916cbc4 AW |
133 | |
134 | Most procedures are compiled, however. This means that Guile has done | |
7545ddd4 AW |
135 | some pre-computation on the procedure, to determine what it will need to |
136 | do each time the procedure runs. Compiled procedures run faster than | |
137 | interpreted procedures. | |
00ce5125 | 138 | |
f916cbc4 AW |
139 | Loading files is the normal way that compiled procedures come to |
140 | being. If Guile sees that a file is uncompiled, or that its compiled | |
141 | file is out of date, it will attempt to compile the file when it is | |
142 | loaded, and save the result to disk. Procedures can be compiled at | |
143 | runtime as well. @xref{Read/Load/Eval/Compile}, for more information | |
144 | on runtime compilation. | |
00ce5125 | 145 | |
5a069042 AW |
146 | Compiled procedures, also known as @dfn{programs}, respond all |
147 | procedures that operate on procedures. In addition, there are a few | |
46d666d4 | 148 | more accessors for low-level details on programs. |
5a069042 | 149 | |
46d666d4 AW |
150 | Most people won't need to use the routines described in this section, |
151 | but it's good to have them documented. You'll have to include the | |
152 | appropriate module first, though: | |
5a069042 AW |
153 | |
154 | @example | |
155 | (use-modules (system vm program)) | |
156 | @end example | |
157 | ||
46d666d4 AW |
158 | @deffn {Scheme Procedure} program? obj |
159 | @deffnx {C Function} scm_program_p (obj) | |
160 | Returns @code{#t} iff @var{obj} is a compiled procedure. | |
161 | @end deffn | |
162 | ||
81fd3152 AW |
163 | @deffn {Scheme Procedure} program-objcode program |
164 | @deffnx {C Function} scm_program_objcode (program) | |
73643339 AW |
165 | Returns the object code associated with this program. @xref{Bytecode |
166 | and Objcode}, for more information. | |
46d666d4 AW |
167 | @end deffn |
168 | ||
169 | @deffn {Scheme Procedure} program-objects program | |
170 | @deffnx {C Function} scm_program_objects (program) | |
171 | Returns the ``object table'' associated with this program, as a | |
172 | vector. @xref{VM Programs}, for more information. | |
5a069042 AW |
173 | @end deffn |
174 | ||
46d666d4 AW |
175 | @deffn {Scheme Procedure} program-module program |
176 | @deffnx {C Function} scm_program_module (program) | |
81fd3152 AW |
177 | Returns the module that was current when this program was created. Can |
178 | return @code{#f} if the compiler could determine that this information | |
179 | was unnecessary. | |
46d666d4 AW |
180 | @end deffn |
181 | ||
f916cbc4 AW |
182 | @deffn {Scheme Procedure} program-free-variables program |
183 | @deffnx {C Function} scm_program_free_variables (program) | |
184 | Returns the set of free variables that this program captures in its | |
185 | closure, as a vector. If a closure is code with data, you can get the | |
186 | code from @code{program-objcode}, and the data via | |
187 | @code{program-free-variables}. | |
188 | ||
189 | Some of the values captured are actually in variable ``boxes''. | |
190 | @xref{Variables and the VM}, for more information. | |
46d666d4 AW |
191 | |
192 | Users must not modify the returned value unless they think they're | |
193 | really clever. | |
194 | @end deffn | |
195 | ||
46d666d4 | 196 | @deffn {Scheme Procedure} program-meta program |
f916cbc4 | 197 | @deffnx {C Function} scm_program_meta (program) |
46d666d4 AW |
198 | Return the metadata thunk of @var{program}, or @code{#f} if it has no |
199 | metadata. | |
200 | ||
201 | When called, a metadata thunk returns a list of the following form: | |
f916cbc4 | 202 | @code{(@var{bindings} @var{sources} @var{arities} . @var{properties})}. The format |
46d666d4 AW |
203 | of each of these elements is discussed below. |
204 | @end deffn | |
205 | ||
5a069042 | 206 | @deffn {Scheme Procedure} program-bindings program |
f916cbc4 | 207 | @deffnx {Scheme Procedure} make-binding name boxed? index start end |
5a069042 | 208 | @deffnx {Scheme Procedure} binding:name binding |
f916cbc4 | 209 | @deffnx {Scheme Procedure} binding:boxed? binding |
5a069042 AW |
210 | @deffnx {Scheme Procedure} binding:index binding |
211 | @deffnx {Scheme Procedure} binding:start binding | |
212 | @deffnx {Scheme Procedure} binding:end binding | |
213 | Bindings annotations for programs, along with their accessors. | |
214 | ||
215 | Bindings declare names and liveness extents for block-local variables. | |
216 | The best way to see what these are is to play around with them at a | |
f916cbc4 | 217 | REPL. @xref{VM Concepts}, for more information. |
5a069042 | 218 | |
81fd3152 AW |
219 | Note that bindings information is stored in a program as part of its |
220 | metadata thunk, so including it in the generated object code does not | |
221 | impose a runtime performance penalty. | |
5a069042 AW |
222 | @end deffn |
223 | ||
224 | @deffn {Scheme Procedure} program-sources program | |
225 | @deffnx {Scheme Procedure} source:addr source | |
226 | @deffnx {Scheme Procedure} source:line source | |
227 | @deffnx {Scheme Procedure} source:column source | |
228 | @deffnx {Scheme Procedure} source:file source | |
229 | Source location annotations for programs, along with their accessors. | |
230 | ||
46d666d4 AW |
231 | Source location information propagates through the compiler and ends |
232 | up being serialized to the program's metadata. This information is | |
233 | keyed by the offset of the instruction pointer within the object code | |
234 | of the program. Specifically, it is keyed on the @code{ip} @emph{just | |
235 | following} an instruction, so that backtraces can find the source | |
236 | location of a call that is in progress. | |
5a069042 AW |
237 | @end deffn |
238 | ||
f916cbc4 AW |
239 | @deffn {Scheme Procedure} program-arities program |
240 | @deffnx {C Function} scm_program_arities (program) | |
241 | @deffnx {Scheme Procedure} program-arity program ip | |
242 | @deffnx {Scheme Procedure} arity:start arity | |
243 | @deffnx {Scheme Procedure} arity:end arity | |
244 | @deffnx {Scheme Procedure} arity:nreq arity | |
245 | @deffnx {Scheme Procedure} arity:nopt arity | |
246 | @deffnx {Scheme Procedure} arity:rest? arity | |
247 | @deffnx {Scheme Procedure} arity:kw arity | |
248 | @deffnx {Scheme Procedure} arity:allow-other-keys? arity | |
249 | Accessors for a representation of the ``arity'' of a program. | |
250 | ||
251 | The normal case is that a procedure has one arity. For example, | |
252 | @code{(lambda (x) x)}, takes one required argument, and that's it. One | |
253 | could access that number of required arguments via @code{(arity:nreq | |
254 | (program-arities (lambda (x) x)))}. Similarly, @code{arity:nopt} gets | |
255 | the number of optional arguments, and @code{arity:rest?} returns a true | |
256 | value if the procedure has a rest arg. | |
257 | ||
258 | @code{arity:kw} returns a list of @code{(@var{kw} . @var{idx})} pairs, | |
259 | if the procedure has keyword arguments. The @var{idx} refers to the | |
260 | @var{idx}th local variable; @xref{Variables and the VM}, for more | |
261 | information. Finally @code{arity:allow-other-keys?} returns a true | |
262 | value if other keys are allowed. @xref{Optional Arguments}, for more | |
263 | information. | |
264 | ||
265 | So what about @code{arity:start} and @code{arity:end}, then? They | |
266 | return the range of bytes in the program's bytecode for which a given | |
267 | arity is valid. You see, a procedure can actually have more than one | |
268 | arity. The question, ``what is a procedure's arity'' only really makes | |
269 | sense at certain points in the program, delimited by these | |
270 | @code{arity:start} and @code{arity:end} values. | |
271 | @end deffn | |
272 | ||
00ce5125 | 273 | |
07d83abe MV |
274 | @node Optional Arguments |
275 | @subsection Optional Arguments | |
276 | ||
07d83abe MV |
277 | Scheme procedures, as defined in R5RS, can either handle a fixed number |
278 | of actual arguments, or a fixed number of actual arguments followed by | |
279 | arbitrarily many additional arguments. Writing procedures of variable | |
280 | arity can be useful, but unfortunately, the syntactic means for handling | |
281 | argument lists of varying length is a bit inconvenient. It is possible | |
f916cbc4 | 282 | to give names to the fixed number of arguments, but the remaining |
07d83abe MV |
283 | (optional) arguments can be only referenced as a list of values |
284 | (@pxref{Lambda}). | |
285 | ||
f916cbc4 AW |
286 | For this reason, Guile provides an extension to @code{lambda}, |
287 | @code{lambda*}, which allows the user to define procedures with | |
288 | optional and keyword arguments. In addition, Guile's virtual machine | |
289 | has low-level support for optional and keyword argument dispatch. | |
290 | Calls to procedures with optional and keyword arguments can be made | |
291 | cheaply, without allocating a rest list. | |
07d83abe MV |
292 | |
293 | @menu | |
f916cbc4 AW |
294 | * lambda* and define*:: Creating advanced argument handling procedures. |
295 | * ice-9 optargs:: (ice-9 optargs) provides some utilities. | |
07d83abe MV |
296 | @end menu |
297 | ||
298 | ||
f916cbc4 AW |
299 | @node lambda* and define* |
300 | @subsubsection lambda* and define*. | |
07d83abe | 301 | |
f916cbc4 AW |
302 | @code{lambda*} is like @code{lambda}, except with some extensions to |
303 | allow optional and keyword arguments. | |
07d83abe | 304 | |
edcd3e83 KR |
305 | @deffn {library syntax} lambda* ([var@dots{}] @* [#:optional vardef@dots{}] @* [#:key vardef@dots{} [#:allow-other-keys]] @* [#:rest var | . var]) @* body |
306 | @sp 1 | |
307 | Create a procedure which takes optional and/or keyword arguments | |
308 | specified with @code{#:optional} and @code{#:key}. For example, | |
07d83abe MV |
309 | |
310 | @lisp | |
311 | (lambda* (a b #:optional c d . e) '()) | |
312 | @end lisp | |
313 | ||
edcd3e83 KR |
314 | is a procedure with fixed arguments @var{a} and @var{b}, optional |
315 | arguments @var{c} and @var{d}, and rest argument @var{e}. If the | |
07d83abe MV |
316 | optional arguments are omitted in a call, the variables for them are |
317 | bound to @code{#f}. | |
318 | ||
f916cbc4 AW |
319 | @fnindex define* |
320 | Likewise, @code{define*} is syntactic sugar for defining procedures | |
321 | using @code{lambda*}. | |
322 | ||
323 | @code{lambda*} can also make procedures with keyword arguments. For | |
324 | example, a procedure defined like this: | |
07d83abe MV |
325 | |
326 | @lisp | |
f916cbc4 AW |
327 | (define* (sir-yes-sir #:key action how-high) |
328 | (list action how-high)) | |
07d83abe MV |
329 | @end lisp |
330 | ||
f916cbc4 AW |
331 | can be called as @code{(sir-yes-sir #:action 'jump)}, |
332 | @code{(sir-yes-sir #:how-high 13)}, @code{(sir-yes-sir #:action | |
333 | 'lay-down #:how-high 0)}, or just @code{(sir-yes-sir)}. Whichever | |
334 | arguments are given as keywords are bound to values (and those not | |
335 | given are @code{#f}). | |
07d83abe | 336 | |
edcd3e83 KR |
337 | Optional and keyword arguments can also have default values to take |
338 | when not present in a call, by giving a two-element list of variable | |
339 | name and expression. For example in | |
07d83abe MV |
340 | |
341 | @lisp | |
f916cbc4 AW |
342 | (define* (frob foo #:optional (bar 42) #:key (baz 73)) |
343 | (list foo bar baz)) | |
07d83abe MV |
344 | @end lisp |
345 | ||
346 | @var{foo} is a fixed argument, @var{bar} is an optional argument with | |
347 | default value 42, and baz is a keyword argument with default value 73. | |
edcd3e83 KR |
348 | Default value expressions are not evaluated unless they are needed, |
349 | and until the procedure is called. | |
07d83abe | 350 | |
edcd3e83 KR |
351 | Normally it's an error if a call has keywords other than those |
352 | specified by @code{#:key}, but adding @code{#:allow-other-keys} to the | |
353 | definition (after the keyword argument declarations) will ignore | |
354 | unknown keywords. | |
07d83abe | 355 | |
edcd3e83 KR |
356 | If a call has a keyword given twice, the last value is used. For |
357 | example, | |
07d83abe MV |
358 | |
359 | @lisp | |
f916cbc4 AW |
360 | (define* (flips #:key (heads 0) (tails 0)) |
361 | (display (list heads tails))) | |
362 | ||
363 | (flips #:heads 37 #:tails 42 #:heads 99) | |
edcd3e83 | 364 | @print{} (99 42) |
07d83abe MV |
365 | @end lisp |
366 | ||
edcd3e83 KR |
367 | @code{#:rest} is a synonym for the dotted syntax rest argument. The |
368 | argument lists @code{(a . b)} and @code{(a #:rest b)} are equivalent | |
369 | in all respects. This is provided for more similarity to DSSSL, | |
370 | MIT-Scheme and Kawa among others, as well as for refugees from other | |
371 | Lisp dialects. | |
372 | ||
373 | When @code{#:key} is used together with a rest argument, the keyword | |
374 | parameters in a call all remain in the rest list. This is the same as | |
375 | Common Lisp. For example, | |
07d83abe | 376 | |
edcd3e83 KR |
377 | @lisp |
378 | ((lambda* (#:key (x 0) #:allow-other-keys #:rest r) | |
379 | (display r)) | |
380 | #:x 123 #:y 456) | |
381 | @print{} (#:x 123 #:y 456) | |
382 | @end lisp | |
383 | ||
384 | @code{#:optional} and @code{#:key} establish their bindings | |
f916cbc4 AW |
385 | successively, from left to right. This means default expressions can |
386 | refer back to prior parameters, for example | |
edcd3e83 KR |
387 | |
388 | @lisp | |
389 | (lambda* (start #:optional (end (+ 10 start))) | |
390 | (do ((i start (1+ i))) | |
391 | ((> i end)) | |
392 | (display i))) | |
393 | @end lisp | |
f916cbc4 AW |
394 | |
395 | The exception to this left-to-right scoping rule is the rest argument. | |
396 | If there is a rest argument, it is bound after the optional arguments, | |
397 | but before the keyword arguments. | |
07d83abe MV |
398 | @end deffn |
399 | ||
400 | ||
f916cbc4 AW |
401 | @node ice-9 optargs |
402 | @subsubsection (ice-9 optargs) | |
07d83abe | 403 | |
f916cbc4 AW |
404 | Before Guile 2.0, @code{lambda*} and @code{define*} were implemented |
405 | using macros that processed rest list arguments. This was not optimal, | |
406 | as calling procedures with optional arguments had to allocate rest | |
407 | lists at every procedure invocation. Guile 2.0 improved this | |
408 | situation by bringing optional and keyword arguments into Guile's | |
409 | core. | |
07d83abe | 410 | |
f916cbc4 AW |
411 | However there are occasions in which you have a list and want to parse |
412 | it for optional or keyword arguments. Guile's @code{(ice-9 optargs)} | |
413 | provides some macros to help with that task. | |
07d83abe | 414 | |
f916cbc4 AW |
415 | The syntax @code{let-optional} and @code{let-optional*} are for |
416 | destructuring rest argument lists and giving names to the various list | |
417 | elements. @code{let-optional} binds all variables simultaneously, while | |
418 | @code{let-optional*} binds them sequentially, consistent with @code{let} | |
419 | and @code{let*} (@pxref{Local Bindings}). | |
420 | ||
421 | @deffn {library syntax} let-optional rest-arg (binding @dots{}) expr @dots{} | |
422 | @deffnx {library syntax} let-optional* rest-arg (binding @dots{}) expr @dots{} | |
423 | These two macros give you an optional argument interface that is very | |
424 | @dfn{Schemey} and introduces no fancy syntax. They are compatible with | |
425 | the scsh macros of the same name, but are slightly extended. Each of | |
426 | @var{binding} may be of one of the forms @var{var} or @code{(@var{var} | |
427 | @var{default-value})}. @var{rest-arg} should be the rest-argument of the | |
428 | procedures these are used from. The items in @var{rest-arg} are | |
429 | sequentially bound to the variable names are given. When @var{rest-arg} | |
430 | runs out, the remaining vars are bound either to the default values or | |
431 | @code{#f} if no default value was specified. @var{rest-arg} remains | |
432 | bound to whatever may have been left of @var{rest-arg}. | |
07d83abe | 433 | |
f916cbc4 AW |
434 | After binding the variables, the expressions @var{expr} @dots{} are |
435 | evaluated in order. | |
436 | @end deffn | |
07d83abe | 437 | |
f916cbc4 AW |
438 | Similarly, @code{let-keywords} and @code{let-keywords*} extract values |
439 | from keyword style argument lists, binding local variables to those | |
440 | values or to defaults. | |
07d83abe | 441 | |
f916cbc4 AW |
442 | @deffn {library syntax} let-keywords args allow-other-keys? (binding @dots{}) body @dots{} |
443 | @deffnx {library syntax} let-keywords* args allow-other-keys? (binding @dots{}) body @dots{} | |
444 | @var{args} is evaluated and should give a list of the form | |
445 | @code{(#:keyword1 value1 #:keyword2 value2 @dots{})}. The | |
446 | @var{binding}s are variables and default expressions, with the | |
447 | variables to be set (by name) from the keyword values. The @var{body} | |
448 | forms are then evaluated and the last is the result. An example will | |
449 | make the syntax clearest, | |
450 | ||
451 | @example | |
452 | (define args '(#:xyzzy "hello" #:foo "world")) | |
07d83abe | 453 | |
f916cbc4 AW |
454 | (let-keywords args #t |
455 | ((foo "default for foo") | |
456 | (bar (string-append "default" "for" "bar"))) | |
457 | (display foo) | |
458 | (display ", ") | |
459 | (display bar)) | |
460 | @print{} world, defaultforbar | |
461 | @end example | |
07d83abe | 462 | |
f916cbc4 AW |
463 | The binding for @code{foo} comes from the @code{#:foo} keyword in |
464 | @code{args}. But the binding for @code{bar} is the default in the | |
465 | @code{let-keywords}, since there's no @code{#:bar} in the args. | |
466 | ||
467 | @var{allow-other-keys?} is evaluated and controls whether unknown | |
468 | keywords are allowed in the @var{args} list. When true other keys are | |
469 | ignored (such as @code{#:xyzzy} in the example), when @code{#f} an | |
470 | error is thrown for anything unknown. | |
471 | @end deffn | |
472 | ||
473 | @code{(ice-9 optargs)} also provides some more @code{define*} sugar, | |
474 | which is not so useful with modern Guile coding, but still supported: | |
475 | @code{define*-public} is the @code{lambda*} version of | |
476 | @code{define-public}; @code{defmacro*} and @code{defmacro*-public} | |
477 | exist for defining macros with the improved argument list handling | |
478 | possibilities. The @code{-public} versions not only define the | |
479 | procedures/macros, but also export them from the current module. | |
480 | ||
481 | @deffn {library syntax} define*-public formals body | |
482 | Like a mix of @code{define*} and @code{define-public}. | |
07d83abe MV |
483 | @end deffn |
484 | ||
485 | @deffn {library syntax} defmacro* name formals body | |
486 | @deffnx {library syntax} defmacro*-public name formals body | |
487 | These are just like @code{defmacro} and @code{defmacro-public} except that they | |
488 | take @code{lambda*}-style extended parameter lists, where @code{#:optional}, | |
489 | @code{#:key}, @code{#:allow-other-keys} and @code{#:rest} are allowed with the usual | |
490 | semantics. Here is an example of a macro with an optional argument: | |
491 | ||
492 | @lisp | |
ecb87335 | 493 | (defmacro* transmogrify (a #:optional b) |
f916cbc4 | 494 | (a 1)) |
07d83abe MV |
495 | @end lisp |
496 | @end deffn | |
497 | ||
f916cbc4 AW |
498 | @node Case-lambda |
499 | @subsection Case-lambda | |
500 | @cindex SRFI-16 | |
501 | @cindex variable arity | |
502 | @cindex arity, variable | |
503 | ||
504 | R5RS's rest arguments are indeed useful and very general, but they | |
505 | often aren't the most appropriate or efficient means to get the job | |
506 | done. For example, @code{lambda*} is a much better solution to the | |
507 | optional argument problem than @code{lambda} with rest arguments. | |
508 | ||
509 | @fnindex case-lambda | |
510 | Likewise, @code{case-lambda} works well for when you want one | |
511 | procedure to do double duty (or triple, or ...), without the penalty | |
512 | of consing a rest list. | |
513 | ||
514 | For example: | |
515 | ||
516 | @lisp | |
517 | (define (make-accum n) | |
518 | (case-lambda | |
519 | (() n) | |
520 | ((m) (set! n (+ n m)) n))) | |
521 | ||
522 | (define a (make-accum 20)) | |
523 | (a) @result{} 20 | |
524 | (a 10) @result{} 30 | |
525 | (a) @result{} 30 | |
526 | @end lisp | |
527 | ||
528 | The value returned by a @code{case-lambda} form is a procedure which | |
529 | matches the number of actual arguments against the formals in the | |
530 | various clauses, in order. The first matching clause is selected, the | |
531 | corresponding values from the actual parameter list are bound to the | |
532 | variable names in the clauses and the body of the clause is evaluated. | |
533 | If no clause matches, an error is signalled. | |
534 | ||
535 | The syntax of the @code{case-lambda} form is defined in the following | |
536 | EBNF grammar. @dfn{Formals} means a formal argument list just like | |
537 | with @code{lambda} (@pxref{Lambda}). | |
538 | ||
539 | @example | |
540 | @group | |
541 | <case-lambda> | |
542 | --> (case-lambda <case-lambda-clause>) | |
543 | <case-lambda-clause> | |
544 | --> (<formals> <definition-or-command>*) | |
545 | <formals> | |
546 | --> (<identifier>*) | |
547 | | (<identifier>* . <identifier>) | |
548 | | <identifier> | |
549 | @end group | |
550 | @end example | |
551 | ||
552 | Rest lists can be useful with @code{case-lambda}: | |
553 | ||
554 | @lisp | |
555 | (define plus | |
556 | (case-lambda | |
557 | (() 0) | |
558 | ((a) a) | |
559 | ((a b) (+ a b)) | |
560 | ((a b . rest) (apply plus (+ a b) rest)))) | |
561 | (plus 1 2 3) @result{} 6 | |
562 | @end lisp | |
563 | ||
564 | @fnindex case-lambda* | |
565 | Also, for completeness. Guile defines @code{case-lambda*} as well, | |
566 | which is like @code{case-lambda}, except with @code{lambda*} clauses. | |
567 | A @code{case-lambda*} clause matches if the arguments fill the | |
568 | required arguments, but are not too many for the optional and/or rest | |
569 | arguments. | |
570 | ||
f916cbc4 AW |
571 | Keyword arguments are possible with @code{case-lambda*}, but they do |
572 | not contribute to the ``matching'' behavior. That is to say, | |
573 | @code{case-lambda*} matches only on required, optional, and rest | |
574 | arguments, and on the predicate; keyword arguments may be present but | |
575 | do not contribute to the ``success'' of a match. In fact a bad keyword | |
576 | argument list may cause an error to be raised. | |
07d83abe | 577 | |
18f06db9 LC |
578 | @node Higher-Order Functions |
579 | @subsection Higher-Order Functions | |
580 | ||
581 | @cindex higher-order functions | |
582 | ||
583 | As a functional programming language, Scheme allows the definition of | |
584 | @dfn{higher-order functions}, i.e., functions that take functions as | |
585 | arguments and/or return functions. Utilities to derive procedures from | |
586 | other procedures are provided and described below. | |
587 | ||
588 | @deffn {Scheme Procedure} const value | |
589 | Return a procedure that accepts any number of arguments and returns | |
590 | @var{value}. | |
591 | ||
592 | @lisp | |
593 | (procedure? (const 3)) @result{} #t | |
594 | ((const 'hello)) @result{} hello | |
595 | ((const 'hello) 'world) @result{} hello | |
596 | @end lisp | |
597 | @end deffn | |
598 | ||
599 | @deffn {Scheme Procedure} negate proc | |
600 | Return a procedure with the same arity as @var{proc} that returns the | |
601 | @code{not} of @var{proc}'s result. | |
602 | ||
603 | @lisp | |
604 | (procedure? (negate number?)) @result{} #t | |
605 | ((negate odd?) 2) @result{} #t | |
606 | ((negate real?) 'dream) @result{} #t | |
607 | ((negate string-prefix?) "GNU" "GNU Guile") | |
608 | @result{} #f | |
609 | (filter (negate number?) '(a 2 "b")) | |
610 | @result{} (a "b") | |
611 | @end lisp | |
612 | @end deffn | |
613 | ||
614 | @deffn {Scheme Procedure} compose proc rest ... | |
615 | Compose @var{proc} with the procedures in @var{rest}, such that the last | |
616 | one in @var{rest} is applied first and @var{proc} last, and return the | |
617 | resulting procedure. The given procedures must have compatible arity. | |
618 | ||
619 | @lisp | |
620 | (procedure? (compose 1+ 1-)) @result{} #t | |
621 | ((compose sqrt 1+ 1+) 2) @result{} 2.0 | |
622 | ((compose 1+ sqrt) 3) @result{} 2.73205080756888 | |
623 | (eq? (compose 1+) 1+) @result{} #t | |
624 | ||
625 | ((compose zip unzip2) '((1 2) (a b))) | |
626 | @result{} ((1 2) (a b)) | |
627 | @end lisp | |
628 | @end deffn | |
629 | ||
630 | @deffn {Scheme Procedure} identity x | |
631 | Return X. | |
632 | @end deffn | |
633 | ||
07d83abe MV |
634 | @node Procedure Properties |
635 | @subsection Procedure Properties and Meta-information | |
636 | ||
f916cbc4 AW |
637 | In addition to the information that is strictly necessary to run, |
638 | procedures may have other associated information. For example, the | |
639 | name of a procedure is information not for the procedure, but about | |
640 | the procedure. This meta-information can be accessed via the procedure | |
641 | properties interface. | |
07d83abe | 642 | |
f916cbc4 AW |
643 | The first group of procedures in this meta-interface are predicates to |
644 | test whether a Scheme object is a procedure, or a special procedure, | |
645 | respectively. @code{procedure?} is the most general predicates, it | |
646 | returns @code{#t} for any kind of procedure. @code{closure?} does not | |
647 | return @code{#t} for primitive procedures, and @code{thunk?} only | |
648 | returns @code{#t} for procedures which do not accept any arguments. | |
07d83abe MV |
649 | |
650 | @rnindex procedure? | |
651 | @deffn {Scheme Procedure} procedure? obj | |
652 | @deffnx {C Function} scm_procedure_p (obj) | |
653 | Return @code{#t} if @var{obj} is a procedure. | |
654 | @end deffn | |
655 | ||
07d83abe MV |
656 | @deffn {Scheme Procedure} thunk? obj |
657 | @deffnx {C Function} scm_thunk_p (obj) | |
658 | Return @code{#t} if @var{obj} is a thunk. | |
659 | @end deffn | |
660 | ||
07d83abe | 661 | @cindex procedure properties |
f916cbc4 AW |
662 | Procedure properties are general properties associated with |
663 | procedures. These can be the name of a procedure or other relevant | |
07d83abe MV |
664 | information, such as debug hints. |
665 | ||
666 | @deffn {Scheme Procedure} procedure-name proc | |
667 | @deffnx {C Function} scm_procedure_name (proc) | |
668 | Return the name of the procedure @var{proc} | |
669 | @end deffn | |
670 | ||
671 | @deffn {Scheme Procedure} procedure-source proc | |
672 | @deffnx {C Function} scm_procedure_source (proc) | |
f916cbc4 AW |
673 | Return the source of the procedure @var{proc}. Returns @code{#f} if |
674 | the source code is not available. | |
07d83abe MV |
675 | @end deffn |
676 | ||
677 | @deffn {Scheme Procedure} procedure-environment proc | |
678 | @deffnx {C Function} scm_procedure_environment (proc) | |
f916cbc4 | 679 | Return the environment of the procedure @var{proc}. Very deprecated. |
07d83abe MV |
680 | @end deffn |
681 | ||
682 | @deffn {Scheme Procedure} procedure-properties proc | |
683 | @deffnx {C Function} scm_procedure_properties (proc) | |
f916cbc4 AW |
684 | Return the properties associated with @var{proc}, as an association |
685 | list. | |
07d83abe MV |
686 | @end deffn |
687 | ||
f916cbc4 AW |
688 | @deffn {Scheme Procedure} procedure-property proc key |
689 | @deffnx {C Function} scm_procedure_property (proc, key) | |
690 | Return the property of @var{proc} with name @var{key}. | |
07d83abe MV |
691 | @end deffn |
692 | ||
693 | @deffn {Scheme Procedure} set-procedure-properties! proc alist | |
694 | @deffnx {C Function} scm_set_procedure_properties_x (proc, alist) | |
f916cbc4 | 695 | Set @var{proc}'s property list to @var{alist}. |
07d83abe MV |
696 | @end deffn |
697 | ||
f916cbc4 AW |
698 | @deffn {Scheme Procedure} set-procedure-property! proc key value |
699 | @deffnx {C Function} scm_set_procedure_property_x (proc, key, value) | |
700 | In @var{proc}'s property list, set the property named @var{key} to | |
07d83abe MV |
701 | @var{value}. |
702 | @end deffn | |
703 | ||
704 | @cindex procedure documentation | |
705 | Documentation for a procedure can be accessed with the procedure | |
706 | @code{procedure-documentation}. | |
707 | ||
708 | @deffn {Scheme Procedure} procedure-documentation proc | |
709 | @deffnx {C Function} scm_procedure_documentation (proc) | |
710 | Return the documentation string associated with @code{proc}. By | |
711 | convention, if a procedure contains more than one expression and the | |
712 | first expression is a string constant, that string is assumed to contain | |
713 | documentation for that procedure. | |
714 | @end deffn | |
715 | ||
07d83abe MV |
716 | |
717 | @node Procedures with Setters | |
718 | @subsection Procedures with Setters | |
719 | ||
720 | @c FIXME::martin: Review me! | |
721 | ||
722 | @c FIXME::martin: Document `operator struct'. | |
723 | ||
724 | @cindex procedure with setter | |
725 | @cindex setter | |
726 | A @dfn{procedure with setter} is a special kind of procedure which | |
727 | normally behaves like any accessor procedure, that is a procedure which | |
728 | accesses a data structure. The difference is that this kind of | |
729 | procedure has a so-called @dfn{setter} attached, which is a procedure | |
730 | for storing something into a data structure. | |
731 | ||
732 | Procedures with setters are treated specially when the procedure appears | |
733 | in the special form @code{set!} (REFFIXME). How it works is best shown | |
734 | by example. | |
735 | ||
736 | Suppose we have a procedure called @code{foo-ref}, which accepts two | |
737 | arguments, a value of type @code{foo} and an integer. The procedure | |
738 | returns the value stored at the given index in the @code{foo} object. | |
739 | Let @code{f} be a variable containing such a @code{foo} data | |
740 | structure.@footnote{Working definitions would be: | |
741 | @lisp | |
742 | (define foo-ref vector-ref) | |
743 | (define foo-set! vector-set!) | |
744 | (define f (make-vector 2 #f)) | |
745 | @end lisp | |
746 | } | |
747 | ||
748 | @lisp | |
749 | (foo-ref f 0) @result{} bar | |
750 | (foo-ref f 1) @result{} braz | |
751 | @end lisp | |
752 | ||
753 | Also suppose that a corresponding setter procedure called | |
754 | @code{foo-set!} does exist. | |
755 | ||
756 | @lisp | |
757 | (foo-set! f 0 'bla) | |
758 | (foo-ref f 0) @result{} bla | |
759 | @end lisp | |
760 | ||
761 | Now we could create a new procedure called @code{foo}, which is a | |
762 | procedure with setter, by calling @code{make-procedure-with-setter} with | |
763 | the accessor and setter procedures @code{foo-ref} and @code{foo-set!}. | |
764 | Let us call this new procedure @code{foo}. | |
765 | ||
766 | @lisp | |
767 | (define foo (make-procedure-with-setter foo-ref foo-set!)) | |
768 | @end lisp | |
769 | ||
770 | @code{foo} can from now an be used to either read from the data | |
771 | structure stored in @code{f}, or to write into the structure. | |
772 | ||
773 | @lisp | |
774 | (set! (foo f 0) 'dum) | |
775 | (foo f 0) @result{} dum | |
776 | @end lisp | |
777 | ||
778 | @deffn {Scheme Procedure} make-procedure-with-setter procedure setter | |
779 | @deffnx {C Function} scm_make_procedure_with_setter (procedure, setter) | |
780 | Create a new procedure which behaves like @var{procedure}, but | |
781 | with the associated setter @var{setter}. | |
782 | @end deffn | |
783 | ||
784 | @deffn {Scheme Procedure} procedure-with-setter? obj | |
785 | @deffnx {C Function} scm_procedure_with_setter_p (obj) | |
786 | Return @code{#t} if @var{obj} is a procedure with an | |
787 | associated setter procedure. | |
788 | @end deffn | |
789 | ||
790 | @deffn {Scheme Procedure} procedure proc | |
791 | @deffnx {C Function} scm_procedure (proc) | |
3323ec06 NJ |
792 | Return the procedure of @var{proc}, which must be an |
793 | applicable struct. | |
07d83abe MV |
794 | @end deffn |
795 | ||
796 | @deffn {Scheme Procedure} setter proc | |
797 | Return the setter of @var{proc}, which must be either a procedure with | |
798 | setter or an operator struct. | |
799 | @end deffn | |
800 | ||
165b10dd AR |
801 | @node Inlinable Procedures |
802 | @subsection Inlinable Procedures | |
803 | ||
804 | You can define an @dfn{inlinable procedure} by using | |
805 | @code{define-inlinable} instead of @code{define}. An inlinable | |
806 | procedure behaves the same as a regular procedure, but direct calls will | |
807 | result in the procedure body being inlined into the caller. | |
808 | ||
809 | Procedures defined with @code{define-inlinable} are @emph{always} | |
810 | inlined, at all direct call sites. This eliminates function call | |
811 | overhead at the expense of an increase in code size. Additionally, the | |
812 | caller will not transparently use the new definition if the inline | |
813 | procedure is redefined. It is not possible to trace an inlined | |
814 | procedures or install a breakpoint in it (@pxref{Traps}). For these | |
815 | reasons, you should not make a procedure inlinable unless it | |
816 | demonstrably improves performance in a crucial way. | |
817 | ||
818 | In general, only small procedures should be considered for inlining, as | |
819 | making large procedures inlinable will probably result in an increase in | |
820 | code size. Additionally, the elimination of the call overhead rarely | |
821 | matters for for large procedures. | |
822 | ||
823 | @deffn {Scheme Syntax} define-inlinable (name parameter ...) body ... | |
824 | Define @var{name} as a procedure with parameters @var{parameter}s and | |
825 | body @var{body}. | |
826 | @end deffn | |
07d83abe | 827 | |
07d83abe MV |
828 | @c Local Variables: |
829 | @c TeX-master: "guile.texi" | |
830 | @c End: |