95f7d64aa5f727c6d25c4c8e783443ae53fe61b8
[bpt/guile.git] / doc / scheme-procedures.texi
1 @page
2 @node Procedures and Macros
3 @chapter Procedures and Macros
4
5 @menu
6 * Lambda:: Basic procedure creation using lambda.
7 * Optional Arguments:: Handling keyword, optional and rest arguments.
8 * Procedure Properties:: Procedure properties and metainformation.
9 * Procedures with Setters:: Procedures with setters.
10 * Macros:: Macros.
11 @end menu
12
13
14 @node Lambda
15 @section Lambda: Basic Procedure Creation
16
17 @c FIXME::martin: Review me!
18
19 A @code{lambda} expression evaluates to a procedure. The environment
20 which is in effect when a @code{lambda} expression is evaluated is
21 enclosed in the newly created procedure, this is referred to as a
22 @dfn{closure} (@pxref{About Closure}).
23
24 When a procedure created by @code{lambda} is called with some actual
25 arguments, the environment enclosed in the procedure is extended by
26 binding the variables named in the formal argument list to new locations
27 and storing the actual arguments into these locations. Then the body of
28 the @code{lambda} expression is evaluation sequentially. The result of
29 the last expression in the procedure body is then the result of the
30 procedure invocation.
31
32 The following examples will show how procedures can be created using
33 @code{lambda}, and what you can do with these procedures.
34
35 @lisp
36 (lambda (x) (+ x x)) @result{} @r{a procedure}
37 ((lambda (x) (+ x x)) 4) @result{} 8
38 @end lisp
39
40 The fact that the environment in effect when creating a procedure is
41 enclosed in the procedure is shown with this example:
42
43 @lisp
44 (define add4
45 (let ((x 4))
46 (lambda (y) (+ x y))))
47 (add4 6) @result{} 10
48 @end lisp
49
50
51 @deffn syntax lambda formals body
52 @var{formals} should be a formal argument list as described in the
53 following table.
54
55 @table @code
56 @item (@var{variable1} @dots{})
57 The procedure takes a fixed number of arguments; when the procedure is
58 called, the arguments will be stored into the newly created location for
59 the formal variables.
60 @item @var{variable}
61 The procedure takes any number of arguments; when the procedure is
62 called, the sequence of actual arguments will converted into a list and
63 stored into the newly created location for the formal variable.
64 @item (@var{variable1} @dots{} @var{variablen} . @var{variablen+1})
65 If a space--delimited period precedes the last variable, then the
66 procedure takes @var{n} or more variablesm where @var{n} is the number
67 of formal arguments before the period. There must be at least one
68 argument before the period. The first @var{n} actual arguments will be
69 stored into the newly allocated locations for the first @var{n} formal
70 arguments and the sequence of the remaining actual arguments is
71 converted into a list and the stored into the location for the last
72 formal argument. If there are exactly @var{n} actual arguments, the
73 empty list is stored into the location of the last formal argument.
74 @end table
75
76 @var{body} is a sequence of Scheme expressions which are evaluated in
77 order when the procedure is invoked.
78 @end deffn
79
80
81 @node Optional Arguments
82 @section Optional Arguments
83
84
85 @node Procedure Properties
86 @section Procedure Properties and Metainformation
87
88 @c FIXME::martin: Review me!
89
90 Procedures always have attached the environment in which they were
91 created and information about how to apply them to actual arguments. In
92 addition to that, properties and metainformation can be stored with
93 procedures. The procedures in this section can be used to test whether
94 a given procedure satisfies a condition; and to access and set a
95 procedure's property.
96
97 The first group of procedures are predicates to test whether a Scheme
98 object is a procedure, or a special procedure, respectively.
99 @code{procedure?} is the most general predicates, it returns @code{#t}
100 for any kind of procedure. @code{closure?} does not return @code{#t}
101 for primitive procedures, and @code{thunk?} only returns @code{#t} for
102 procedures which do not accept any arguments.
103 @c FIXME::martin: thunk? returns true for `id'. What's wrong here?
104
105 @rnindex procedure?
106 @deffn primitive procedure? obj
107 Return @code{#t} if @var{obj} is a procedure.
108 @end deffn
109
110 @deffn primitive closure? obj
111 Return @code{#t} if @var{obj} is a closure.
112 @end deffn
113
114 @deffn primitive thunk? obj
115 Return @code{#t} if @var{obj} is a thunk.
116 @end deffn
117
118 @c FIXME::martin: Is that true?
119 @cindex procedure properties
120 Procedure properties are general properties to be attached to
121 procedures. These can be the name of a procedure or other relevant
122 information, such as debug hints.
123
124 @deffn primitive procedure-properties proc
125 Return @var{obj}'s property list.
126 @end deffn
127
128 @deffn primitive procedure-property p k
129 Return the property of @var{obj} with name @var{key}.
130 @end deffn
131
132 @deffn primitive set-procedure-properties! proc new_val
133 Set @var{obj}'s property list to @var{alist}.
134 @end deffn
135
136 @deffn primitive set-procedure-property! p k v
137 In @var{obj}'s property list, set the property named @var{key} to
138 @var{value}.
139 @end deffn
140
141 @cindex procedure documentation
142 Documentation for a procedure can be accessed with the procedure
143 @code{procedure-documentation}.
144
145 @deffn primitive procedure-documentation proc
146 Return the documentation string associated with @code{proc}. By
147 convention, if a procedure contains more than one expression and the
148 first expression is a string constant, that string is assumed to contain
149 documentation for that procedure.
150 @end deffn
151
152 @cindex source properties
153 @c FIXME::martin: Is the following true?
154 Source properties are properties which are related to the source code of
155 a procedure, such as the line and column numbers, the file name etc.
156
157 @deffn primitive set-source-properties! obj plist
158 Install the association list @var{plist} as the source property
159 list for @var{obj}.
160 @end deffn
161
162 @deffn primitive set-source-property! obj key datum
163 Set the source property of object @var{obj}, which is specified by
164 @var{key} to @var{datum}. Normally, the key will be a symbol.
165 @end deffn
166
167 @deffn primitive source-properties obj
168 Return the source property association list of @var{obj}.
169 @end deffn
170
171
172 @deffn primitive source-property obj key
173 Return the source property specified by @var{key} from
174 @var{obj}'s source property list.
175 @end deffn
176
177
178 @node Procedures with Setters
179 @section Procedures with Setters
180
181 @c FIXME::martin: Review me!
182
183 @c FIXME::martin: Document `operator struct'.
184
185 @cindex procedure with setter
186 @cindex setter
187 A @dfn{procedure with setter} is a special kind of procedure which
188 normally behaves like any accesor procedure, that is a procedure which
189 accesses a data structure. The difference is that this kind of
190 procedure has a so--called @dfn{setter} attached, which is a procedure
191 for storing something into a data structure.
192
193 Procedures with setters are treated specially when the procedure appears
194 in the special form @code{set!} (REFFIXME). How it works is best shown
195 by example.
196
197 Suppose we have a procedure called @code{foo-ref}, which accepts two
198 arguments, a value of type @code{foo} and an integer. The procedure
199 returns the value stored at the given index in the @code{foo} object.
200 Let @code{f} be a variable containing such a @code{foo} data
201 structure.@footnote{Working definitions would be:
202 @lisp
203 (define foo-ref vector-ref)
204 (define foo-set! vector-set!)
205 (define f (make-vector 2 #f))
206 @end lisp
207 }
208
209 @lisp
210 (foo-ref f 0) @result{} bar
211 (foo-ref f 1) @result{} braz
212 @end lisp
213
214 Also suppose that a corresponding setter procedure called
215 @code{foo-set!} does exist.
216
217 @lisp
218 (foo-set! f 0 'bla)
219 (foo-ref f 0) @result{} bla
220 @end lisp
221
222 Now we could create a new procedure called @code{foo}, which is a
223 procedure with setter, by calling @code{make-procedure-with-setter} with
224 the accessor and setter procedures @code{foo-ref} and @code{foo-set!}.
225 Let us call this new procedure @code{foo}.
226
227 @lisp
228 (define foo (make-procedure-with-setter foo-ref foo-set!))
229 @end lisp
230
231 @code{foo} can from now an be used to either read from the data
232 structure stored in @code{f}, or to write into the structure.
233
234 @lisp
235 (set! (foo f 0) 'dum)
236 (foo f 0) @result{} dum
237 @end lisp
238
239 @deffn primitive make-procedure-with-setter procedure setter
240 Create a new procedure which behaves like @var{procedure}, but
241 with the associated setter @var{setter}.
242 @end deffn
243
244 @deffn primitive procedure-with-setter? obj
245 Return @code{#t} if @var{obj} is a procedure with an
246 associated setter procedure.
247 @end deffn
248
249 @deffn primitive procedure proc
250 Return the procedure of @var{proc}, which must be either a
251 procedure with setter, or an operator struct.
252 @end deffn
253
254 @deffn primitive setter proc
255 Return the setter of @var{proc}, which must be either a procedure with
256 setter or an operator struct.
257 @end deffn
258
259
260 @node Macros
261 @section Macros
262
263 [FIXME: This needs some more text on the difference between procedures,
264 macros and memoizing macros. Also, any definitions listed here should
265 be double-checked by someone who knows what's going on. Ask Mikael, Jim
266 or Aubrey for help. -twp]
267
268 @deffn primitive procedure->syntax code
269 Return a @dfn{macro} which, when a symbol defined to this value
270 appears as the first symbol in an expression, returns the
271 result of applying @var{code} to the expression and the
272 environment.
273 @end deffn
274
275 @deffn primitive procedure->macro code
276 Return a @dfn{macro} which, when a symbol defined to this value
277 appears as the first symbol in an expression, evaluates the
278 result of applying @var{code} to the expression and the
279 environment. The value returned from @var{code} which has been
280 passed to @code{procedure->memoizing-macro} replaces the form
281 passed to @var{code}. For example:
282 @lisp
283 (define trace
284 (procedure->macro
285 (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
286
287 (trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).
288 @end lisp
289 @end deffn
290
291 @deffn primitive procedure->memoizing-macro code
292 Return a @dfn{macro} which, when a symbol defined to this value
293 appears as the first symbol in an expression, evaluates the
294 result of applying @var{proc} to the expression and the
295 environment. The value returned from @var{proc} which has been
296 passed to @code{procedure->memoizing-macro} replaces the form
297 passed to @var{proc}. For example:
298 @lisp
299 (define trace
300 (procedure->macro
301 (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
302
303 (trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).
304 @end lisp
305 @end deffn
306
307 @deffn primitive macro? obj
308 Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a
309 syntax transformer.
310 @end deffn
311
312 @deffn primitive macro-type m
313 Return one of the symbols @code{syntax}, @code{macro} or
314 @code{macro!}, depending on whether @var{m} is a syntax
315 tranformer, a regular macro, or a memoizing macro,
316 respectively. If @var{m} is not a macro, @code{#f} is
317 returned.
318 @end deffn
319
320 @deffn primitive macro-name m
321 Return the name of the macro @var{m}.
322 @end deffn
323
324 @deffn primitive macro-transformer m
325 Return the transformer of the macro @var{m}.
326 @end deffn
327
328 @deffn primitive cons-source xorig x y
329 Create and return a new pair whose car and cdr are @var{x} and @var{y}.
330 Any source properties associated with @var{xorig} are also associated
331 with the new pair.
332 @end deffn
333
334
335 @c Local Variables:
336 @c TeX-master: "guile.texi"
337 @c End: