2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
11 * Lambda:: Basic procedure creation using lambda.
12 * Primitive Procedures:: Procedures defined in C.
13 * Compiled Procedures:: Scheme procedures can be compiled.
14 * Optional Arguments:: Handling keyword, optional and rest arguments.
15 * Case-lambda:: One function, multiple arities.
16 * Procedure Properties:: Procedure properties and meta-information.
17 * Procedures with Setters:: Procedures with setters.
22 @subsection Lambda: Basic Procedure Creation
25 A @code{lambda} expression evaluates to a procedure. The environment
26 which is in effect when a @code{lambda} expression is evaluated is
27 enclosed in the newly created procedure, this is referred to as a
28 @dfn{closure} (@pxref{About Closure}).
30 When a procedure created by @code{lambda} is called with some actual
31 arguments, the environment enclosed in the procedure is extended by
32 binding the variables named in the formal argument list to new locations
33 and storing the actual arguments into these locations. Then the body of
34 the @code{lambda} expression is evaluated sequentially. The result of
35 the last expression in the procedure body is then the result of the
38 The following examples will show how procedures can be created using
39 @code{lambda}, and what you can do with these procedures.
42 (lambda (x) (+ x x)) @result{} @r{a procedure}
43 ((lambda (x) (+ x x)) 4) @result{} 8
46 The fact that the environment in effect when creating a procedure is
47 enclosed in the procedure is shown with this example:
52 (lambda (y) (+ x y))))
57 @deffn syntax lambda formals body
58 @var{formals} should be a formal argument list as described in the
62 @item (@var{variable1} @dots{})
63 The procedure takes a fixed number of arguments; when the procedure is
64 called, the arguments will be stored into the newly created location for
67 The procedure takes any number of arguments; when the procedure is
68 called, the sequence of actual arguments will converted into a list and
69 stored into the newly created location for the formal variable.
70 @item (@var{variable1} @dots{} @var{variablen} . @var{variablen+1})
71 If a space-delimited period precedes the last variable, then the
72 procedure takes @var{n} or more variables where @var{n} is the number
73 of formal arguments before the period. There must be at least one
74 argument before the period. The first @var{n} actual arguments will be
75 stored into the newly allocated locations for the first @var{n} formal
76 arguments and the sequence of the remaining actual arguments is
77 converted into a list and the stored into the location for the last
78 formal argument. If there are exactly @var{n} actual arguments, the
79 empty list is stored into the location of the last formal argument.
82 The list in @var{variable} or @var{variablen+1} is always newly
83 created and the procedure can modify it if desired. This is the case
84 even when the procedure is invoked via @code{apply}, the required part
85 of the list argument there will be copied (@pxref{Fly Evaluation,,
86 Procedures for On the Fly Evaluation}).
88 @var{body} is a sequence of Scheme expressions which are evaluated in
89 order when the procedure is invoked.
92 @node Primitive Procedures
93 @subsection Primitive Procedures
95 @cindex primitive procedures
97 Procedures written in C can be registered for use from Scheme,
98 provided they take only arguments of type @code{SCM} and return
99 @code{SCM} values. @code{scm_c_define_gsubr} is likely to be the most
100 useful mechanism, combining the process of registration
101 (@code{scm_c_make_gsubr}) and definition (@code{scm_define}).
103 @deftypefun SCM scm_c_make_gsubr (const char *name, int req, int opt, int rst, fcn)
104 Register a C procedure @var{FCN} as a ``subr'' --- a primitive
105 subroutine that can be called from Scheme. It will be associated with
106 the given @var{name} but no environment binding will be created. The
107 arguments @var{req}, @var{opt} and @var{rst} specify the number of
108 required, optional and ``rest'' arguments respectively. The total
109 number of these arguments should match the actual number of arguments
110 to @var{fcn}, but may not exceed 10. The number of rest arguments should be 0 or 1.
111 @code{scm_c_make_gsubr} returns a value of type @code{SCM} which is a
112 ``handle'' for the procedure.
115 @deftypefun SCM scm_c_define_gsubr (const char *name, int req, int opt, int rst, fcn)
116 Register a C procedure @var{FCN}, as for @code{scm_c_make_gsubr}
117 above, and additionally create a top-level Scheme binding for the
118 procedure in the ``current environment'' using @code{scm_define}.
119 @code{scm_c_define_gsubr} returns a handle for the procedure in the
120 same way as @code{scm_c_make_gsubr}, which is usually not further
124 @code{scm_c_make_gsubr} and @code{scm_c_define_gsubr} automatically
125 use @code{scm_c_make_subr} and also @code{scm_makcclo} if necessary.
126 It is advisable to use the gsubr variants since they provide a
127 slightly higher-level abstraction of the Guile implementation.
129 @node Compiled Procedures
130 @subsection Compiled Procedures
132 In Guile, procedures can be executed by directly interpreting their
133 source code. Scheme source code is a set of nested lists, after all,
134 with each list representing a procedure call.
136 Most procedures are compiled, however. This means that Guile has done
137 some pre-computation on the procedure, to determine what it will need
138 to do each time the procedure runs. Compiled procedures run faster
139 than interpreted procedures.
141 Loading files is the normal way that compiled procedures come to
142 being. If Guile sees that a file is uncompiled, or that its compiled
143 file is out of date, it will attempt to compile the file when it is
144 loaded, and save the result to disk. Procedures can be compiled at
145 runtime as well. @xref{Read/Load/Eval/Compile}, for more information
146 on runtime compilation.
148 Compiled procedures, also known as @dfn{programs}, respond all
149 procedures that operate on procedures. In addition, there are a few
150 more accessors for low-level details on programs.
152 Most people won't need to use the routines described in this section,
153 but it's good to have them documented. You'll have to include the
154 appropriate module first, though:
157 (use-modules (system vm program))
160 @deffn {Scheme Procedure} program? obj
161 @deffnx {C Function} scm_program_p (obj)
162 Returns @code{#t} iff @var{obj} is a compiled procedure.
165 @deffn {Scheme Procedure} program-objcode program
166 @deffnx {C Function} scm_program_objcode (program)
167 Returns the object code associated with this program. @xref{Bytecode
168 and Objcode}, for more information.
171 @deffn {Scheme Procedure} program-objects program
172 @deffnx {C Function} scm_program_objects (program)
173 Returns the ``object table'' associated with this program, as a
174 vector. @xref{VM Programs}, for more information.
177 @deffn {Scheme Procedure} program-module program
178 @deffnx {C Function} scm_program_module (program)
179 Returns the module that was current when this program was created. Can
180 return @code{#f} if the compiler could determine that this information
184 @deffn {Scheme Procedure} program-free-variables program
185 @deffnx {C Function} scm_program_free_variables (program)
186 Returns the set of free variables that this program captures in its
187 closure, as a vector. If a closure is code with data, you can get the
188 code from @code{program-objcode}, and the data via
189 @code{program-free-variables}.
191 Some of the values captured are actually in variable ``boxes''.
192 @xref{Variables and the VM}, for more information.
194 Users must not modify the returned value unless they think they're
198 @deffn {Scheme Procedure} program-meta program
199 @deffnx {C Function} scm_program_meta (program)
200 Return the metadata thunk of @var{program}, or @code{#f} if it has no
203 When called, a metadata thunk returns a list of the following form:
204 @code{(@var{bindings} @var{sources} @var{arities} . @var{properties})}. The format
205 of each of these elements is discussed below.
208 @deffn {Scheme Procedure} program-bindings program
209 @deffnx {Scheme Procedure} make-binding name boxed? index start end
210 @deffnx {Scheme Procedure} binding:name binding
211 @deffnx {Scheme Procedure} binding:boxed? binding
212 @deffnx {Scheme Procedure} binding:index binding
213 @deffnx {Scheme Procedure} binding:start binding
214 @deffnx {Scheme Procedure} binding:end binding
215 Bindings annotations for programs, along with their accessors.
217 Bindings declare names and liveness extents for block-local variables.
218 The best way to see what these are is to play around with them at a
219 REPL. @xref{VM Concepts}, for more information.
221 Note that bindings information is stored in a program as part of its
222 metadata thunk, so including it in the generated object code does not
223 impose a runtime performance penalty.
226 @deffn {Scheme Procedure} program-sources program
227 @deffnx {Scheme Procedure} source:addr source
228 @deffnx {Scheme Procedure} source:line source
229 @deffnx {Scheme Procedure} source:column source
230 @deffnx {Scheme Procedure} source:file source
231 Source location annotations for programs, along with their accessors.
233 Source location information propagates through the compiler and ends
234 up being serialized to the program's metadata. This information is
235 keyed by the offset of the instruction pointer within the object code
236 of the program. Specifically, it is keyed on the @code{ip} @emph{just
237 following} an instruction, so that backtraces can find the source
238 location of a call that is in progress.
241 @deffn {Scheme Procedure} program-arities program
242 @deffnx {C Function} scm_program_arities (program)
243 @deffnx {Scheme Procedure} program-arity program ip
244 @deffnx {Scheme Procedure} arity:start arity
245 @deffnx {Scheme Procedure} arity:end arity
246 @deffnx {Scheme Procedure} arity:nreq arity
247 @deffnx {Scheme Procedure} arity:nopt arity
248 @deffnx {Scheme Procedure} arity:rest? arity
249 @deffnx {Scheme Procedure} arity:kw arity
250 @deffnx {Scheme Procedure} arity:allow-other-keys? arity
251 Accessors for a representation of the ``arity'' of a program.
253 The normal case is that a procedure has one arity. For example,
254 @code{(lambda (x) x)}, takes one required argument, and that's it. One
255 could access that number of required arguments via @code{(arity:nreq
256 (program-arities (lambda (x) x)))}. Similarly, @code{arity:nopt} gets
257 the number of optional arguments, and @code{arity:rest?} returns a true
258 value if the procedure has a rest arg.
260 @code{arity:kw} returns a list of @code{(@var{kw} . @var{idx})} pairs,
261 if the procedure has keyword arguments. The @var{idx} refers to the
262 @var{idx}th local variable; @xref{Variables and the VM}, for more
263 information. Finally @code{arity:allow-other-keys?} returns a true
264 value if other keys are allowed. @xref{Optional Arguments}, for more
267 So what about @code{arity:start} and @code{arity:end}, then? They
268 return the range of bytes in the program's bytecode for which a given
269 arity is valid. You see, a procedure can actually have more than one
270 arity. The question, ``what is a procedure's arity'' only really makes
271 sense at certain points in the program, delimited by these
272 @code{arity:start} and @code{arity:end} values.
276 @node Optional Arguments
277 @subsection Optional Arguments
279 Scheme procedures, as defined in R5RS, can either handle a fixed number
280 of actual arguments, or a fixed number of actual arguments followed by
281 arbitrarily many additional arguments. Writing procedures of variable
282 arity can be useful, but unfortunately, the syntactic means for handling
283 argument lists of varying length is a bit inconvenient. It is possible
284 to give names to the fixed number of arguments, but the remaining
285 (optional) arguments can be only referenced as a list of values
288 For this reason, Guile provides an extension to @code{lambda},
289 @code{lambda*}, which allows the user to define procedures with
290 optional and keyword arguments. In addition, Guile's virtual machine
291 has low-level support for optional and keyword argument dispatch.
292 Calls to procedures with optional and keyword arguments can be made
293 cheaply, without allocating a rest list.
296 * lambda* and define*:: Creating advanced argument handling procedures.
297 * ice-9 optargs:: (ice-9 optargs) provides some utilities.
301 @node lambda* and define*
302 @subsubsection lambda* and define*.
304 @code{lambda*} is like @code{lambda}, except with some extensions to
305 allow optional and keyword arguments.
307 @deffn {library syntax} lambda* ([var@dots{}] @* [#:optional vardef@dots{}] @* [#:key vardef@dots{} [#:allow-other-keys]] @* [#:rest var | . var]) @* body
309 Create a procedure which takes optional and/or keyword arguments
310 specified with @code{#:optional} and @code{#:key}. For example,
313 (lambda* (a b #:optional c d . e) '())
316 is a procedure with fixed arguments @var{a} and @var{b}, optional
317 arguments @var{c} and @var{d}, and rest argument @var{e}. If the
318 optional arguments are omitted in a call, the variables for them are
322 Likewise, @code{define*} is syntactic sugar for defining procedures
323 using @code{lambda*}.
325 @code{lambda*} can also make procedures with keyword arguments. For
326 example, a procedure defined like this:
329 (define* (sir-yes-sir #:key action how-high)
330 (list action how-high))
333 can be called as @code{(sir-yes-sir #:action 'jump)},
334 @code{(sir-yes-sir #:how-high 13)}, @code{(sir-yes-sir #:action
335 'lay-down #:how-high 0)}, or just @code{(sir-yes-sir)}. Whichever
336 arguments are given as keywords are bound to values (and those not
337 given are @code{#f}).
339 Optional and keyword arguments can also have default values to take
340 when not present in a call, by giving a two-element list of variable
341 name and expression. For example in
344 (define* (frob foo #:optional (bar 42) #:key (baz 73))
348 @var{foo} is a fixed argument, @var{bar} is an optional argument with
349 default value 42, and baz is a keyword argument with default value 73.
350 Default value expressions are not evaluated unless they are needed,
351 and until the procedure is called.
353 Normally it's an error if a call has keywords other than those
354 specified by @code{#:key}, but adding @code{#:allow-other-keys} to the
355 definition (after the keyword argument declarations) will ignore
358 If a call has a keyword given twice, the last value is used. For
362 (define* (flips #:key (heads 0) (tails 0))
363 (display (list heads tails)))
365 (flips #:heads 37 #:tails 42 #:heads 99)
369 @code{#:rest} is a synonym for the dotted syntax rest argument. The
370 argument lists @code{(a . b)} and @code{(a #:rest b)} are equivalent
371 in all respects. This is provided for more similarity to DSSSL,
372 MIT-Scheme and Kawa among others, as well as for refugees from other
375 When @code{#:key} is used together with a rest argument, the keyword
376 parameters in a call all remain in the rest list. This is the same as
377 Common Lisp. For example,
380 ((lambda* (#:key (x 0) #:allow-other-keys #:rest r)
383 @print{} (#:x 123 #:y 456)
386 @code{#:optional} and @code{#:key} establish their bindings
387 successively, from left to right. This means default expressions can
388 refer back to prior parameters, for example
391 (lambda* (start #:optional (end (+ 10 start)))
392 (do ((i start (1+ i)))
397 The exception to this left-to-right scoping rule is the rest argument.
398 If there is a rest argument, it is bound after the optional arguments,
399 but before the keyword arguments.
404 @subsubsection (ice-9 optargs)
406 Before Guile 2.0, @code{lambda*} and @code{define*} were implemented
407 using macros that processed rest list arguments. This was not optimal,
408 as calling procedures with optional arguments had to allocate rest
409 lists at every procedure invocation. Guile 2.0 improved this
410 situation by bringing optional and keyword arguments into Guile's
413 However there are occasions in which you have a list and want to parse
414 it for optional or keyword arguments. Guile's @code{(ice-9 optargs)}
415 provides some macros to help with that task.
417 The syntax @code{let-optional} and @code{let-optional*} are for
418 destructuring rest argument lists and giving names to the various list
419 elements. @code{let-optional} binds all variables simultaneously, while
420 @code{let-optional*} binds them sequentially, consistent with @code{let}
421 and @code{let*} (@pxref{Local Bindings}).
423 @deffn {library syntax} let-optional rest-arg (binding @dots{}) expr @dots{}
424 @deffnx {library syntax} let-optional* rest-arg (binding @dots{}) expr @dots{}
425 These two macros give you an optional argument interface that is very
426 @dfn{Schemey} and introduces no fancy syntax. They are compatible with
427 the scsh macros of the same name, but are slightly extended. Each of
428 @var{binding} may be of one of the forms @var{var} or @code{(@var{var}
429 @var{default-value})}. @var{rest-arg} should be the rest-argument of the
430 procedures these are used from. The items in @var{rest-arg} are
431 sequentially bound to the variable names are given. When @var{rest-arg}
432 runs out, the remaining vars are bound either to the default values or
433 @code{#f} if no default value was specified. @var{rest-arg} remains
434 bound to whatever may have been left of @var{rest-arg}.
436 After binding the variables, the expressions @var{expr} @dots{} are
440 Similarly, @code{let-keywords} and @code{let-keywords*} extract values
441 from keyword style argument lists, binding local variables to those
442 values or to defaults.
444 @deffn {library syntax} let-keywords args allow-other-keys? (binding @dots{}) body @dots{}
445 @deffnx {library syntax} let-keywords* args allow-other-keys? (binding @dots{}) body @dots{}
446 @var{args} is evaluated and should give a list of the form
447 @code{(#:keyword1 value1 #:keyword2 value2 @dots{})}. The
448 @var{binding}s are variables and default expressions, with the
449 variables to be set (by name) from the keyword values. The @var{body}
450 forms are then evaluated and the last is the result. An example will
451 make the syntax clearest,
454 (define args '(#:xyzzy "hello" #:foo "world"))
456 (let-keywords args #t
457 ((foo "default for foo")
458 (bar (string-append "default" "for" "bar")))
462 @print{} world, defaultforbar
465 The binding for @code{foo} comes from the @code{#:foo} keyword in
466 @code{args}. But the binding for @code{bar} is the default in the
467 @code{let-keywords}, since there's no @code{#:bar} in the args.
469 @var{allow-other-keys?} is evaluated and controls whether unknown
470 keywords are allowed in the @var{args} list. When true other keys are
471 ignored (such as @code{#:xyzzy} in the example), when @code{#f} an
472 error is thrown for anything unknown.
475 @code{(ice-9 optargs)} also provides some more @code{define*} sugar,
476 which is not so useful with modern Guile coding, but still supported:
477 @code{define*-public} is the @code{lambda*} version of
478 @code{define-public}; @code{defmacro*} and @code{defmacro*-public}
479 exist for defining macros with the improved argument list handling
480 possibilities. The @code{-public} versions not only define the
481 procedures/macros, but also export them from the current module.
483 @deffn {library syntax} define*-public formals body
484 Like a mix of @code{define*} and @code{define-public}.
487 @deffn {library syntax} defmacro* name formals body
488 @deffnx {library syntax} defmacro*-public name formals body
489 These are just like @code{defmacro} and @code{defmacro-public} except that they
490 take @code{lambda*}-style extended parameter lists, where @code{#:optional},
491 @code{#:key}, @code{#:allow-other-keys} and @code{#:rest} are allowed with the usual
492 semantics. Here is an example of a macro with an optional argument:
495 (defmacro* transmorgify (a #:optional b)
501 @subsection Case-lambda
503 @cindex variable arity
504 @cindex arity, variable
506 R5RS's rest arguments are indeed useful and very general, but they
507 often aren't the most appropriate or efficient means to get the job
508 done. For example, @code{lambda*} is a much better solution to the
509 optional argument problem than @code{lambda} with rest arguments.
512 Likewise, @code{case-lambda} works well for when you want one
513 procedure to do double duty (or triple, or ...), without the penalty
514 of consing a rest list.
519 (define (make-accum n)
522 ((m) (set! n (+ n m)) n)))
524 (define a (make-accum 20))
530 The value returned by a @code{case-lambda} form is a procedure which
531 matches the number of actual arguments against the formals in the
532 various clauses, in order. The first matching clause is selected, the
533 corresponding values from the actual parameter list are bound to the
534 variable names in the clauses and the body of the clause is evaluated.
535 If no clause matches, an error is signalled.
537 The syntax of the @code{case-lambda} form is defined in the following
538 EBNF grammar. @dfn{Formals} means a formal argument list just like
539 with @code{lambda} (@pxref{Lambda}).
544 --> (case-lambda <case-lambda-clause>)
546 --> (<formals> <definition-or-command>*)
549 | (<identifier>* . <identifier>)
554 Rest lists can be useful with @code{case-lambda}:
562 ((a b . rest) (apply plus (+ a b) rest))))
563 (plus 1 2 3) @result{} 6
566 @fnindex case-lambda*
567 Also, for completeness. Guile defines @code{case-lambda*} as well,
568 which is like @code{case-lambda}, except with @code{lambda*} clauses.
569 A @code{case-lambda*} clause matches if the arguments fill the
570 required arguments, but are not too many for the optional and/or rest
573 Keyword arguments are possible with @code{case-lambda*}, but they do
574 not contribute to the ``matching'' behavior. That is to say,
575 @code{case-lambda*} matches only on required, optional, and rest
576 arguments, and on the predicate; keyword arguments may be present but
577 do not contribute to the ``success'' of a match. In fact a bad keyword
578 argument list may cause an error to be raised.
580 @node Procedure Properties
581 @subsection Procedure Properties and Meta-information
583 In addition to the information that is strictly necessary to run,
584 procedures may have other associated information. For example, the
585 name of a procedure is information not for the procedure, but about
586 the procedure. This meta-information can be accessed via the procedure
587 properties interface.
589 The first group of procedures in this meta-interface are predicates to
590 test whether a Scheme object is a procedure, or a special procedure,
591 respectively. @code{procedure?} is the most general predicates, it
592 returns @code{#t} for any kind of procedure. @code{closure?} does not
593 return @code{#t} for primitive procedures, and @code{thunk?} only
594 returns @code{#t} for procedures which do not accept any arguments.
597 @deffn {Scheme Procedure} procedure? obj
598 @deffnx {C Function} scm_procedure_p (obj)
599 Return @code{#t} if @var{obj} is a procedure.
602 @deffn {Scheme Procedure} closure? obj
603 @deffnx {C Function} scm_closure_p (obj)
604 Return @code{#t} if @var{obj} is a closure. This category somewhat
605 misnamed, actually, as it applies only to interpreted procedures, not
606 compiled procedures. But since it has historically been used more to
607 select on implementation details than on essence (closure or not), we
608 keep it here for compatibility. Don't use it in new code, though.
611 @deffn {Scheme Procedure} thunk? obj
612 @deffnx {C Function} scm_thunk_p (obj)
613 Return @code{#t} if @var{obj} is a thunk.
616 @cindex procedure properties
617 Procedure properties are general properties associated with
618 procedures. These can be the name of a procedure or other relevant
619 information, such as debug hints.
621 @deffn {Scheme Procedure} procedure-name proc
622 @deffnx {C Function} scm_procedure_name (proc)
623 Return the name of the procedure @var{proc}
626 @deffn {Scheme Procedure} procedure-source proc
627 @deffnx {C Function} scm_procedure_source (proc)
628 Return the source of the procedure @var{proc}. Returns @code{#f} if
629 the source code is not available.
632 @deffn {Scheme Procedure} procedure-environment proc
633 @deffnx {C Function} scm_procedure_environment (proc)
634 Return the environment of the procedure @var{proc}. Very deprecated.
637 @deffn {Scheme Procedure} procedure-properties proc
638 @deffnx {C Function} scm_procedure_properties (proc)
639 Return the properties associated with @var{proc}, as an association
643 @deffn {Scheme Procedure} procedure-property proc key
644 @deffnx {C Function} scm_procedure_property (proc, key)
645 Return the property of @var{proc} with name @var{key}.
648 @deffn {Scheme Procedure} set-procedure-properties! proc alist
649 @deffnx {C Function} scm_set_procedure_properties_x (proc, alist)
650 Set @var{proc}'s property list to @var{alist}.
653 @deffn {Scheme Procedure} set-procedure-property! proc key value
654 @deffnx {C Function} scm_set_procedure_property_x (proc, key, value)
655 In @var{proc}'s property list, set the property named @var{key} to
659 @cindex procedure documentation
660 Documentation for a procedure can be accessed with the procedure
661 @code{procedure-documentation}.
663 @deffn {Scheme Procedure} procedure-documentation proc
664 @deffnx {C Function} scm_procedure_documentation (proc)
665 Return the documentation string associated with @code{proc}. By
666 convention, if a procedure contains more than one expression and the
667 first expression is a string constant, that string is assumed to contain
668 documentation for that procedure.
672 @node Procedures with Setters
673 @subsection Procedures with Setters
675 @c FIXME::martin: Review me!
677 @c FIXME::martin: Document `operator struct'.
679 @cindex procedure with setter
681 A @dfn{procedure with setter} is a special kind of procedure which
682 normally behaves like any accessor procedure, that is a procedure which
683 accesses a data structure. The difference is that this kind of
684 procedure has a so-called @dfn{setter} attached, which is a procedure
685 for storing something into a data structure.
687 Procedures with setters are treated specially when the procedure appears
688 in the special form @code{set!} (REFFIXME). How it works is best shown
691 Suppose we have a procedure called @code{foo-ref}, which accepts two
692 arguments, a value of type @code{foo} and an integer. The procedure
693 returns the value stored at the given index in the @code{foo} object.
694 Let @code{f} be a variable containing such a @code{foo} data
695 structure.@footnote{Working definitions would be:
697 (define foo-ref vector-ref)
698 (define foo-set! vector-set!)
699 (define f (make-vector 2 #f))
704 (foo-ref f 0) @result{} bar
705 (foo-ref f 1) @result{} braz
708 Also suppose that a corresponding setter procedure called
709 @code{foo-set!} does exist.
713 (foo-ref f 0) @result{} bla
716 Now we could create a new procedure called @code{foo}, which is a
717 procedure with setter, by calling @code{make-procedure-with-setter} with
718 the accessor and setter procedures @code{foo-ref} and @code{foo-set!}.
719 Let us call this new procedure @code{foo}.
722 (define foo (make-procedure-with-setter foo-ref foo-set!))
725 @code{foo} can from now an be used to either read from the data
726 structure stored in @code{f}, or to write into the structure.
729 (set! (foo f 0) 'dum)
730 (foo f 0) @result{} dum
733 @deffn {Scheme Procedure} make-procedure-with-setter procedure setter
734 @deffnx {C Function} scm_make_procedure_with_setter (procedure, setter)
735 Create a new procedure which behaves like @var{procedure}, but
736 with the associated setter @var{setter}.
739 @deffn {Scheme Procedure} procedure-with-setter? obj
740 @deffnx {C Function} scm_procedure_with_setter_p (obj)
741 Return @code{#t} if @var{obj} is a procedure with an
742 associated setter procedure.
745 @deffn {Scheme Procedure} procedure proc
746 @deffnx {C Function} scm_procedure (proc)
747 Return the procedure of @var{proc}, which must be an
751 @deffn {Scheme Procedure} setter proc
752 Return the setter of @var{proc}, which must be either a procedure with
753 setter or an operator struct.
758 @c TeX-master: "guile.texi"