2 @node Procedures and Macros
3 @chapter Procedures and Macros
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.
15 @section Lambda: Basic Procedure Creation
17 @c FIXME::martin: Review me!
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}).
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
32 The following examples will show how procedures can be created using
33 @code{lambda}, and what you can do with these procedures.
36 (lambda (x) (+ x x)) @result{} @r{a procedure}
37 ((lambda (x) (+ x x)) 4) @result{} 8
40 The fact that the environment in effect when creating a procedure is
41 enclosed in the procedure is shown with this example:
46 (lambda (y) (+ x y))))
51 @deffn syntax lambda formals body
52 @var{formals} should be a formal argument list as described in the
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
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.
76 @var{body} is a sequence of Scheme expressions which are evaluated in
77 order when the procedure is invoked.
81 @node Optional Arguments
82 @section Optional Arguments
85 @node Procedure Properties
86 @section Procedure Properties and Metainformation
88 @c FIXME::martin: Review me!
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
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?
106 @deffn primitive procedure? obj
107 Return @code{#t} if @var{obj} is a procedure.
110 @deffn primitive closure? obj
111 Return @code{#t} if @var{obj} is a closure.
114 @deffn primitive thunk? obj
115 Return @code{#t} if @var{obj} is a thunk.
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.
124 @deffn primitive procedure-properties proc
125 Return @var{obj}'s property list.
128 @deffn primitive procedure-property p k
129 Return the property of @var{obj} with name @var{key}.
132 @deffn primitive set-procedure-properties! proc new_val
133 Set @var{obj}'s property list to @var{alist}.
136 @deffn primitive set-procedure-property! p k v
137 In @var{obj}'s property list, set the property named @var{key} to
141 @cindex procedure documentation
142 Documentation for a procedure can be accessed with the procedure
143 @code{procedure-documentation}.
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.
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.
157 @deffn primitive set-source-properties! obj plist
158 Install the association list @var{plist} as the source property
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.
167 @deffn primitive source-properties obj
168 Return the source property association list of @var{obj}.
172 @deffn primitive source-property obj key
173 Return the source property specified by @var{key} from
174 @var{obj}'s source property list.
178 @node Procedures with Setters
179 @section Procedures with Setters
181 @c FIXME::martin: Review me!
183 @c FIXME::martin: Document `operator struct'.
185 @cindex procedure with 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.
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
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:
203 (define foo-ref vector-ref)
204 (define foo-set! vector-set!)
205 (define f (make-vector 2 #f))
210 (foo-ref f 0) @result{} bar
211 (foo-ref f 1) @result{} braz
214 Also suppose that a corresponding setter procedure called
215 @code{foo-set!} does exist.
219 (foo-ref f 0) @result{} bla
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}.
228 (define foo (make-procedure-with-setter foo-ref foo-set!))
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.
235 (set! (foo f 0) 'dum)
236 (foo f 0) @result{} dum
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}.
244 @deffn primitive procedure-with-setter? obj
245 Return @code{#t} if @var{obj} is a procedure with an
246 associated setter procedure.
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.
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.
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]
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
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:
285 (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
287 (trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).
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:
301 (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
303 (trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).
307 @deffn primitive macro? obj
308 Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a
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
320 @deffn primitive macro-name m
321 Return the name of the macro @var{m}.
324 @deffn primitive macro-transformer m
325 Return the transformer of the macro @var{m}.
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
336 @c TeX-master: "guile.texi"