2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
8 @node Compound Data Types
9 @section Compound Data Types
11 This chapter describes Guile's compound data types. By @dfn{compound}
12 we mean that the primary purpose of these data types is to act as
13 containers for other kinds of data (including other compound objects).
14 For instance, a (non-uniform) vector with length 5 is a container that
15 can hold five arbitrary Scheme objects.
17 The various kinds of container object differ from each other in how
18 their memory is allocated, how they are indexed, and how particular
19 values can be looked up within them.
22 * Pairs:: Scheme's basic building block.
23 * Lists:: Special list functions supported by Guile.
24 * Vectors:: One-dimensional arrays of Scheme objects.
25 * Uniform Numeric Vectors:: Vectors with elements of a single numeric type.
26 * Bit Vectors:: Vectors of bits.
27 * Generalized Vectors:: Treating all vector-like things uniformly.
28 * Arrays:: Matrices, etc.
31 * Dictionary Types:: About dictionary types in general.
32 * Association Lists:: List-based dictionaries.
33 * Hash Tables:: Table-based dictionaries.
41 Pairs are used to combine two Scheme objects into one compound object.
42 Hence the name: A pair stores a pair of objects.
44 The data type @dfn{pair} is extremely important in Scheme, just like in
45 any other Lisp dialect. The reason is that pairs are not only used to
46 make two values available as one object, but that pairs are used for
47 constructing lists of values. Because lists are so important in Scheme,
48 they are described in a section of their own (@pxref{Lists}).
50 Pairs can literally get entered in source code or at the REPL, in the
51 so-called @dfn{dotted list} syntax. This syntax consists of an opening
52 parentheses, the first element of the pair, a dot, the second element
53 and a closing parentheses. The following example shows how a pair
54 consisting of the two numbers 1 and 2, and a pair containing the symbols
55 @code{foo} and @code{bar} can be entered. It is very important to write
56 the whitespace before and after the dot, because otherwise the Scheme
57 parser would not be able to figure out where to split the tokens.
64 But beware, if you want to try out these examples, you have to
65 @dfn{quote} the expressions. More information about quotation is
66 available in the section (REFFIXME). The correct way to try these
67 examples is as follows.
78 A new pair is made by calling the procedure @code{cons} with two
79 arguments. Then the argument values are stored into a newly allocated
80 pair, and the pair is returned. The name @code{cons} stands for
81 "construct". Use the procedure @code{pair?} to test whether a
82 given Scheme object is a pair or not.
85 @deffn {Scheme Procedure} cons x y
86 @deffnx {C Function} scm_cons (x, y)
87 Return a newly allocated pair whose car is @var{x} and whose
88 cdr is @var{y}. The pair is guaranteed to be different (in the
89 sense of @code{eq?}) from every previously existing object.
93 @deffn {Scheme Procedure} pair? x
94 @deffnx {C Function} scm_pair_p (x)
95 Return @code{#t} if @var{x} is a pair; otherwise return
99 @deftypefn {C Function} int scm_is_pair (SCM x)
100 Return 1 when @var{x} is a pair; otherwise return 0.
103 The two parts of a pair are traditionally called @dfn{car} and
104 @dfn{cdr}. They can be retrieved with procedures of the same name
105 (@code{car} and @code{cdr}), and can be modified with the procedures
106 @code{set-car!} and @code{set-cdr!}. Since a very common operation in
107 Scheme programs is to access the car of a pair, or the car of the cdr of
108 a pair, etc., the procedures called @code{caar}, @code{cadr} and so on
113 @deffn {Scheme Procedure} car pair
114 @deffnx {Scheme Procedure} cdr pair
115 @deffnx {C Function} scm_car (pair)
116 @deffnx {C Function} scm_cdr (pair)
117 Return the car or the cdr of @var{pair}, respectively.
120 @deffn {Scheme Procedure} cddr pair
121 @deffnx {Scheme Procedure} cdar pair
122 @deffnx {Scheme Procedure} cadr pair
123 @deffnx {Scheme Procedure} caar pair
124 @deffnx {Scheme Procedure} cdddr pair
125 @deffnx {Scheme Procedure} cddar pair
126 @deffnx {Scheme Procedure} cdadr pair
127 @deffnx {Scheme Procedure} cdaar pair
128 @deffnx {Scheme Procedure} caddr pair
129 @deffnx {Scheme Procedure} cadar pair
130 @deffnx {Scheme Procedure} caadr pair
131 @deffnx {Scheme Procedure} caaar pair
132 @deffnx {Scheme Procedure} cddddr pair
133 @deffnx {Scheme Procedure} cdddar pair
134 @deffnx {Scheme Procedure} cddadr pair
135 @deffnx {Scheme Procedure} cddaar pair
136 @deffnx {Scheme Procedure} cdaddr pair
137 @deffnx {Scheme Procedure} cdadar pair
138 @deffnx {Scheme Procedure} cdaadr pair
139 @deffnx {Scheme Procedure} cdaaar pair
140 @deffnx {Scheme Procedure} cadddr pair
141 @deffnx {Scheme Procedure} caddar pair
142 @deffnx {Scheme Procedure} cadadr pair
143 @deffnx {Scheme Procedure} cadaar pair
144 @deffnx {Scheme Procedure} caaddr pair
145 @deffnx {Scheme Procedure} caadar pair
146 @deffnx {Scheme Procedure} caaadr pair
147 @deffnx {Scheme Procedure} caaaar pair
148 @deffnx {C Function} scm_cddr (pair)
149 @deffnx {C Function} scm_cdar (pair)
150 @deffnx {C Function} scm_cadr (pair)
151 @deffnx {C Function} scm_caar (pair)
152 @deffnx {C Function} scm_cdddr (pair)
153 @deffnx {C Function} scm_cddar (pair)
154 @deffnx {C Function} scm_cdadr (pair)
155 @deffnx {C Function} scm_cdaar (pair)
156 @deffnx {C Function} scm_caddr (pair)
157 @deffnx {C Function} scm_cadar (pair)
158 @deffnx {C Function} scm_caadr (pair)
159 @deffnx {C Function} scm_caaar (pair)
160 @deffnx {C Function} scm_cddddr (pair)
161 @deffnx {C Function} scm_cdddar (pair)
162 @deffnx {C Function} scm_cddadr (pair)
163 @deffnx {C Function} scm_cddaar (pair)
164 @deffnx {C Function} scm_cdaddr (pair)
165 @deffnx {C Function} scm_cdadar (pair)
166 @deffnx {C Function} scm_cdaadr (pair)
167 @deffnx {C Function} scm_cdaaar (pair)
168 @deffnx {C Function} scm_cadddr (pair)
169 @deffnx {C Function} scm_caddar (pair)
170 @deffnx {C Function} scm_cadadr (pair)
171 @deffnx {C Function} scm_cadaar (pair)
172 @deffnx {C Function} scm_caaddr (pair)
173 @deffnx {C Function} scm_caadar (pair)
174 @deffnx {C Function} scm_caaadr (pair)
175 @deffnx {C Function} scm_caaaar (pair)
176 These procedures are compositions of @code{car} and @code{cdr}, where
177 for example @code{caddr} could be defined by
180 (define caddr (lambda (x) (car (cdr (cdr x)))))
185 @deffn {Scheme Procedure} set-car! pair value
186 @deffnx {C Function} scm_set_car_x (pair, value)
187 Stores @var{value} in the car field of @var{pair}. The value returned
188 by @code{set-car!} is unspecified.
192 @deffn {Scheme Procedure} set-cdr! pair value
193 @deffnx {C Function} scm_set_cdr_x (pair, value)
194 Stores @var{value} in the cdr field of @var{pair}. The value returned
195 by @code{set-cdr!} is unspecified.
203 A very important data type in Scheme---as well as in all other Lisp
204 dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
205 Scheme does not have a real datatype @dfn{list}. Lists are made up of
206 @dfn{chained pairs}, and only exist by definition---a list is a chain
207 of pairs which looks like a list.}
209 This is the short definition of what a list is:
213 Either the empty list @code{()},
216 or a pair which has a list in its cdr.
219 @c FIXME::martin: Describe the pair chaining in more detail.
221 @c FIXME::martin: What is a proper, what an improper list?
222 @c What is a circular list?
224 @c FIXME::martin: Maybe steal some graphics from the Elisp reference
228 * List Syntax:: Writing literal lists.
229 * List Predicates:: Testing lists.
230 * List Constructors:: Creating new lists.
231 * List Selection:: Selecting from lists, getting their length.
232 * Append/Reverse:: Appending and reversing lists.
233 * List Modification:: Modifying existing lists.
234 * List Searching:: Searching for list elements
235 * List Mapping:: Applying procedures to lists.
239 @subsubsection List Read Syntax
241 The syntax for lists is an opening parentheses, then all the elements of
242 the list (separated by whitespace) and finally a closing
243 parentheses.@footnote{Note that there is no separation character between
244 the list elements, like a comma or a semicolon.}.
247 (1 2 3) ; @r{a list of the numbers 1, 2 and 3}
248 ("foo" bar 3.1415) ; @r{a string, a symbol and a real number}
249 () ; @r{the empty list}
252 The last example needs a bit more explanation. A list with no elements,
253 called the @dfn{empty list}, is special in some ways. It is used for
254 terminating lists by storing it into the cdr of the last pair that makes
255 up a list. An example will clear that up:
266 This example also shows that lists have to be quoted (REFFIXME) when
267 written, because they would otherwise be mistakingly taken as procedure
268 applications (@pxref{Simple Invocation}).
271 @node List Predicates
272 @subsubsection List Predicates
274 Often it is useful to test whether a given Scheme object is a list or
275 not. List-processing procedures could use this information to test
276 whether their input is valid, or they could do different things
277 depending on the datatype of their arguments.
280 @deffn {Scheme Procedure} list? x
281 @deffnx {C Function} scm_list_p (x)
282 Return @code{#t} iff @var{x} is a proper list, else @code{#f}.
285 The predicate @code{null?} is often used in list-processing code to
286 tell whether a given list has run out of elements. That is, a loop
287 somehow deals with the elements of a list until the list satisfies
288 @code{null?}. Then, the algorithm terminates.
291 @deffn {Scheme Procedure} null? x
292 @deffnx {C Function} scm_null_p (x)
293 Return @code{#t} iff @var{x} is the empty list, else @code{#f}.
296 @deftypefn {C Function} int scm_is_null (SCM x)
297 Return 1 when @var{x} is the empty list; otherwise return 0.
301 @node List Constructors
302 @subsubsection List Constructors
304 This section describes the procedures for constructing new lists.
305 @code{list} simply returns a list where the elements are the arguments,
306 @code{cons*} is similar, but the last argument is stored in the cdr of
307 the last pair of the list.
309 @c C Function scm_list(rest) used to be documented here, but it's a
310 @c no-op since it does nothing but return the list the caller must
311 @c have already created.
313 @deffn {Scheme Procedure} list elem1 @dots{} elemN
314 @deffnx {C Function} scm_list_1 (elem1)
315 @deffnx {C Function} scm_list_2 (elem1, elem2)
316 @deffnx {C Function} scm_list_3 (elem1, elem2, elem3)
317 @deffnx {C Function} scm_list_4 (elem1, elem2, elem3, elem4)
318 @deffnx {C Function} scm_list_5 (elem1, elem2, elem3, elem4, elem5)
319 @deffnx {C Function} scm_list_n (elem1, @dots{}, elemN, @nicode{SCM_UNDEFINED})
321 Return a new list containing elements @var{elem1} to @var{elemN}.
323 @code{scm_list_n} takes a variable number of arguments, terminated by
324 the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is
325 not included in the list. None of @var{elem1} to @var{elemN} can
326 themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will
327 terminate at that point.
330 @c C Function scm_cons_star(arg1,rest) used to be documented here,
331 @c but it's not really a useful interface, since it expects the
332 @c caller to have already consed up all but the first argument
335 @deffn {Scheme Procedure} cons* arg1 arg2 @dots{}
336 Like @code{list}, but the last arg provides the tail of the
337 constructed list, returning @code{(cons @var{arg1} (cons
338 @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
339 argument. If given one argument, that argument is returned as
340 result. This function is called @code{list*} in some other
341 Schemes and in Common LISP.
344 @deffn {Scheme Procedure} list-copy lst
345 @deffnx {C Function} scm_list_copy (lst)
346 Return a (newly-created) copy of @var{lst}.
349 @deffn {Scheme Procedure} make-list n [init]
350 Create a list containing of @var{n} elements, where each element is
351 initialized to @var{init}. @var{init} defaults to the empty list
352 @code{()} if not given.
355 Note that @code{list-copy} only makes a copy of the pairs which make up
356 the spine of the lists. The list elements are not copied, which means
357 that modifying the elements of the new list also modifies the elements
358 of the old list. On the other hand, applying procedures like
359 @code{set-cdr!} or @code{delv!} to the new list will not alter the old
360 list. If you also need to copy the list elements (making a deep copy),
361 use the procedure @code{copy-tree} (@pxref{Copying}).
364 @subsubsection List Selection
366 These procedures are used to get some information about a list, or to
367 retrieve one or more elements of a list.
370 @deffn {Scheme Procedure} length lst
371 @deffnx {C Function} scm_length (lst)
372 Return the number of elements in list @var{lst}.
375 @deffn {Scheme Procedure} last-pair lst
376 @deffnx {C Function} scm_last_pair (lst)
377 Return the last pair in @var{lst}, signalling an error if
378 @var{lst} is circular.
382 @deffn {Scheme Procedure} list-ref list k
383 @deffnx {C Function} scm_list_ref (list, k)
384 Return the @var{k}th element from @var{list}.
388 @deffn {Scheme Procedure} list-tail lst k
389 @deffnx {Scheme Procedure} list-cdr-ref lst k
390 @deffnx {C Function} scm_list_tail (lst, k)
391 Return the "tail" of @var{lst} beginning with its @var{k}th element.
392 The first element of the list is considered to be element 0.
394 @code{list-tail} and @code{list-cdr-ref} are identical. It may help to
395 think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
396 or returning the results of cdring @var{k} times down @var{lst}.
399 @deffn {Scheme Procedure} list-head lst k
400 @deffnx {C Function} scm_list_head (lst, k)
401 Copy the first @var{k} elements from @var{lst} into a new list, and
406 @subsubsection Append and Reverse
408 @code{append} and @code{append!} are used to concatenate two or more
409 lists in order to form a new list. @code{reverse} and @code{reverse!}
410 return lists with the same elements as their arguments, but in reverse
411 order. The procedure variants with an @code{!} directly modify the
412 pairs which form the list, whereas the other procedures create new
413 pairs. This is why you should be careful when using the side-effecting
417 @deffn {Scheme Procedure} append lst1 @dots{} lstN
418 @deffnx {Scheme Procedure} append! lst1 @dots{} lstN
419 @deffnx {C Function} scm_append (lstlst)
420 @deffnx {C Function} scm_append_x (lstlst)
421 Return a list comprising all the elements of lists @var{lst1} to
425 (append '(x) '(y)) @result{} (x y)
426 (append '(a) '(b c d)) @result{} (a b c d)
427 (append '(a (b)) '((c))) @result{} (a (b) (c))
430 The last argument @var{lstN} may actually be any object; an improper
431 list results if the last argument is not a proper list.
434 (append '(a b) '(c . d)) @result{} (a b c . d)
435 (append '() 'a) @result{} a
438 @code{append} doesn't modify the given lists, but the return may share
439 structure with the final @var{lstN}. @code{append!} modifies the
440 given lists to form its return.
442 For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list
443 of the list operands @var{lst1} @dots{} @var{lstN}. That @var{lstlst}
444 itself is not modified or used in the return.
448 @deffn {Scheme Procedure} reverse lst
449 @deffnx {Scheme Procedure} reverse! lst [newtail]
450 @deffnx {C Function} scm_reverse (lst)
451 @deffnx {C Function} scm_reverse_x (lst, newtail)
452 Return a list comprising the elements of @var{lst}, in reverse order.
454 @code{reverse} constructs a new list, @code{reverse!} modifies
455 @var{lst} in constructing its return.
457 For @code{reverse!}, the optional @var{newtail} is appended to to the
458 result. @var{newtail} isn't reversed, it simply becomes the list
459 tail. For @code{scm_reverse_x}, the @var{newtail} parameter is
460 mandatory, but can be @code{SCM_EOL} if no further tail is required.
463 @node List Modification
464 @subsubsection List Modification
466 The following procedures modify an existing list, either by changing
467 elements of the list, or by changing the list structure itself.
469 @deffn {Scheme Procedure} list-set! list k val
470 @deffnx {C Function} scm_list_set_x (list, k, val)
471 Set the @var{k}th element of @var{list} to @var{val}.
474 @deffn {Scheme Procedure} list-cdr-set! list k val
475 @deffnx {C Function} scm_list_cdr_set_x (list, k, val)
476 Set the @var{k}th cdr of @var{list} to @var{val}.
479 @deffn {Scheme Procedure} delq item lst
480 @deffnx {C Function} scm_delq (item, lst)
481 Return a newly-created copy of @var{lst} with elements
482 @code{eq?} to @var{item} removed. This procedure mirrors
483 @code{memq}: @code{delq} compares elements of @var{lst} against
484 @var{item} with @code{eq?}.
487 @deffn {Scheme Procedure} delv item lst
488 @deffnx {C Function} scm_delv (item, lst)
489 Return a newly-created copy of @var{lst} with elements
490 @code{eqv?} to @var{item} removed. This procedure mirrors
491 @code{memv}: @code{delv} compares elements of @var{lst} against
492 @var{item} with @code{eqv?}.
495 @deffn {Scheme Procedure} delete item lst
496 @deffnx {C Function} scm_delete (item, lst)
497 Return a newly-created copy of @var{lst} with elements
498 @code{equal?} to @var{item} removed. This procedure mirrors
499 @code{member}: @code{delete} compares elements of @var{lst}
500 against @var{item} with @code{equal?}.
503 @deffn {Scheme Procedure} delq! item lst
504 @deffnx {Scheme Procedure} delv! item lst
505 @deffnx {Scheme Procedure} delete! item lst
506 @deffnx {C Function} scm_delq_x (item, lst)
507 @deffnx {C Function} scm_delv_x (item, lst)
508 @deffnx {C Function} scm_delete_x (item, lst)
509 These procedures are destructive versions of @code{delq}, @code{delv}
510 and @code{delete}: they modify the pointers in the existing @var{lst}
511 rather than creating a new list. Caveat evaluator: Like other
512 destructive list functions, these functions cannot modify the binding of
513 @var{lst}, and so cannot be used to delete the first element of
514 @var{lst} destructively.
517 @deffn {Scheme Procedure} delq1! item lst
518 @deffnx {C Function} scm_delq1_x (item, lst)
519 Like @code{delq!}, but only deletes the first occurrence of
520 @var{item} from @var{lst}. Tests for equality using
521 @code{eq?}. See also @code{delv1!} and @code{delete1!}.
524 @deffn {Scheme Procedure} delv1! item lst
525 @deffnx {C Function} scm_delv1_x (item, lst)
526 Like @code{delv!}, but only deletes the first occurrence of
527 @var{item} from @var{lst}. Tests for equality using
528 @code{eqv?}. See also @code{delq1!} and @code{delete1!}.
531 @deffn {Scheme Procedure} delete1! item lst
532 @deffnx {C Function} scm_delete1_x (item, lst)
533 Like @code{delete!}, but only deletes the first occurrence of
534 @var{item} from @var{lst}. Tests for equality using
535 @code{equal?}. See also @code{delq1!} and @code{delv1!}.
538 @deffn {Scheme Procedure} filter pred lst
539 @deffnx {Scheme Procedure} filter! pred lst
540 Return a list containing all elements from @var{lst} which satisfy the
541 predicate @var{pred}. The elements in the result list have the same
542 order as in @var{lst}. The order in which @var{pred} is applied to
543 the list elements is not specified.
545 @code{filter!} is allowed, but not required to modify the structure of
549 @subsubsection List Searching
551 The following procedures search lists for particular elements. They use
552 different comparison predicates for comparing list elements with the
553 object to be searched. When they fail, they return @code{#f}, otherwise
554 they return the sublist whose car is equal to the search object, where
555 equality depends on the equality predicate used.
558 @deffn {Scheme Procedure} memq x lst
559 @deffnx {C Function} scm_memq (x, lst)
560 Return the first sublist of @var{lst} whose car is @code{eq?}
561 to @var{x} where the sublists of @var{lst} are the non-empty
562 lists returned by @code{(list-tail @var{lst} @var{k})} for
563 @var{k} less than the length of @var{lst}. If @var{x} does not
564 occur in @var{lst}, then @code{#f} (not the empty list) is
569 @deffn {Scheme Procedure} memv x lst
570 @deffnx {C Function} scm_memv (x, lst)
571 Return the first sublist of @var{lst} whose car is @code{eqv?}
572 to @var{x} where the sublists of @var{lst} are the non-empty
573 lists returned by @code{(list-tail @var{lst} @var{k})} for
574 @var{k} less than the length of @var{lst}. If @var{x} does not
575 occur in @var{lst}, then @code{#f} (not the empty list) is
580 @deffn {Scheme Procedure} member x lst
581 @deffnx {C Function} scm_member (x, lst)
582 Return the first sublist of @var{lst} whose car is
583 @code{equal?} to @var{x} where the sublists of @var{lst} are
584 the non-empty lists returned by @code{(list-tail @var{lst}
585 @var{k})} for @var{k} less than the length of @var{lst}. If
586 @var{x} does not occur in @var{lst}, then @code{#f} (not the
587 empty list) is returned.
592 @subsubsection List Mapping
594 List processing is very convenient in Scheme because the process of
595 iterating over the elements of a list can be highly abstracted. The
596 procedures in this section are the most basic iterating procedures for
597 lists. They take a procedure and one or more lists as arguments, and
598 apply the procedure to each element of the list. They differ in their
602 @c begin (texi-doc-string "guile" "map")
603 @deffn {Scheme Procedure} map proc arg1 arg2 @dots{}
604 @deffnx {Scheme Procedure} map-in-order proc arg1 arg2 @dots{}
605 @deffnx {C Function} scm_map (proc, arg1, args)
606 Apply @var{proc} to each element of the list @var{arg1} (if only two
607 arguments are given), or to the corresponding elements of the argument
608 lists (if more than two arguments are given). The result(s) of the
609 procedure applications are saved and returned in a list. For
610 @code{map}, the order of procedure applications is not specified,
611 @code{map-in-order} applies the procedure from left to right to the list
616 @c begin (texi-doc-string "guile" "for-each")
617 @deffn {Scheme Procedure} for-each proc arg1 arg2 @dots{}
618 Like @code{map}, but the procedure is always applied from left to right,
619 and the result(s) of the procedure applications are thrown away. The
620 return value is not specified.
628 Vectors are sequences of Scheme objects. Unlike lists, the length of a
629 vector, once the vector is created, cannot be changed. The advantage of
630 vectors over lists is that the time required to access one element of a vector
631 given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number,
632 is constant, whereas lists have an access time linear to the position of the
633 accessed element in the list.
635 Vectors can contain any kind of Scheme object; it is even possible to
636 have different types of objects in the same vector. For vectors
637 containing vectors, you may wish to use arrays, instead. Note, too,
638 that vectors are the special case of non-uniform, one-dimensional,
639 zero-origin arrays and that most array procedures operate happily on
640 vectors (@pxref{Arrays}).
643 * Vector Syntax:: Read syntax for vectors.
644 * Vector Creation:: Dynamic vector creation and validation.
645 * Vector Accessors:: Accessing and modifying vector contents.
650 @subsubsection Read Syntax for Vectors
652 Vectors can literally be entered in source code, just like strings,
653 characters or some of the other data types. The read syntax for vectors
654 is as follows: A sharp sign (@code{#}), followed by an opening
655 parentheses, all elements of the vector in their respective read syntax,
656 and finally a closing parentheses. The following are examples of the
657 read syntax for vectors; where the first vector only contains numbers
658 and the second three different object types: a string, a symbol and a
659 number in hexadecimal notation.
663 #("Hello" foo #xdeadbeef)
666 Like lists, vectors have to be quoted (REFFIXME):
669 '#(a b c) @result{} #(a b c)
672 @node Vector Creation
673 @subsubsection Dynamic Vector Creation and Validation
675 Instead of creating a vector implicitly by using the read syntax just
676 described, you can create a vector dynamically by calling one of the
677 @code{vector} and @code{list->vector} primitives with the list of Scheme
678 values that you want to place into a vector. The size of the vector
679 thus created is determined implicitly by the number of arguments given.
682 @rnindex list->vector
683 @deffn {Scheme Procedure} vector . l
684 @deffnx {Scheme Procedure} list->vector l
685 @deffnx {C Function} scm_vector (l)
686 Return a newly allocated vector composed of the
687 given arguments. Analogous to @code{list}.
690 (vector 'a 'b 'c) @result{} #(a b c)
694 (As an aside, an interesting implementation detail is that the Guile
695 reader reads the @code{#(@dots{})} syntax by reading everything but the
696 initial @code{#} as a @emph{list}, and then passing the list that
697 results to @code{list->vector}. Notice how neatly this fits with the
698 similarity between the read (and print) syntaxes for lists and vectors.)
700 The inverse operation is @code{vector->list}:
702 @rnindex vector->list
703 @deffn {Scheme Procedure} vector->list v
704 @deffnx {C Function} scm_vector_to_list (v)
705 Return a newly allocated list composed of the elements of @var{v}.
708 (vector->list '#(dah dah didah)) @result{} (dah dah didah)
709 (list->vector '(dididit dah)) @result{} #(dididit dah)
713 To allocate a vector with an explicitly specified size, use
714 @code{make-vector}. With this primitive you can also specify an initial
715 value for the vector elements (the same value for all elements, that
719 @deffn {Scheme Procedure} make-vector len [fill]
720 @deffnx {C Function} scm_make_vector (len, fill)
721 Return a newly allocated vector of @var{len} elements. If a
722 second argument is given, then each position is initialized to
723 @var{fill}. Otherwise the initial contents of each position is
727 @deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill)
728 Like @code{scm_make_vector}, but the length is given as a @code{size_t}.
731 To check whether an arbitrary Scheme value @emph{is} a vector, use the
732 @code{vector?} primitive:
735 @deffn {Scheme Procedure} vector? obj
736 @deffnx {C Function} scm_vector_p (obj)
737 Return @code{#t} if @var{obj} is a vector, otherwise return
741 @deftypefn {C Function} int scm_is_vector (SCM obj)
742 Return non-zero when @var{obj} is a vector, otherwise return
746 @node Vector Accessors
747 @subsubsection Accessing and Modifying Vector Contents
749 @code{vector-length} and @code{vector-ref} return information about a
750 given vector, respectively its size and the elements that are contained
753 @rnindex vector-length
754 @deffn {Scheme Procedure} vector-length vector
755 @deffnx {C Function} scm_vector_length vector
756 Return the number of elements in @var{vector} as an exact integer.
759 @deftypefn {C Function} size_t scm_c_vector_length (SCM v)
760 Return the number of elements in @var{vector} as a @code{size_t}.
764 @deffn {Scheme Procedure} vector-ref vector k
765 @deffnx {C Function} scm_vector_ref vector k
766 Return the contents of position @var{k} of @var{vector}.
767 @var{k} must be a valid index of @var{vector}.
769 (vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8
770 (vector-ref '#(1 1 2 3 5 8 13 21)
771 (let ((i (round (* 2 (acos -1)))))
778 @deftypefn {C Function} SCM scm_c_vector_ref (SCM v, size_t k)
779 Return the contents of position @var{k} (s @code{size_t}) of
783 A vector created by one of the dynamic vector constructor procedures
784 (@pxref{Vector Creation}) can be modified using the following
787 @emph{NOTE:} According to R5RS, it is an error to use any of these
788 procedures on a literally read vector, because such vectors should be
789 considered as constants. Currently, however, Guile does not detect this
793 @deffn {Scheme Procedure} vector-set! vector k obj
794 @deffnx {C Function} scm_vector_set_x vector k obj
795 Store @var{obj} in position @var{k} of @var{vector}.
796 @var{k} must be a valid index of @var{vector}.
797 The value returned by @samp{vector-set!} is unspecified.
799 (let ((vec (vector 0 '(2 2 2 2) "Anna")))
800 (vector-set! vec 1 '("Sue" "Sue"))
801 vec) @result{} #(0 ("Sue" "Sue") "Anna")
805 @deftypefn {C Function} void scm_c_vector_set_x (SCM v, size_t k, SCM obj)
806 Store @var{obj} in position @var{k} (a @code{size_t}) of @var{v}.
809 @rnindex vector-fill!
810 @deffn {Scheme Procedure} vector-fill! v fill
811 @deffnx {C Function} scm_vector_fill_x (v, fill)
812 Store @var{fill} in every position of @var{vector}. The value
813 returned by @code{vector-fill!} is unspecified.
816 @deffn {Scheme Procedure} vector-move-left! vec1 start1 end1 vec2 start2
817 @deffnx {C Function} scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
818 Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
819 to @var{vec2} starting at position @var{start2}. @var{start1} and
820 @var{start2} are inclusive indices; @var{end1} is exclusive.
822 @code{vector-move-left!} copies elements in leftmost order.
823 Therefore, in the case where @var{vec1} and @var{vec2} refer to the
824 same vector, @code{vector-move-left!} is usually appropriate when
825 @var{start1} is greater than @var{start2}.
828 @deffn {Scheme Procedure} vector-move-right! vec1 start1 end1 vec2 start2
829 @deffnx {C Function} scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
830 Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
831 to @var{vec2} starting at position @var{start2}. @var{start1} and
832 @var{start2} are inclusive indices; @var{end1} is exclusive.
834 @code{vector-move-right!} copies elements in rightmost order.
835 Therefore, in the case where @var{vec1} and @var{vec2} refer to the
836 same vector, @code{vector-move-right!} is usually appropriate when
837 @var{start1} is less than @var{start2}.
840 @node Uniform Numeric Vectors
841 @subsection Uniform Numeric Vectors
843 A uniform numeric vector is a vector whose elements are all of a single
844 numeric type. Guile offers uniform vectors for signed and unsigned
845 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of floating point
846 values, and complex floating-point numbers of these two sizes.
848 Strings could be regarded as uniform vectors of characters,
849 @xref{Strings}. Likewise, bit vectors could be regarded as uniform
850 vectors of bits, @xref{Bit Vectors}. Both are sufficiently different
851 from numeric vectors that the procedures described here do not apply to
852 these two data types. However, both strings and bit vectors are
853 generalized vectors, @xref{Generalized Vectors}, and arrays,
856 Uniform numeric vectors are the special case of one-dimensional, uniform
857 numeric, zero-origin arrays.
859 Uniform numeric vectors can be useful since they consume less memory
860 than the non-uniform, general vectors. Also, since the types they can
861 store correspond directly to C types, it is easier to work with them
862 efficiently on a low level. Consider image processing as an example,
863 where you want to apply a filter to some image. While you could store
864 the pixels of an image in a general vector and write a general
865 convolution function, things are much more efficient with uniform
866 vectors: the convolution function knows that all pixels are unsigned
867 8-bit values (say), and can use a very tight inner loop.
869 That is, when it is written in C. Functions for efficiently working
870 with uniform numeric vectors from C are listed at the end of this
873 Procedures similar to the vector procedures (@pxref{Vectors}) are
874 provided for handling these uniform vectors, but they are distinct
875 datatypes and the two cannot be inter-mixed. If you want to work
876 primarily with uniform numeric vectors, but want to offer support for
877 general vectors as a convenience, you can use one of the
878 @code{scm_any_to_*} functions. They will coerce lists and vectors to
879 the given type of uniform vector.
881 One set of these procedures is a generic one: it works with all types of
882 uniform vectors. In addition to that, there is a set of procedures for
883 each type that only works with that type. Unless you really need to the
884 generality of the first set, it is best to use the more specific
885 functions. They might not be that much faster, but their use can serve
886 as a kind of declaration and makes it easier to optimize later on.
888 The generic set of procedures uses @code{uniform-vector} in its names,
889 the specific ones use the tag from the following table.
893 unsigned 8-bit integers
896 signed 8-bit integers
899 unsigned 16-bit integers
902 signed 16-bit integers
905 unsigned 32-bit integers
908 signed 32-bit integers
911 unsigned 64-bit integers
914 signed 64-bit integers
917 the C type @code{float}
920 the C type @code{double}
923 complex numbers in rectangular form with the real and imaginary part
927 complex numbers in rectangular form with the real and imaginary part
928 being a @code{double}
932 The external representation (ie.@: read syntax) for these vectors is
933 similar to normal Scheme vectors, but with an additional tag from the
934 tabel above indiciating the vector's type. For example,
941 Note that the read syntax for floating-point here conflicts with
942 @code{#f} for false. In Standard Scheme one can write @code{(1 #f3)}
943 for a three element list @code{(1 #f 3)}, but for Guile @code{(1 #f3)}
944 is invalid. @code{(1 #f 3)} is almost certainly what one should write
945 anyway to make the intention clear, so this is rarely a problem.
947 @deffn {Scheme Procedure} uniform-vector? obj
948 @deffnx {Scheme Procedure} u8vector? obj
949 @deffnx {Scheme Procedure} s8vector? obj
950 @deffnx {Scheme Procedure} u16vector? obj
951 @deffnx {Scheme Procedure} s16vector? obj
952 @deffnx {Scheme Procedure} u32vector? obj
953 @deffnx {Scheme Procedure} s32vector? obj
954 @deffnx {Scheme Procedure} u64vector? obj
955 @deffnx {Scheme Procedure} s64vector? obj
956 @deffnx {Scheme Procedure} f32vector? obj
957 @deffnx {Scheme Procedure} f64vector? obj
958 @deffnx {Scheme Procedure} c32vector? obj
959 @deffnx {Scheme Procedure} c64vector? obj
960 @deffnx {C Function} scm_uniform_vector_p obj
961 @deffnx {C Function} scm_u8vector_p obj
962 @deffnx {C Function} scm_s8vector_p obj
963 @deffnx {C Function} scm_u16vector_p obj
964 @deffnx {C Function} scm_s16vector_p obj
965 @deffnx {C Function} scm_u32vector_p obj
966 @deffnx {C Function} scm_s32vector_p obj
967 @deffnx {C Function} scm_u64vector_p obj
968 @deffnx {C Function} scm_s64vector_p obj
969 @deffnx {C Function} scm_f32vector_p obj
970 @deffnx {C Function} scm_f64vector_p obj
971 @deffnx {C Function} scm_c32vector_p obj
972 @deffnx {C Function} scm_c64vector_p obj
973 Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
977 @deffn {Scheme Procedure} make-u8vector n [value]
978 @deffnx {Scheme Procedure} make-s8vector n [value]
979 @deffnx {Scheme Procedure} make-u16vector n [value]
980 @deffnx {Scheme Procedure} make-s16vector n [value]
981 @deffnx {Scheme Procedure} make-u32vector n [value]
982 @deffnx {Scheme Procedure} make-s32vector n [value]
983 @deffnx {Scheme Procedure} make-u64vector n [value]
984 @deffnx {Scheme Procedure} make-s64vector n [value]
985 @deffnx {Scheme Procedure} make-f32vector n [value]
986 @deffnx {Scheme Procedure} make-f64vector n [value]
987 @deffnx {Scheme Procedure} make-c32vector n [value]
988 @deffnx {Scheme Procedure} make-c64vector n [value]
989 @deffnx {C Function} scm_make_u8vector n [value]
990 @deffnx {C Function} scm_make_s8vector n [value]
991 @deffnx {C Function} scm_make_u16vector n [value]
992 @deffnx {C Function} scm_make_s16vector n [value]
993 @deffnx {C Function} scm_make_u32vector n [value]
994 @deffnx {C Function} scm_make_s32vector n [value]
995 @deffnx {C Function} scm_make_u64vector n [value]
996 @deffnx {C Function} scm_make_s64vector n [value]
997 @deffnx {C Function} scm_make_f32vector n [value]
998 @deffnx {C Function} scm_make_f64vector n [value]
999 @deffnx {C Function} scm_make_c32vector n [value]
1000 @deffnx {C Function} scm_make_c64vector n [value]
1001 Return a newly allocated homogeneous numeric vector holding @var{n}
1002 elements of the indicated type. If @var{value} is given, the vector
1003 is initialized with that value, otherwise the contents are
1007 @deffn {Scheme Procedure} u8vector value @dots{}
1008 @deffnx {Scheme Procedure} s8vector value @dots{}
1009 @deffnx {Scheme Procedure} u16vector value @dots{}
1010 @deffnx {Scheme Procedure} s16vector value @dots{}
1011 @deffnx {Scheme Procedure} u32vector value @dots{}
1012 @deffnx {Scheme Procedure} s32vector value @dots{}
1013 @deffnx {Scheme Procedure} u64vector value @dots{}
1014 @deffnx {Scheme Procedure} s64vector value @dots{}
1015 @deffnx {Scheme Procedure} f32vector value @dots{}
1016 @deffnx {Scheme Procedure} f64vector value @dots{}
1017 @deffnx {Scheme Procedure} c32vector value @dots{}
1018 @deffnx {Scheme Procedure} c64vector value @dots{}
1019 @deffnx {C Function} scm_u8vector values
1020 @deffnx {C Function} scm_s8vector values
1021 @deffnx {C Function} scm_u16vector values
1022 @deffnx {C Function} scm_s16vector values
1023 @deffnx {C Function} scm_u32vector values
1024 @deffnx {C Function} scm_s32vector values
1025 @deffnx {C Function} scm_u64vector values
1026 @deffnx {C Function} scm_s64vector values
1027 @deffnx {C Function} scm_f32vector values
1028 @deffnx {C Function} scm_f64vector values
1029 @deffnx {C Function} scm_c32vector values
1030 @deffnx {C Function} scm_c64vector values
1031 Return a newly allocated homogeneous numeric vector of the indicated
1032 type, holding the given parameter @var{value}s. The vector length is
1033 the number of parameters given.
1036 @deffn {Scheme Procedure} uniform-vector-length vec
1037 @deffnx {Scheme Procedure} u8vector-length vec
1038 @deffnx {Scheme Procedure} s8vector-length vec
1039 @deffnx {Scheme Procedure} u16vector-length vec
1040 @deffnx {Scheme Procedure} s16vector-length vec
1041 @deffnx {Scheme Procedure} u32vector-length vec
1042 @deffnx {Scheme Procedure} s32vector-length vec
1043 @deffnx {Scheme Procedure} u64vector-length vec
1044 @deffnx {Scheme Procedure} s64vector-length vec
1045 @deffnx {Scheme Procedure} f32vector-length vec
1046 @deffnx {Scheme Procedure} f64vector-length vec
1047 @deffnx {Scheme Procedure} c32vector-length vec
1048 @deffnx {Scheme Procedure} c64vector-length vec
1049 @deffnx {C Function} scm_uniform_vector_length vec
1050 @deffnx {C Function} scm_u8vector_length vec
1051 @deffnx {C Function} scm_s8vector_length vec
1052 @deffnx {C Function} scm_u16vector_length vec
1053 @deffnx {C Function} scm_s16vector_length vec
1054 @deffnx {C Function} scm_u32vector_length vec
1055 @deffnx {C Function} scm_s32vector_length vec
1056 @deffnx {C Function} scm_u64vector_length vec
1057 @deffnx {C Function} scm_s64vector_length vec
1058 @deffnx {C Function} scm_f32vector_length vec
1059 @deffnx {C Function} scm_f64vector_length vec
1060 @deffnx {C Function} scm_c32vector_length vec
1061 @deffnx {C Function} scm_c64vector_length vec
1062 Return the number of elements in @var{vec}.
1065 @deffn {Scheme Procedure} uniform-vector-ref vec i
1066 @deffnx {Scheme Procedure} u8vector-ref vec i
1067 @deffnx {Scheme Procedure} s8vector-ref vec i
1068 @deffnx {Scheme Procedure} u16vector-ref vec i
1069 @deffnx {Scheme Procedure} s16vector-ref vec i
1070 @deffnx {Scheme Procedure} u32vector-ref vec i
1071 @deffnx {Scheme Procedure} s32vector-ref vec i
1072 @deffnx {Scheme Procedure} u64vector-ref vec i
1073 @deffnx {Scheme Procedure} s64vector-ref vec i
1074 @deffnx {Scheme Procedure} f32vector-ref vec i
1075 @deffnx {Scheme Procedure} f64vector-ref vec i
1076 @deffnx {Scheme Procedure} c32vector-ref vec i
1077 @deffnx {Scheme Procedure} c64vector-ref vec i
1078 @deffnx {C Function} scm_uniform_vector_ref vec i
1079 @deffnx {C Function} scm_u8vector_ref vec i
1080 @deffnx {C Function} scm_s8vector_ref vec i
1081 @deffnx {C Function} scm_u16vector_ref vec i
1082 @deffnx {C Function} scm_s16vector_ref vec i
1083 @deffnx {C Function} scm_u32vector_ref vec i
1084 @deffnx {C Function} scm_s32vector_ref vec i
1085 @deffnx {C Function} scm_u64vector_ref vec i
1086 @deffnx {C Function} scm_s64vector_ref vec i
1087 @deffnx {C Function} scm_f32vector_ref vec i
1088 @deffnx {C Function} scm_f64vector_ref vec i
1089 @deffnx {C Function} scm_c32vector_ref vec i
1090 @deffnx {C Function} scm_c64vector_ref vec i
1091 Return the element at index @var{i} in @var{vec}. The first element
1092 in @var{vec} is index 0.
1095 @deffn {Scheme Procedure} uniform-vector-set! vec i value
1096 @deffnx {Scheme Procedure} u8vector-set! vec i value
1097 @deffnx {Scheme Procedure} s8vector-set! vec i value
1098 @deffnx {Scheme Procedure} u16vector-set! vec i value
1099 @deffnx {Scheme Procedure} s16vector-set! vec i value
1100 @deffnx {Scheme Procedure} u32vector-set! vec i value
1101 @deffnx {Scheme Procedure} s32vector-set! vec i value
1102 @deffnx {Scheme Procedure} u64vector-set! vec i value
1103 @deffnx {Scheme Procedure} s64vector-set! vec i value
1104 @deffnx {Scheme Procedure} f32vector-set! vec i value
1105 @deffnx {Scheme Procedure} f64vector-set! vec i value
1106 @deffnx {Scheme Procedure} c32vector-set! vec i value
1107 @deffnx {Scheme Procedure} c64vector-set! vec i value
1108 @deffnx {C Function} scm_uniform_vector_set_x vec i value
1109 @deffnx {C Function} scm_u8vector_set_x vec i value
1110 @deffnx {C Function} scm_s8vector_set_x vec i value
1111 @deffnx {C Function} scm_u16vector_set_x vec i value
1112 @deffnx {C Function} scm_s16vector_set_x vec i value
1113 @deffnx {C Function} scm_u32vector_set_x vec i value
1114 @deffnx {C Function} scm_s32vector_set_x vec i value
1115 @deffnx {C Function} scm_u64vector_set_x vec i value
1116 @deffnx {C Function} scm_s64vector_set_x vec i value
1117 @deffnx {C Function} scm_f32vector_set_x vec i value
1118 @deffnx {C Function} scm_f64vector_set_x vec i value
1119 @deffnx {C Function} scm_c32vector_set_x vec i value
1120 @deffnx {C Function} scm_c64vector_set_x vec i value
1121 Set the element at index @var{i} in @var{vec} to @var{value}. The
1122 first element in @var{vec} is index 0. The return value is
1126 @deffn {Scheme Procedure} uniform-vector->list vec
1127 @deffnx {Scheme Procedure} u8vector->list vec
1128 @deffnx {Scheme Procedure} s8vector->list vec
1129 @deffnx {Scheme Procedure} u16vector->list vec
1130 @deffnx {Scheme Procedure} s16vector->list vec
1131 @deffnx {Scheme Procedure} u32vector->list vec
1132 @deffnx {Scheme Procedure} s32vector->list vec
1133 @deffnx {Scheme Procedure} u64vector->list vec
1134 @deffnx {Scheme Procedure} s64vector->list vec
1135 @deffnx {Scheme Procedure} f32vector->list vec
1136 @deffnx {Scheme Procedure} f64vector->list vec
1137 @deffnx {Scheme Procedure} c32vector->list vec
1138 @deffnx {Scheme Procedure} c64vector->list vec
1139 @deffnx {C Function} scm_uniform_vector_to_list vec
1140 @deffnx {C Function} scm_u8vector_to_list vec
1141 @deffnx {C Function} scm_s8vector_to_list vec
1142 @deffnx {C Function} scm_u16vector_to_list vec
1143 @deffnx {C Function} scm_s16vector_to_list vec
1144 @deffnx {C Function} scm_u32vector_to_list vec
1145 @deffnx {C Function} scm_s32vector_to_list vec
1146 @deffnx {C Function} scm_u64vector_to_list vec
1147 @deffnx {C Function} scm_s64vector_to_list vec
1148 @deffnx {C Function} scm_f32vector_to_list vec
1149 @deffnx {C Function} scm_f64vector_to_list vec
1150 @deffnx {C Function} scm_c32vector_to_list vec
1151 @deffnx {C Function} scm_c64vector_to_list vec
1152 Return a newly allocated list holding all elements of @var{vec}.
1155 @deffn {Scheme Procedure} list->u8vector lst
1156 @deffnx {Scheme Procedure} list->s8vector lst
1157 @deffnx {Scheme Procedure} list->u16vector lst
1158 @deffnx {Scheme Procedure} list->s16vector lst
1159 @deffnx {Scheme Procedure} list->u32vector lst
1160 @deffnx {Scheme Procedure} list->s32vector lst
1161 @deffnx {Scheme Procedure} list->u64vector lst
1162 @deffnx {Scheme Procedure} list->s64vector lst
1163 @deffnx {Scheme Procedure} list->f32vector lst
1164 @deffnx {Scheme Procedure} list->f64vector lst
1165 @deffnx {Scheme Procedure} list->c32vector lst
1166 @deffnx {Scheme Procedure} list->c64vector lst
1167 @deffnx {C Function} scm_list_to_u8vector lst
1168 @deffnx {C Function} scm_list_to_s8vector lst
1169 @deffnx {C Function} scm_list_to_u16vector lst
1170 @deffnx {C Function} scm_list_to_s16vector lst
1171 @deffnx {C Function} scm_list_to_u32vector lst
1172 @deffnx {C Function} scm_list_to_s32vector lst
1173 @deffnx {C Function} scm_list_to_u64vector lst
1174 @deffnx {C Function} scm_list_to_s64vector lst
1175 @deffnx {C Function} scm_list_to_f32vector lst
1176 @deffnx {C Function} scm_list_to_f64vector lst
1177 @deffnx {C Function} scm_list_to_c32vector lst
1178 @deffnx {C Function} scm_list_to_c64vector lst
1179 Return a newly allocated homogeneous numeric vector of the indicated type,
1180 initialized with the elements of the list @var{lst}.
1183 @deffn {Scheme Procedure} any->u8vector obj
1184 @deffnx {Scheme Procedure} any->s8vector obj
1185 @deffnx {Scheme Procedure} any->u16vector obj
1186 @deffnx {Scheme Procedure} any->s16vector obj
1187 @deffnx {Scheme Procedure} any->u32vector obj
1188 @deffnx {Scheme Procedure} any->s32vector obj
1189 @deffnx {Scheme Procedure} any->u64vector obj
1190 @deffnx {Scheme Procedure} any->s64vector obj
1191 @deffnx {Scheme Procedure} any->f32vector obj
1192 @deffnx {Scheme Procedure} any->f64vector obj
1193 @deffnx {Scheme Procedure} any->c32vector obj
1194 @deffnx {Scheme Procedure} any->c64vector obj
1195 @deffnx {C Function} scm_any_to_u8vector obj
1196 @deffnx {C Function} scm_any_to_s8vector obj
1197 @deffnx {C Function} scm_any_to_u16vector obj
1198 @deffnx {C Function} scm_any_to_s16vector obj
1199 @deffnx {C Function} scm_any_to_u32vector obj
1200 @deffnx {C Function} scm_any_to_s32vector obj
1201 @deffnx {C Function} scm_any_to_u64vector obj
1202 @deffnx {C Function} scm_any_to_s64vector obj
1203 @deffnx {C Function} scm_any_to_f32vector obj
1204 @deffnx {C Function} scm_any_to_f64vector obj
1205 @deffnx {C Function} scm_any_to_c32vector obj
1206 @deffnx {C Function} scm_any_to_c64vector obj
1207 Return a newly allocated homogeneous numeric vector of the indicated
1208 type, initialized with the elements of @var{obj}, which must be a list,
1209 a vector, or a uniform vector.
1212 @deftypefn {C Function} int scm_is_uniform_vector (SCM uvec)
1213 Return @code{1} when @var{uvec} is a uniform vector, @code{0} otherwise.
1216 @deftypefn {C Function} SCM scm_take_u8vector (const scm_t_uint8 *data, size_t len)
1217 @deftypefnx {C Function} SCM scm_take_s8vector (const scm_t_int8 *data, size_t len)
1218 @deftypefnx {C Function} SCM scm_take_u16vector (const scm_t_uint16 *data, size_t len)
1219 @deftypefnx {C Function} SCM scm_take_s168vector (const scm_t_int16 *data, size_t len)
1220 @deftypefnx {C Function} SCM scm_take_u32vector (const scm_t_uint32 *data, size_t len)
1221 @deftypefnx {C Function} SCM scm_take_s328vector (const scm_t_int32 *data, size_t len)
1222 @deftypefnx {C Function} SCM scm_take_u64vector (const scm_t_uint64 *data, size_t len)
1223 @deftypefnx {C Function} SCM scm_take_s64vector (const scm_t_int64 *data, size_t len)
1224 @deftypefnx {C Function} SCM scm_take_f32vector (const float *data, size_t len)
1225 @deftypefnx {C Function} SCM scm_take_f64vector (const double *data, size_t len)
1226 @deftypefnx {C Function} SCM scm_take_c32vector (const float *data, size_t len)
1227 @deftypefnx {C Function} SCM scm_take_c64vector (const double *data, size_t len)
1228 Return a new uniform vector of the indicated type and length that uses
1229 the memory pointed to by @var{data} to store its elements. This memory
1230 will eventually be freed with @code{free}. The argument @var{len}
1231 specifies the number of elements in @var{data}, not its size in bytes.
1233 The @code{c32} and @code{c64} variants take a pointer to a C array of
1234 @code{float}s or @code{double}s. The real parts of the complex numbers
1235 are at even indices in that array, the corresponding imaginary parts are
1236 at the following odd index.
1239 @deftypefn {C Function} {void *} scm_uniform_vector_elements (SCM uvec)
1240 @deftypefnx {C Function} {scm_t_uint8 *} scm_u8vector_elements (SCM uvec)
1241 @deftypefnx {C Function} {scm_t_int8 *} scm_s8vector_elements (SCM uvec)
1242 @deftypefnx {C Function} {scm_t_uint16 *} scm_u16vector_elements (SCM uvec)
1243 @deftypefnx {C Function} {scm_t_int16 *} scm_s16vector_elements (SCM uvec)
1244 @deftypefnx {C Function} {scm_t_uint32 *} scm_u32vector_elements (SCM uvec)
1245 @deftypefnx {C Function} {scm_t_int32 *} scm_s32vector_elements (SCM uvec)
1246 @deftypefnx {C Function} {scm_t_uint64 *} scm_u64vector_elements (SCM uvec)
1247 @deftypefnx {C Function} {scm_t_int64 *} scm_s64vector_elements (SCM uvec)
1248 @deftypefnx {C Function} {float *} scm_f32vector_elements (SCM uvec)
1249 @deftypefnx {C Function} {double *} scm_f64vector_elements (SCM uvec)
1250 @deftypefnx {C Function} {float *} scm_c32vector_elements (SCM uvec)
1251 @deftypefnx {C Function} {double *} scm_c64vector_elements (SCM uvec)
1252 Return a pointer to the elements of a uniform vector. For each call to
1253 one of these functions, you must call @code{scm_uniform_vector_release}
1254 and after that call the pointer to the elements becomes invalid.
1256 The @code{c32} and @code{c64} variants return pointers to a C array of
1257 @code{float}s or @code{double}s. The real parts of the complex numbers
1258 are at even indices in that array, the corresponding imaginary parts are
1259 at the following odd index.
1262 @deftypefn {C Function} void scm_uniform_vector_release (SCM uvec)
1263 Finish the access to the elements of a uniform vector, as exlained
1267 @deftypefn {C Function} void scm_frame_uniform_vector_release (SCM uvec)
1268 Arrange for @code{scm_uniform_vector_release} to be called with
1269 @var{uvec} when the current frame is unwound, implicitely or
1270 explicitely; @xref{Frames}.
1273 @deftypefn {C Function} size_t scm_c_uniform_vector_length (SCM uvec)
1274 Return the number of elements of @var{uvec} as a @code{size_t}.
1277 @deftypefn {C Function} size_t scm_c_uniform_vector_element_size (SCM uvec)
1278 Return the number of bytes of one element of @var{uvec}.
1281 @deftypefn {C Function} size_t scm_c_uniform_vector_size (SCM uvec)
1282 Return the number of bytes used by all elements of @var{uvec}. This is
1283 just @code{scm_c_uniform_vector_length (@var{uvec}) *
1284 scm_c_uniform_vector_element_size (@var{uvec})}.
1289 @subsection Bit Vectors
1292 Bit vectors are zero-origin, one-dimensional arrays of booleans. They
1293 are displayed as a sequence of @code{0}s and @code{1}s prefixed by
1297 (make-bitvector 8 #f) @result{}
1301 Bit vectors are are also generalized vectors, @xref{Generalized
1302 Vectors}, and can thus be used with the array procedures, @xref{Arrays}.
1304 @deffn {Scheme Procedure} bitvector? obj
1305 @deffnx {C Function} scm_bitvector_p (obj)
1306 Return @code{#t} when @var{obj} is a bitvector, else
1310 @deftypefn {C Function} int scm_is_bitvector (SCM obj)
1311 Return @code{1} when @var{obj} is a bitvector, else return @code{0}.
1314 @deffn {Scheme Procedure} make-bitvector len [fill]
1315 @deffnx {C Function} scm_make_bitvector (len, fill)
1316 Create a new bitvector of length @var{len} and
1317 optionally initialize all elements to @var{fill}.
1320 @deftypefn {C Function} SCM scm_c_make_bitvector (size_t len, SCM fill)
1321 Like @code{scm_make_bitvector}, but the length is given as a
1325 @deffn {Scheme Procedure} bitvector . bits
1326 @deffnx {C Function} scm_bitvector (bits)
1327 Create a new bitvector with the arguments as elements.
1330 @deffn {Scheme Procedure} bitvector-length vec
1331 @deffnx {C Function} scm_bitvector_length (vec)
1332 Return the length of the bitvector @var{vec}.
1335 @deftypefn {C Function} size_t scm_c_bitvector_length (SCM vec)
1336 Like @code{scm_bitvector_length}, but the length is returned as a
1340 @deffn {Scheme Procedure} bitvector-ref vec idx
1341 @deffnx {C Function} scm_bitvector_ref (vec, idx)
1342 Return the element at index @var{idx} of the bitvector
1346 @deftypefn {C Function} SCM scm_c_bitvector_ref (SCM obj, size_t idx)
1347 Return the element at index @var{idx} of the bitvector
1351 @deffn {Scheme Procedure} bitvector-set! vec idx val
1352 @deffnx {C Function} scm_bitvector_set_x (vec, idx, val)
1353 Set the element at index @var{idx} of the bitvector
1354 @var{vec} when @var{val} is true, else clear it.
1357 @deftypefn {C Function} SCM scm_c_bitvector_set_x (SCM obj, size_t idx, SCM val)
1358 Set the element at index @var{idx} of the bitvector
1359 @var{vec} when @var{val} is true, else clear it.
1362 @deffn {Scheme Procedure} bitvector-fill! vec val
1363 @deffnx {C Function} scm_bitvector_fill_x (vec, val)
1364 Set all elements of the bitvector
1365 @var{vec} when @var{val} is true, else clear them.
1368 @deffn {Scheme Procedure} list->bitvector list
1369 @deffnx {C Function} scm_list_to_bitvector (list)
1370 Return a new bitvector initialized with the elements
1374 @deffn {Scheme Procedure} bitvector->list vec
1375 @deffnx {C Function} scm_bitvector_to_list (vec)
1376 Return a new list initialized with the elements
1377 of the bitvector @var{vec}.
1380 @deftypefn {C Function} scm_t_uint32 *scm_bitvector_elements (SCM vec)
1381 Return a pointer to the memory that holds the elements of @var{vec}.
1382 Each word pointed to by this pointer has exactly 32 bits, the least
1383 significant bit having the lowest index.
1385 There are @code{(scm_c_bitvector_length(@var{vec})+31)/32} words in the
1386 returned memory block. The last word might be only partially used when
1387 the bitvector length is not a multiple of 32. The unused bits must be
1388 ignored during reading.
1390 For each call to this function, you must call
1391 @code{scm_bitvector_release} and after that call the pointer to the
1392 elements becomes invalid.
1395 @deftypefn {C Function} void scm_bitvector_release (SCM vec)
1396 Finish the access to the elements of a bitvector, as exlained above.
1399 @deftypefn {C Function} void scm_frame_bitvector_release (SCM vec)
1400 Arrange for @code{scm_bitvector_release} to be called with @var{vec}
1401 when the current frame is unwound, implicitely or explicitely;
1405 @deffn {Scheme Procedure} bit-count bool bitvector
1406 @deffnx {C Function} scm_bit_count (bool, bitvector)
1407 Return a count of how many entries in @var{bitvector} are equal to
1408 @var{bool}. For example,
1411 (bit-count #f #*000111000) @result{} 6
1415 @deffn {Scheme Procedure} bit-position bool bitvector start
1416 @deffnx {C Function} scm_bit_position (bool, bitvector, start)
1417 Return the index of the first occurrance of @var{bool} in
1418 @var{bitvector}, starting from @var{start}. If there is no @var{bool}
1419 entry between @var{start} and the end of @var{bitvector}, then return
1420 @code{#f}. For example,
1423 (bit-position #t #*000101 0) @result{} 3
1424 (bit-position #f #*0001111 3) @result{} #f
1428 @deffn {Scheme Procedure} bit-invert! bitvector
1429 @deffnx {C Function} scm_bit_invert_x (bitvector)
1430 Modify @var{bitvector} by replacing each element with its negation.
1433 @deffn {Scheme Procedure} bit-set*! bitvector uvec bool
1434 @deffnx {C Function} scm_bit_set_star_x (bitvector, uvec, bool)
1435 Set entries of @var{bitvector} to @var{bool}, with @var{uvec}
1436 selecting the entries to change. The return value is unspecified.
1438 If @var{uvec} is a bit vector, then those entries where it has
1439 @code{#t} are the ones in @var{bitvector} which are set to @var{bool}.
1440 @var{uvec} and @var{bitvector} must be the same length. When
1441 @var{bool} is @code{#t} it's like @var{uvec} is OR'ed into
1442 @var{bitvector}. Or when @var{bool} is @code{#f} it can be seen as an
1446 (define bv #*01000010)
1447 (bit-set*! bv #*10010001 #t)
1449 @result{} #*11010011
1452 If @var{uvec} is a uniform vector of unsigned long integers, then
1453 they're indexes into @var{bitvector} which are set to @var{bool}.
1456 (define bv #*01000010)
1457 (bit-set*! bv #u(5 2 7) #t)
1459 @result{} #*01100111
1463 @deffn {Scheme Procedure} bit-count* bitvector uvec bool
1464 @deffnx {C Function} scm_bit_count_star (bitvector, uvec, bool)
1465 Return a count of how many entries in @var{bitvector} are equal to
1466 @var{bool}, with @var{uvec} selecting the entries to consider.
1468 @var{uvec} is interpreted in the same way as for @code{bit-set*!}
1469 above. Namely, if @var{uvec} is a bit vector then entries which have
1470 @code{#t} there are considered in @var{bitvector}. Or if @var{uvec}
1471 is a uniform vector of unsigned long integers then it's the indexes in
1472 @var{bitvector} to consider.
1477 (bit-count* #*01110111 #*11001101 #t) @result{} 3
1478 (bit-count* #*01110111 #u(7 0 4) #f) @result{} 2
1483 @node Generalized Vectors
1484 @subsection Generalized Vectors
1486 Guile has a number of data types that are generally vector-like:
1487 strings, uniform numeric vectors, bitvectors, and of course ordinary
1488 vectors of arbitrary Scheme values. These types are disjoint: a
1489 Scheme value belongs to at most one of the four types listed above.
1491 If you want to gloss over this distinction and want to treat all four
1492 types with common code, you can use the procedures in this section.
1493 They work with the @emph{generalized vector} type, which is the union
1494 of the four vector-like types.
1496 Generalized vectors play an important role as the underlying storage
1497 for arrays, @xref{Arrays}.
1499 @deffn {Scheme Procedure} generalized-vector? obj
1500 @deffnx {C Function} scm_generalized_vector_p (obj)
1501 Return @code{#t} if @var{obj} is a vector, string,
1502 bitvector, or uniform numeric vector.
1505 @deffn {Scheme Procedure} generalized-vector-length v
1506 @deffnx {C Function} scm_generalized_vector_length (v)
1507 Return the length of the generalized vector @var{v}.
1510 @deffn {Scheme Procedure} generalized-vector-ref v idx
1511 @deffnx {C Function} scm_generalized_vector_ref (v, idx)
1512 Return the element at index @var{idx} of the
1513 generalized vector @var{v}.
1516 @deffn {Scheme Procedure} generalized-vector-set! v idx val
1517 @deffnx {C Function} scm_generalized_vector_set_x (v, idx, val)
1518 Set the element at index @var{idx} of the
1519 generalized vector @var{v} to @var{val}.
1522 @deffn {Scheme Procedure} generalized-vector->list v
1523 @deffnx {C Function} scm_generalized_vector_to_list (v)
1524 Return a new list whose elements are the elements of the
1525 generalized vector @var{v}.
1528 @deftypefn {C Function} int scm_is_generalized_vector (SCM obj)
1529 Return @code{1} if @var{obj} is a vector, string,
1530 bitvector, or uniform numeric vector; else return @code{0}.
1533 @deftypefn {C Function} size_t scm_c_generalized_vector_length (SCM v)
1534 Return the length of the generalized vector @var{v}.
1537 @deftypefn {C Function} SCM scm_c_generalized_vector_ref (SCM v, size_t idx)
1538 Return the element at index @var{idx} of the generalized vector @var{v}.
1541 @deftypefn {C Function} void scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
1542 Set the element at index @var{idx} of the generalized vector @var{v}
1550 @dfn{Arrays} are a collection of cells organized into an arbitrary
1551 number of dimensions. Each cell can be accessed in constant time by
1552 supplying an index for each dimension.
1554 An array uses a generalized vector for the actual storage of its
1555 elements. Any kind of generalized vector will do, so you can have
1556 arrays of uniform numeric values, arrays of characters, arrays of bits,
1557 and of course arrays of arbitrary Scheme values. For example, arrays
1558 with an underlying @code{c64vector} might be nice for digital signal
1559 processing, while arrays made from a @code{u8vector} might be used to
1560 hold gray-scale images.
1562 The number of dimensions of an array is called its @dfn{rank}. Thus, a
1563 matrix is an array of rank 2, while a vector has rank 1. When accessing
1564 an array element, you have to specify one exact integer for each
1565 dimension. These integers are called the @dfn{indices} of the element.
1566 An array specifies the allowed range of indices for each dimension via a
1567 inclusive lower and upper bound. These bounds can well be negative, but
1568 the upper bound must be at grater than or equal to the lower bound.
1569 When all lower bounds of an array are zero, it is called a
1570 @dfn{zero-origin} array.
1572 For example, ordinary vectors can be regarded as rank 1, zero-origin
1575 An array is displayed as @code{#} followed by its rank, followed by a
1576 tag that describes the underlying vector, optionally followed by the
1577 lower bounds, and finally followed by the cells, organized into
1578 dimensions using parentheses.
1580 In more words, the array tag is of the form
1583 #<rank><vectag><@@lower><@@lower>...
1586 where @code{<rank>} is a positive integer in decimal giving the rank of
1587 the array. It is omitted when the rank is 1 and the array is non-shared
1588 and has zero-origin (see below). For shared arrays and for a non-zero
1589 origin, the rank is always printed even when it is 1 to dinstinguish
1590 them from ordinary vectors.
1592 The @code{<vectag>} part is the tag for a uniform numeric vector, like
1593 @code{u8}, @code{s16}, etc, @code{b} for bitvectors, or @code{a} for
1594 strings. It is empty for ordinary vectors.
1596 The @code{<@@lower>} part is a @samp{@@} sign followed by an integer in
1597 decimal giving the lower bound of a dimension. There is one
1598 @code{<@@lower>} for each dimension. When all lower bounds are zero,
1599 all @code{<@@lower>} parts are omitted.
1605 is an ordinary array of rank 1 with lower bound 0 in dimension 0.
1606 (I.e., a regular vector.)
1609 is an ordinary array of rank 1 with lower bound 2 in dimension 0.
1611 @item #2((1 2 3) (4 5 6))
1612 is a non-uniform array of rank 2; a 3@cross{}3 matrix with index ranges 0..2
1616 is a uniform u8 array of rank 1.
1618 @item #2u32@@2@@3((1 2) (2 3))
1619 is a uniform u8 array of rank 2 with index ranges 2..3 and 3..4.
1624 When an array is created, the range of each dimension must be
1625 specified, e.g., to create a 2@cross{}3 array with a zero-based index:
1628 (make-array 'ho 2 3) @result{} #2((ho ho ho) (ho ho ho))
1631 The range of each dimension can also be given explicitly, e.g., another
1632 way to create the same array:
1635 (make-array 'ho '(0 1) '(0 2)) @result{} #2((ho ho ho) (ho ho ho))
1638 The following procedures can be used with arrays (or vectors). An
1639 argument shown as @var{idx}@dots{} means one parameter for each
1640 dimension in the array. A @var{idxlist} argument means a list of such
1641 values, one for each dimension.
1644 @deffn {Scheme Procedure} array? obj [creator]
1645 @deffnx {C Function} scm_array_p (obj, creator)
1646 Return @code{#t} if the @var{obj} is an array, and @code{#f} if
1649 When @var{creator} is given, @code{#t} is only returned when it is
1650 @code{eq?} to @code{(array-creator @var{obj})}.
1653 @deffn {Scheme Procedure} make-array fill bound @dots{}
1654 Equivalent to @code{(make-array* make-vector @var{fill}
1658 @deffn {Scheme Procedure} make-array* creator fill bound @dots{}
1659 Create and return an array that has as many dimensions as there are
1660 @var{bound}s and (maybe) fill it with @var{fill}.
1662 The underlaying storage vector is created by @var{creator}, which must
1663 be a procedure that returns a generalized vector. Examples include
1664 @code{make-vector} for ordinary arrays, @code{make-string} for arrays of
1665 characters, and @code{make-u8vector} for arrays of unsigned 8-bit
1668 Ordinary arrays are filled with @var{fill}. Other types of arrays are
1669 only then filled with @var{fill} when @var{fill} is non-@nicode{#f}.
1671 Each @var{bound} may be a positive non-zero integer @var{N}, in which
1672 case the index for that dimension can range from 0 through @var{N-1}; or
1673 an explicit index range specifier in the form @code{(LOWER UPPER)},
1674 where both @var{lower} and @var{upper} are integers, possibly less than
1675 zero, and possibly the same number (however, @var{lower} cannot be
1676 greater than @var{upper}).
1679 @deffn {Scheme Procedure} list->array dimspec list
1680 Equivalent to @code{(list->array* make-vector @var{dimspec}
1684 @deffn {Scheme Procedure} list->array* creator dimspec list
1685 @deffnx {C Function} scm_list_to_array_star (creator, dimspec, list)
1686 Return an array of the type indicated by @var{creator} with elements the
1687 same as those of @var{list}.
1689 The argument @var{dimspec} determines the number of dimensions of the
1690 array and their lower bounds. When @var{dimspec} is an exact integer,
1691 it gives the number of dimensions directly and all lower bounds are
1692 zero. When it is a list of exact integers, then each element is the
1693 lower index bound of a dimension, and there will be as many dimensions
1694 as elements in the list.
1697 @deffn {Scheme Procedure} array-creator array
1698 Return a procedure that can be used with @code{make-array*}
1699 to create an array of the same kind as @var{array}.
1701 Note that @code{array-creator} does not necessarily return the exact
1702 procedure passed to @code{make-array*} when creating @var{array}.
1705 @deffn {Scheme Procedure} array-ref array idx @dots{}
1706 Return the element at @code{(idx @dots{})} in @var{array}.
1709 (define a (make-array 999 '(1 2) '(3 4)))
1710 (array-ref a 2 4) @result{} 999
1714 @deffn {Scheme Procedure} array-in-bounds? array idx @dots{}
1715 @deffnx {C Function} scm_array_in_bounds_p (array, idxlist)
1716 Return @code{#t} if the given index would be acceptable to
1720 (define a (make-array #f '(1 2) '(3 4)))
1721 (array-in-bounds? a 2 3) @result{} #f
1722 (array-in-bounds? a 0 0) @result{} #f
1726 @deffn {Scheme Procedure} array-set! array obj idx @dots{}
1727 @deffnx {C Function} scm_array_set_x (array, obj, idxlist)
1728 Set the element at @code{(idx @dots{})} in @var{array} to @var{obj}.
1729 The return value is unspecified.
1732 (define a (make-array #f '(0 1) '(0 1)))
1733 (array-set! a #t 1 1)
1734 a @result{} #2((#f #f) (#f #t))
1738 @deffn {Scheme Procedure} make-shared-array oldarray mapfunc bound @dots{}
1739 @deffnx {C Function} scm_make_shared_array (oldarray, mapfunc, boundlist)
1740 @code{make-shared-array} can be used to create shared subarrays of other
1741 arrays. The @var{mapper} is a function that translates coordinates in
1742 the new array into coordinates in the old array. A @var{mapper} must be
1743 affine, and its range must stay within the bounds of the old array, but
1744 it can be otherwise arbitrary. A simple example:
1747 (define fred (make-array #f 8 8))
1748 (define freds-diagonal
1749 (make-shared-array fred (lambda (i) (list i i)) 8))
1750 (array-set! freds-diagonal 'foo 3)
1751 (array-ref fred 3 3) @result{} foo
1752 (define freds-center
1753 (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))
1754 (array-ref freds-center 0 0) @result{} foo
1758 @deffn {Scheme Procedure} shared-array-increments array
1759 @deffnx {C Function} scm_shared_array_increments (array)
1760 For each dimension, return the distance between elements in the root vector.
1763 @deffn {Scheme Procedure} shared-array-offset array
1764 @deffnx {C Function} scm_shared_array_offset (array)
1765 Return the root vector index of the first element in the array.
1768 @deffn {Scheme Procedure} shared-array-root array
1769 @deffnx {C Function} scm_shared_array_root (array)
1770 Return the root vector of a shared array.
1773 @deffn {Scheme Procedure} transpose-array array dim1 @dots{}
1774 @deffnx {C Function} scm_transpose_array (array, dimlist)
1775 Return an array sharing contents with @var{array}, but with
1776 dimensions arranged in a different order. There must be one
1777 @var{dim} argument for each dimension of @var{array}.
1778 @var{dim1}, @var{dim2}, @dots{} should be integers between 0
1779 and the rank of the array to be returned. Each integer in that
1780 range must appear at least once in the argument list.
1782 The values of @var{dim1}, @var{dim2}, @dots{} correspond to
1783 dimensions in the array to be returned, and their positions in the
1784 argument list to dimensions of @var{array}. Several @var{dim}s
1785 may have the same value, in which case the returned array will
1786 have smaller rank than @var{array}.
1789 (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
1790 (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
1791 (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
1792 #2((a 4) (b 5) (c 6))
1796 @deffn {Scheme Procedure} enclose-array array dim1 @dots{}
1797 @deffnx {C Function} scm_enclose_array (array, dimlist)
1798 @var{dim1}, @var{dim2} @dots{} should be nonnegative integers less than
1799 the rank of @var{array}. @code{enclose-array} returns an array
1800 resembling an array of shared arrays. The dimensions of each shared
1801 array are the same as the @var{dim}th dimensions of the original array,
1802 the dimensions of the outer array are the same as those of the original
1803 array that did not match a @var{dim}.
1805 An enclosed array is not a general Scheme array. Its elements may not
1806 be set using @code{array-set!}. Two references to the same element of
1807 an enclosed array will be @code{equal?} but will not in general be
1808 @code{eq?}. The value returned by @code{array-prototype} when given an
1809 enclosed array is unspecified.
1814 (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1)
1816 #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>
1818 (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0)
1820 #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>
1824 @deffn {Scheme Procedure} array-shape array
1825 @deffnx {Scheme Procedure} array-dimensions array
1826 @deffnx {C Function} scm_array_dimensions (array)
1827 Return a list of the bounds for each dimenson of @var{array}.
1829 @code{array-shape} gives @code{(@var{lower} @var{upper})} for each
1830 dimension. @code{array-dimensions} instead returns just
1831 @math{@var{upper}+1} for dimensions with a 0 lower bound. Both are
1832 suitable as input to @code{make-array}.
1837 (define a (make-array 'foo '(-1 3) 5))
1838 (array-shape a) @result{} ((-1 3) (0 4))
1839 (array-dimensions a) @result{} ((-1 3) 5)
1843 @deffn {Scheme Procedure} array-rank obj
1844 @deffnx {C Function} scm_array_rank (obj)
1845 Return the number of dimensions of an array @var{obj}, or if @var{obj}
1846 is not an array then return 0.
1849 @deffn {Scheme Procedure} array->list array
1850 @deffnx {C Function} scm_array_to_list (array)
1851 Return a list consisting of all the elements, in order, of
1855 @c FIXME: Describe how the order affects the copying (it matters for
1856 @c shared arrays with the same underlying root vector, presumably).
1858 @deffn {Scheme Procedure} array-copy! src dst
1859 @deffnx {Scheme Procedure} array-copy-in-order! src dst
1860 @deffnx {C Function} scm_array_copy_x (src, dst)
1861 Copy every element from vector or array @var{src} to the corresponding
1862 element of @var{dst}. @var{dst} must have the same rank as @var{src},
1863 and be at least as large in each dimension. The return value is
1867 @deffn {Scheme Procedure} array-fill! array fill
1868 @deffnx {C Function} scm_array_fill_x (array, fill)
1869 Store @var{fill} in every element of @var{array}. The value returned
1873 @c begin (texi-doc-string "guile" "array-equal?")
1874 @deffn {Scheme Procedure} array-equal? array1 array2 @dots{}
1875 Return @code{#t} if all arguments are arrays with the same shape, the
1876 same type, and have corresponding elements which are either
1877 @code{equal?} or @code{array-equal?}. This function differs from
1878 @code{equal?} in that a one dimensional shared array may be
1879 @var{array-equal?} but not @var{equal?} to a vector or uniform vector.
1882 @deffn {Scheme Procedure} array-contents array [strict]
1883 @deffnx {C Function} scm_array_contents (array, strict)
1884 If @var{array} may be @dfn{unrolled} into a one dimensional shared array
1885 without changing their order (last subscript changing fastest), then
1886 @code{array-contents} returns that shared array, otherwise it returns
1887 @code{#f}. All arrays made by @code{make-array} and
1888 @code{make-generalized-array} may be unrolled, some arrays made by
1889 @code{make-shared-array} may not be.
1891 If the optional argument @var{strict} is provided, a shared array will
1892 be returned only if its elements are stored internally contiguous in
1896 @c FIXME: array-map! accepts no source arrays at all, and in that
1897 @c case makes calls "(proc)". Is that meant to be a documented
1900 @c FIXME: array-for-each doesn't say what happens if the sources have
1901 @c different index ranges. The code currently iterates over the
1902 @c indices of the first and expects the others to cover those. That
1903 @c at least vaguely matches array-map!, but is is meant to be a
1904 @c documented feature?
1906 @deffn {Scheme Procedure} array-map! dst proc src1 @dots{} srcN
1907 @deffnx {Scheme Procedure} array-map-in-order! dst proc src1 @dots{} srcN
1908 @deffnx {C Function} scm_array_map_x (dst, proc, srclist)
1909 Set each element of the @var{dst} array to values obtained from calls
1910 to @var{proc}. The value returned is unspecified.
1912 Each call is @code{(@var{proc} @var{elem1} @dots{} @var{elemN})},
1913 where each @var{elem} is from the corresponding @var{src} array, at
1914 the @var{dst} index. @code{array-map-in-order!} makes the calls in
1915 row-major order, @code{array-map!} makes them in an unspecified order.
1917 The @var{src} arrays must have the same number of dimensions as
1918 @var{dst}, and must have a range for each dimension which covers the
1919 range in @var{dst}. This ensures all @var{dst} indices are valid in
1923 @deffn {Scheme Procedure} array-for-each proc src1 @dots{} srcN
1924 @deffnx {C Function} scm_array_for_each (proc, src1, srclist)
1925 Apply @var{proc} to each tuple of elements of @var{src1} @dots{}
1926 @var{srcN}, in row-major order. The value returned is unspecified.
1929 @deffn {Scheme Procedure} array-index-map! dst proc
1930 @deffnx {C Function} scm_array_index_map_x (dst, proc)
1931 Set each element of the @var{dst} array to values returned by calls to
1932 @var{proc}. The value returned is unspecified.
1934 Each call is @code{(@var{proc} @var{i1} @dots{} @var{iN})}, where
1935 @var{i1}@dots{}@var{iN} is the destination index, one parameter for
1936 each dimension. The order in which the calls are made is unspecified.
1938 For example, to create a @m{4\times4, 4x4} matrix representing a
1942 \advance\leftskip by 2\lispnarrowing {
1960 (define a (make-array #f 4 4))
1961 (array-index-map! a (lambda (i j)
1962 (modulo (+ i j) 4)))
1966 @deffn {Scheme Procedure} uniform-array-read! ra [port_or_fd [start [end]]]
1967 @deffnx {C Function} scm_uniform_array_read_x (ra, port_or_fd, start, end)
1968 Attempt to read all elements of @var{ura}, in lexicographic order, as
1969 binary objects from @var{port-or-fdes}.
1970 If an end of file is encountered,
1971 the objects up to that point are put into @var{ura}
1972 (starting at the beginning) and the remainder of the array is
1975 The optional arguments @var{start} and @var{end} allow
1976 a specified region of a vector (or linearized array) to be read,
1977 leaving the remainder of the vector unchanged.
1979 @code{uniform-array-read!} returns the number of objects read.
1980 @var{port-or-fdes} may be omitted, in which case it defaults to the value
1981 returned by @code{(current-input-port)}.
1984 @deffn {Scheme Procedure} uniform-array-write v [port_or_fd [start [end]]]
1985 @deffnx {C Function} scm_uniform_array_write (v, port_or_fd, start, end)
1986 Writes all elements of @var{ura} as binary objects to
1989 The optional arguments @var{start}
1991 a specified region of a vector (or linearized array) to be written.
1993 The number of objects actually written is returned.
1994 @var{port-or-fdes} may be
1995 omitted, in which case it defaults to the value returned by
1996 @code{(current-output-port)}.
2002 A @dfn{record type} is a first class object representing a user-defined
2003 data type. A @dfn{record} is an instance of a record type.
2005 @deffn {Scheme Procedure} record? obj
2006 Return @code{#t} if @var{obj} is a record of any type and @code{#f}
2009 Note that @code{record?} may be true of any Scheme value; there is no
2010 promise that records are disjoint with other Scheme types.
2013 @deffn {Scheme Procedure} make-record-type type-name field-names
2014 Return a @dfn{record-type descriptor}, a value representing a new data
2015 type disjoint from all others. The @var{type-name} argument must be a
2016 string, but is only used for debugging purposes (such as the printed
2017 representation of a record of the new type). The @var{field-names}
2018 argument is a list of symbols naming the @dfn{fields} of a record of the
2019 new type. It is an error if the list contains any duplicates. It is
2020 unspecified how record-type descriptors are represented.
2023 @deffn {Scheme Procedure} record-constructor rtd [field-names]
2024 Return a procedure for constructing new members of the type represented
2025 by @var{rtd}. The returned procedure accepts exactly as many arguments
2026 as there are symbols in the given list, @var{field-names}; these are
2027 used, in order, as the initial values of those fields in a new record,
2028 which is returned by the constructor procedure. The values of any
2029 fields not named in that list are unspecified. The @var{field-names}
2030 argument defaults to the list of field names in the call to
2031 @code{make-record-type} that created the type represented by @var{rtd};
2032 if the @var{field-names} argument is provided, it is an error if it
2033 contains any duplicates or any symbols not in the default list.
2036 @deffn {Scheme Procedure} record-predicate rtd
2037 Return a procedure for testing membership in the type represented by
2038 @var{rtd}. The returned procedure accepts exactly one argument and
2039 returns a true value if the argument is a member of the indicated record
2040 type; it returns a false value otherwise.
2043 @deffn {Scheme Procedure} record-accessor rtd field-name
2044 Return a procedure for reading the value of a particular field of a
2045 member of the type represented by @var{rtd}. The returned procedure
2046 accepts exactly one argument which must be a record of the appropriate
2047 type; it returns the current value of the field named by the symbol
2048 @var{field-name} in that record. The symbol @var{field-name} must be a
2049 member of the list of field-names in the call to @code{make-record-type}
2050 that created the type represented by @var{rtd}.
2053 @deffn {Scheme Procedure} record-modifier rtd field-name
2054 Return a procedure for writing the value of a particular field of a
2055 member of the type represented by @var{rtd}. The returned procedure
2056 accepts exactly two arguments: first, a record of the appropriate type,
2057 and second, an arbitrary Scheme value; it modifies the field named by
2058 the symbol @var{field-name} in that record to contain the given value.
2059 The returned value of the modifier procedure is unspecified. The symbol
2060 @var{field-name} must be a member of the list of field-names in the call
2061 to @code{make-record-type} that created the type represented by
2065 @deffn {Scheme Procedure} record-type-descriptor record
2066 Return a record-type descriptor representing the type of the given
2067 record. That is, for example, if the returned descriptor were passed to
2068 @code{record-predicate}, the resulting predicate would return a true
2069 value when passed the given record. Note that it is not necessarily the
2070 case that the returned descriptor is the one that was passed to
2071 @code{record-constructor} in the call that created the constructor
2072 procedure that created the given record.
2075 @deffn {Scheme Procedure} record-type-name rtd
2076 Return the type-name associated with the type represented by rtd. The
2077 returned value is @code{eqv?} to the @var{type-name} argument given in
2078 the call to @code{make-record-type} that created the type represented by
2082 @deffn {Scheme Procedure} record-type-fields rtd
2083 Return a list of the symbols naming the fields in members of the type
2084 represented by @var{rtd}. The returned value is @code{equal?} to the
2085 field-names argument given in the call to @code{make-record-type} that
2086 created the type represented by @var{rtd}.
2091 @subsection Structures
2094 [FIXME: this is pasted in from Tom Lord's original guile.texi and should
2097 A @dfn{structure type} is a first class user-defined data type. A
2098 @dfn{structure} is an instance of a structure type. A structure type is
2101 Structures are less abstract and more general than traditional records.
2102 In fact, in Guile Scheme, records are implemented using structures.
2105 * Structure Concepts:: The structure of Structures
2106 * Structure Layout:: Defining the layout of structure types
2107 * Structure Basics:: make-, -ref and -set! procedures for structs
2108 * Vtables:: Accessing type-specific data
2111 @node Structure Concepts
2112 @subsubsection Structure Concepts
2114 A structure object consists of a handle, structure data, and a vtable.
2115 The handle is a Scheme value which points to both the vtable and the
2116 structure's data. Structure data is a dynamically allocated region of
2117 memory, private to the structure, divided up into typed fields. A
2118 vtable is another structure used to hold type-specific data. Multiple
2119 structures can share a common vtable.
2121 Three concepts are key to understanding structures.
2124 @item @dfn{layout specifications}
2126 Layout specifications determine how memory allocated to structures is
2127 divided up into fields. Programmers must write a layout specification
2128 whenever a new type of structure is defined.
2130 @item @dfn{structural accessors}
2132 Structure access is by field number. There is only one set of
2133 accessors common to all structure objects.
2137 Vtables, themselves structures, are first class representations of
2138 disjoint sub-types of structures in general. In most cases, when a
2139 new structure is created, programmers must specify a vtable for the
2140 new structure. Each vtable has a field describing the layout of its
2141 instances. Vtables can have additional, user-defined fields as well.
2146 @node Structure Layout
2147 @subsubsection Structure Layout
2149 When a structure is created, a region of memory is allocated to hold its
2150 state. The @dfn{layout} of the structure's type determines how that
2151 memory is divided into fields.
2153 Each field has a specified type. There are only three types allowed, each
2154 corresponding to a one letter code. The allowed types are:
2157 @item 'u' -- unprotected
2159 The field holds binary data that is not GC protected.
2161 @item 'p' -- protected
2163 The field holds a Scheme value and is GC protected.
2167 The field holds a Scheme value and is GC protected. When a structure is
2168 created with this type of field, the field is initialized to refer to
2169 the structure's own handle. This kind of field is mainly useful when
2170 mixing Scheme and C code in which the C code may need to compute a
2171 structure's handle given only the address of its malloc'd data.
2175 Each field also has an associated access protection. There are only
2176 three kinds of protection, each corresponding to a one letter code.
2177 The allowed protections are:
2180 @item 'w' -- writable
2182 The field can be read and written.
2184 @item 'r' -- readable
2186 The field can be read, but not written.
2190 The field can be neither read nor written. This kind
2191 of protection is for fields useful only to built-in routines.
2194 A layout specification is described by stringing together pairs
2195 of letters: one to specify a field type and one to specify a field
2196 protection. For example, a traditional cons pair type object could
2200 ; cons pairs have two writable fields of Scheme data
2204 A pair object in which the first field is held constant could be:
2210 Binary fields, (fields of type "u"), hold one @dfn{word} each. The
2211 size of a word is a machine dependent value defined to be equal to the
2212 value of the C expression: @code{sizeof (long)}.
2214 The last field of a structure layout may specify a tail array.
2215 A tail array is indicated by capitalizing the field's protection
2216 code ('W', 'R' or 'O'). A tail-array field is replaced by
2217 a read-only binary data field containing an array size. The array
2218 size is determined at the time the structure is created. It is followed
2219 by a corresponding number of fields of the type specified for the
2220 tail array. For example, a conventional Scheme vector can be
2224 ; A vector is an arbitrary number of writable fields holding Scheme
2229 In the above example, field 0 contains the size of the vector and
2230 fields beginning at 1 contain the vector elements.
2232 A kind of tagged vector (a constant tag followed by conventional
2233 vector elements) might be:
2240 Structure layouts are represented by specially interned symbols whose
2241 name is a string of type and protection codes. To create a new
2242 structure layout, use this procedure:
2244 @deffn {Scheme Procedure} make-struct-layout fields
2245 @deffnx {C Function} scm_make_struct_layout (fields)
2246 Return a new structure layout object.
2248 @var{fields} must be a string made up of pairs of characters
2249 strung together. The first character of each pair describes a field
2250 type, the second a field protection. Allowed types are 'p' for
2251 GC-protected Scheme data, 'u' for unprotected binary data, and 's' for
2252 a field that points to the structure itself. Allowed protections
2253 are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque
2254 fields. The last field protection specification may be capitalized to
2255 indicate that the field is a tail-array.
2260 @node Structure Basics
2261 @subsubsection Structure Basics
2263 This section describes the basic procedures for creating and accessing
2266 @deffn {Scheme Procedure} make-struct vtable tail_array_size . init
2267 @deffnx {C Function} scm_make_struct (vtable, tail_array_size, init)
2268 Create a new structure.
2270 @var{type} must be a vtable structure (@pxref{Vtables}).
2272 @var{tail-elts} must be a non-negative integer. If the layout
2273 specification indicated by @var{type} includes a tail-array,
2274 this is the number of elements allocated to that array.
2276 The @var{init1}, @dots{} are optional arguments describing how
2277 successive fields of the structure should be initialized. Only fields
2278 with protection 'r' or 'w' can be initialized, except for fields of
2279 type 's', which are automatically initialized to point to the new
2280 structure itself; fields with protection 'o' can not be initialized by
2283 If fewer optional arguments than initializable fields are supplied,
2284 fields of type 'p' get default value #f while fields of type 'u' are
2287 Structs are currently the basic representation for record-like data
2288 structures in Guile. The plan is to eventually replace them with a
2289 new representation which will at the same time be easier to use and
2292 For more information, see the documentation for @code{make-vtable-vtable}.
2295 @deffn {Scheme Procedure} struct? x
2296 @deffnx {C Function} scm_struct_p (x)
2297 Return @code{#t} iff @var{x} is a structure object, else
2302 @deffn {Scheme Procedure} struct-ref handle pos
2303 @deffnx {Scheme Procedure} struct-set! struct n value
2304 @deffnx {C Function} scm_struct_ref (handle, pos)
2305 @deffnx {C Function} scm_struct_set_x (struct, n, value)
2306 Access (or modify) the @var{n}th field of @var{struct}.
2308 If the field is of type 'p', then it can be set to an arbitrary value.
2310 If the field is of type 'u', then it can only be set to a non-negative
2311 integer value small enough to fit in one machine word.
2317 @subsubsection Vtables
2319 Vtables are structures that are used to represent structure types. Each
2320 vtable contains a layout specification in field
2321 @code{vtable-index-layout} -- instances of the type are laid out
2322 according to that specification. Vtables contain additional fields
2323 which are used only internally to libguile. The variable
2324 @code{vtable-offset-user} is bound to a field number. Vtable fields
2325 at that position or greater are user definable.
2327 @deffn {Scheme Procedure} struct-vtable handle
2328 @deffnx {C Function} scm_struct_vtable (handle)
2329 Return the vtable structure that describes the type of @var{struct}.
2332 @deffn {Scheme Procedure} struct-vtable? x
2333 @deffnx {C Function} scm_struct_vtable_p (x)
2334 Return @code{#t} iff @var{x} is a vtable structure.
2337 If you have a vtable structure, @code{V}, you can create an instance of
2338 the type it describes by using @code{(make-struct V ...)}. But where
2339 does @code{V} itself come from? One possibility is that @code{V} is an
2340 instance of a user-defined vtable type, @code{V'}, so that @code{V} is
2341 created by using @code{(make-struct V' ...)}. Another possibility is
2342 that @code{V} is an instance of the type it itself describes. Vtable
2343 structures of the second sort are created by this procedure:
2345 @deffn {Scheme Procedure} make-vtable-vtable user_fields tail_array_size . init
2346 @deffnx {C Function} scm_make_vtable_vtable (user_fields, tail_array_size, init)
2347 Return a new, self-describing vtable structure.
2349 @var{user-fields} is a string describing user defined fields of the
2350 vtable beginning at index @code{vtable-offset-user}
2351 (see @code{make-struct-layout}).
2353 @var{tail-size} specifies the size of the tail-array (if any) of
2356 @var{init1}, @dots{} are the optional initializers for the fields of
2359 Vtables have one initializable system field---the struct printer.
2360 This field comes before the user fields in the initializers passed
2361 to @code{make-vtable-vtable} and @code{make-struct}, and thus works as
2362 a third optional argument to @code{make-vtable-vtable} and a fourth to
2363 @code{make-struct} when creating vtables:
2365 If the value is a procedure, it will be called instead of the standard
2366 printer whenever a struct described by this vtable is printed.
2367 The procedure will be called with arguments STRUCT and PORT.
2369 The structure of a struct is described by a vtable, so the vtable is
2370 in essence the type of the struct. The vtable is itself a struct with
2371 a vtable. This could go on forever if it weren't for the
2372 vtable-vtables which are self-describing vtables, and thus terminate
2375 There are several potential ways of using structs, but the standard
2376 one is to use three kinds of structs, together building up a type
2377 sub-system: one vtable-vtable working as the root and one or several
2378 "types", each with a set of "instances". (The vtable-vtable should be
2379 compared to the class <class> which is the class of itself.)
2382 (define ball-root (make-vtable-vtable "pr" 0))
2384 (define (make-ball-type ball-color)
2385 (make-struct ball-root 0
2386 (make-struct-layout "pw")
2388 (format port "#<a ~A ball owned by ~A>"
2392 (define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))
2393 (define (owner ball) (struct-ref ball 0))
2395 (define red (make-ball-type 'red))
2396 (define green (make-ball-type 'green))
2398 (define (make-ball type owner) (make-struct type 0 owner))
2400 (define ball (make-ball green 'Nisse))
2401 ball @result{} #<a green ball owned by Nisse>
2405 @deffn {Scheme Procedure} struct-vtable-name vtable
2406 @deffnx {C Function} scm_struct_vtable_name (vtable)
2407 Return the name of the vtable @var{vtable}.
2410 @deffn {Scheme Procedure} set-struct-vtable-name! vtable name
2411 @deffnx {C Function} scm_set_struct_vtable_name_x (vtable, name)
2412 Set the name of the vtable @var{vtable} to @var{name}.
2415 @deffn {Scheme Procedure} struct-vtable-tag handle
2416 @deffnx {C Function} scm_struct_vtable_tag (handle)
2417 Return the vtable tag of the structure @var{handle}.
2421 @node Dictionary Types
2422 @subsection Dictionary Types
2424 A @dfn{dictionary} object is a data structure used to index
2425 information in a user-defined way. In standard Scheme, the main
2426 aggregate data types are lists and vectors. Lists are not really
2427 indexed at all, and vectors are indexed only by number
2428 (e.g. @code{(vector-ref foo 5)}). Often you will find it useful
2429 to index your data on some other type; for example, in a library
2430 catalog you might want to look up a book by the name of its
2431 author. Dictionaries are used to help you organize information in
2434 An @dfn{association list} (or @dfn{alist} for short) is a list of
2435 key-value pairs. Each pair represents a single quantity or
2436 object; the @code{car} of the pair is a key which is used to
2437 identify the object, and the @code{cdr} is the object's value.
2439 A @dfn{hash table} also permits you to index objects with
2440 arbitrary keys, but in a way that makes looking up any one object
2441 extremely fast. A well-designed hash system makes hash table
2442 lookups almost as fast as conventional array or vector references.
2444 Alists are popular among Lisp programmers because they use only
2445 the language's primitive operations (lists, @dfn{car}, @dfn{cdr}
2446 and the equality primitives). No changes to the language core are
2447 necessary. Therefore, with Scheme's built-in list manipulation
2448 facilities, it is very convenient to handle data stored in an
2449 association list. Also, alists are highly portable and can be
2450 easily implemented on even the most minimal Lisp systems.
2452 However, alists are inefficient, especially for storing large
2453 quantities of data. Because we want Guile to be useful for large
2454 software systems as well as small ones, Guile provides a rich set
2455 of tools for using either association lists or hash tables.
2457 @node Association Lists
2458 @subsection Association Lists
2459 @tpindex Association Lists
2462 @cindex Association List
2466 An association list is a conventional data structure that is often used
2467 to implement simple key-value databases. It consists of a list of
2468 entries in which each entry is a pair. The @dfn{key} of each entry is
2469 the @code{car} of the pair and the @dfn{value} of each entry is the
2473 ASSOCIATION LIST ::= '( (KEY1 . VALUE1)
2481 Association lists are also known, for short, as @dfn{alists}.
2483 The structure of an association list is just one example of the infinite
2484 number of possible structures that can be built using pairs and lists.
2485 As such, the keys and values in an association list can be manipulated
2486 using the general list structure procedures @code{cons}, @code{car},
2487 @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However,
2488 because association lists are so useful, Guile also provides specific
2489 procedures for manipulating them.
2492 * Alist Key Equality::
2493 * Adding or Setting Alist Entries::
2494 * Retrieving Alist Entries::
2495 * Removing Alist Entries::
2496 * Sloppy Alist Functions::
2500 @node Alist Key Equality
2501 @subsubsection Alist Key Equality
2503 All of Guile's dedicated association list procedures, apart from
2504 @code{acons}, come in three flavours, depending on the level of equality
2505 that is required to decide whether an existing key in the association
2506 list is the same as the key that the procedure call uses to identify the
2511 Procedures with @dfn{assq} in their name use @code{eq?} to determine key
2515 Procedures with @dfn{assv} in their name use @code{eqv?} to determine
2519 Procedures with @dfn{assoc} in their name use @code{equal?} to
2520 determine key equality.
2523 @code{acons} is an exception because it is used to build association
2524 lists which do not require their entries' keys to be unique.
2526 @node Adding or Setting Alist Entries
2527 @subsubsection Adding or Setting Alist Entries
2529 @code{acons} adds a new entry to an association list and returns the
2530 combined association list. The combined alist is formed by consing the
2531 new entry onto the head of the alist specified in the @code{acons}
2532 procedure call. So the specified alist is not modified, but its
2533 contents become shared with the tail of the combined alist that
2534 @code{acons} returns.
2536 In the most common usage of @code{acons}, a variable holding the
2537 original association list is updated with the combined alist:
2540 (set! address-list (acons name address address-list))
2543 In such cases, it doesn't matter that the old and new values of
2544 @code{address-list} share some of their contents, since the old value is
2545 usually no longer independently accessible.
2547 Note that @code{acons} adds the specified new entry regardless of
2548 whether the alist may already contain entries with keys that are, in
2549 some sense, the same as that of the new entry. Thus @code{acons} is
2550 ideal for building alists where there is no concept of key uniqueness.
2553 (set! task-list (acons 3 "pay gas bill" '()))
2556 ((3 . "pay gas bill"))
2558 (set! task-list (acons 3 "tidy bedroom" task-list))
2561 ((3 . "tidy bedroom") (3 . "pay gas bill"))
2564 @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add
2565 or replace an entry in an association list where there @emph{is} a
2566 concept of key uniqueness. If the specified association list already
2567 contains an entry whose key is the same as that specified in the
2568 procedure call, the existing entry is replaced by the new one.
2569 Otherwise, the new entry is consed onto the head of the old association
2570 list to create the combined alist. In all cases, these procedures
2571 return the combined alist.
2573 @code{assq-set!} and friends @emph{may} destructively modify the
2574 structure of the old association list in such a way that an existing
2575 variable is correctly updated without having to @code{set!} it to the
2581 (("mary" . "34 Elm Road") ("james" . "16 Bow Street"))
2583 (assoc-set! address-list "james" "1a London Road")
2585 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
2589 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
2595 (assoc-set! address-list "bob" "11 Newington Avenue")
2597 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
2598 ("james" . "1a London Road"))
2602 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
2605 The only safe way to update an association list variable when adding or
2606 replacing an entry like this is to @code{set!} the variable to the
2611 (assoc-set! address-list "bob" "11 Newington Avenue"))
2614 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
2615 ("james" . "1a London Road"))
2618 Because of this slight inconvenience, you may find it more convenient to
2619 use hash tables to store dictionary data. If your application will not
2620 be modifying the contents of an alist very often, this may not make much
2623 If you need to keep the old value of an association list in a form
2624 independent from the list that results from modification by
2625 @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!},
2626 use @code{list-copy} to copy the old association list before modifying
2629 @deffn {Scheme Procedure} acons key value alist
2630 @deffnx {C Function} scm_acons (key, value, alist)
2631 Add a new key-value pair to @var{alist}. A new pair is
2632 created whose car is @var{key} and whose cdr is @var{value}, and the
2633 pair is consed onto @var{alist}, and the new list is returned. This
2634 function is @emph{not} destructive; @var{alist} is not modified.
2637 @deffn {Scheme Procedure} assq-set! alist key val
2638 @deffnx {Scheme Procedure} assv-set! alist key value
2639 @deffnx {Scheme Procedure} assoc-set! alist key value
2640 @deffnx {C Function} scm_assq_set_x (alist, key, val)
2641 @deffnx {C Function} scm_assv_set_x (alist, key, val)
2642 @deffnx {C Function} scm_assoc_set_x (alist, key, val)
2643 Reassociate @var{key} in @var{alist} with @var{value}: find any existing
2644 @var{alist} entry for @var{key} and associate it with the new
2645 @var{value}. If @var{alist} does not contain an entry for @var{key},
2646 add a new one. Return the (possibly new) alist.
2648 These functions do not attempt to verify the structure of @var{alist},
2649 and so may cause unusual results if passed an object that is not an
2653 @node Retrieving Alist Entries
2654 @subsubsection Retrieving Alist Entries
2659 @code{assq}, @code{assv} and @code{assoc} take an alist and a key as
2660 arguments and return the entry for that key if an entry exists, or
2661 @code{#f} if there is no entry for that key. Note that, in the cases
2662 where an entry exists, these procedures return the complete entry, that
2663 is @code{(KEY . VALUE)}, not just the value.
2665 @deffn {Scheme Procedure} assq key alist
2666 @deffnx {Scheme Procedure} assv key alist
2667 @deffnx {Scheme Procedure} assoc key alist
2668 @deffnx {C Function} scm_assq (key, alist)
2669 @deffnx {C Function} scm_assv (key, alist)
2670 @deffnx {C Function} scm_assoc (key, alist)
2671 Fetch the entry in @var{alist} that is associated with @var{key}. To
2672 decide whether the argument @var{key} matches a particular entry in
2673 @var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}
2674 uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}
2675 cannot be found in @var{alist} (according to whichever equality
2676 predicate is in use), then return @code{#f}. These functions
2677 return the entire alist entry found (i.e. both the key and the value).
2680 @code{assq-ref}, @code{assv-ref} and @code{assoc-ref}, on the other
2681 hand, take an alist and a key and return @emph{just the value} for that
2682 key, if an entry exists. If there is no entry for the specified key,
2683 these procedures return @code{#f}.
2685 This creates an ambiguity: if the return value is @code{#f}, it means
2686 either that there is no entry with the specified key, or that there
2687 @emph{is} an entry for the specified key, with value @code{#f}.
2688 Consequently, @code{assq-ref} and friends should only be used where it
2689 is known that an entry exists, or where the ambiguity doesn't matter
2690 for some other reason.
2692 @deffn {Scheme Procedure} assq-ref alist key
2693 @deffnx {Scheme Procedure} assv-ref alist key
2694 @deffnx {Scheme Procedure} assoc-ref alist key
2695 @deffnx {C Function} scm_assq_ref (alist, key)
2696 @deffnx {C Function} scm_assv_ref (alist, key)
2697 @deffnx {C Function} scm_assoc_ref (alist, key)
2698 Like @code{assq}, @code{assv} and @code{assoc}, except that only the
2699 value associated with @var{key} in @var{alist} is returned. These
2700 functions are equivalent to
2703 (let ((ent (@var{associator} @var{key} @var{alist})))
2704 (and ent (cdr ent)))
2707 where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.
2710 @node Removing Alist Entries
2711 @subsubsection Removing Alist Entries
2713 To remove the element from an association list whose key matches a
2714 specified key, use @code{assq-remove!}, @code{assv-remove!} or
2715 @code{assoc-remove!} (depending, as usual, on the level of equality
2716 required between the key that you specify and the keys in the
2719 As with @code{assq-set!} and friends, the specified alist may or may not
2720 be modified destructively, and the only safe way to update a variable
2721 containing the alist is to @code{set!} it to the value that
2722 @code{assq-remove!} and friends return.
2727 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
2728 ("james" . "1a London Road"))
2730 (set! address-list (assoc-remove! address-list "mary"))
2733 (("bob" . "11 Newington Avenue") ("james" . "1a London Road"))
2736 Note that, when @code{assq/v/oc-remove!} is used to modify an
2737 association list that has been constructed only using the corresponding
2738 @code{assq/v/oc-set!}, there can be at most one matching entry in the
2739 alist, so the question of multiple entries being removed in one go does
2740 not arise. If @code{assq/v/oc-remove!} is applied to an association
2741 list that has been constructed using @code{acons}, or an
2742 @code{assq/v/oc-set!} with a different level of equality, or any mixture
2743 of these, it removes only the first matching entry from the alist, even
2744 if the alist might contain further matching entries. For example:
2747 (define address-list '())
2748 (set! address-list (assq-set! address-list "mary" "11 Elm Street"))
2749 (set! address-list (assq-set! address-list "mary" "57 Pine Drive"))
2752 (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street"))
2754 (set! address-list (assoc-remove! address-list "mary"))
2757 (("mary" . "11 Elm Street"))
2760 In this example, the two instances of the string "mary" are not the same
2761 when compared using @code{eq?}, so the two @code{assq-set!} calls add
2762 two distinct entries to @code{address-list}. When compared using
2763 @code{equal?}, both "mary"s in @code{address-list} are the same as the
2764 "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops
2765 after removing the first matching entry that it finds, and so one of the
2766 "mary" entries is left in place.
2768 @deffn {Scheme Procedure} assq-remove! alist key
2769 @deffnx {Scheme Procedure} assv-remove! alist key
2770 @deffnx {Scheme Procedure} assoc-remove! alist key
2771 @deffnx {C Function} scm_assq_remove_x (alist, key)
2772 @deffnx {C Function} scm_assv_remove_x (alist, key)
2773 @deffnx {C Function} scm_assoc_remove_x (alist, key)
2774 Delete the first entry in @var{alist} associated with @var{key}, and return
2775 the resulting alist.
2778 @node Sloppy Alist Functions
2779 @subsubsection Sloppy Alist Functions
2781 @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave
2782 like the corresponding non-@code{sloppy-} procedures, except that they
2783 return @code{#f} when the specified association list is not well-formed,
2784 where the non-@code{sloppy-} versions would signal an error.
2786 Specifically, there are two conditions for which the non-@code{sloppy-}
2787 procedures signal an error, which the @code{sloppy-} procedures handle
2788 instead by returning @code{#f}. Firstly, if the specified alist as a
2789 whole is not a proper list:
2792 (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
2794 ERROR: In procedure assoc in expression (assoc "mary" (quote #)):
2795 ERROR: Wrong type argument in position 2 (expecting association list): ((1 . 2) ("key" . "door") . "open sesame")
2797 (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
2803 Secondly, if one of the entries in the specified alist is not a pair:
2806 (assoc 2 '((1 . 1) 2 (3 . 9)))
2808 ERROR: In procedure assoc in expression (assoc 2 (quote #)):
2809 ERROR: Wrong type argument in position 2 (expecting association list): ((1 . 1) 2 (3 . 9))
2811 (sloppy-assoc 2 '((1 . 1) 2 (3 . 9)))
2816 Unless you are explicitly working with badly formed association lists,
2817 it is much safer to use the non-@code{sloppy-} procedures, because they
2818 help to highlight coding and data errors that the @code{sloppy-}
2819 versions would silently cover up.
2821 @deffn {Scheme Procedure} sloppy-assq key alist
2822 @deffnx {C Function} scm_sloppy_assq (key, alist)
2823 Behaves like @code{assq} but does not do any error checking.
2824 Recommended only for use in Guile internals.
2827 @deffn {Scheme Procedure} sloppy-assv key alist
2828 @deffnx {C Function} scm_sloppy_assv (key, alist)
2829 Behaves like @code{assv} but does not do any error checking.
2830 Recommended only for use in Guile internals.
2833 @deffn {Scheme Procedure} sloppy-assoc key alist
2834 @deffnx {C Function} scm_sloppy_assoc (key, alist)
2835 Behaves like @code{assoc} but does not do any error checking.
2836 Recommended only for use in Guile internals.
2840 @subsubsection Alist Example
2842 Here is a longer example of how alists may be used in practice.
2845 (define capitals '(("New York" . "Albany")
2846 ("Oregon" . "Salem")
2847 ("Florida" . "Miami")))
2849 ;; What's the capital of Oregon?
2850 (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
2851 (assoc-ref capitals "Oregon") @result{} "Salem"
2853 ;; We left out South Dakota.
2855 (assoc-set! capitals "South Dakota" "Pierre"))
2857 @result{} (("South Dakota" . "Pierre")
2858 ("New York" . "Albany")
2859 ("Oregon" . "Salem")
2860 ("Florida" . "Miami"))
2862 ;; And we got Florida wrong.
2864 (assoc-set! capitals "Florida" "Tallahassee"))
2866 @result{} (("South Dakota" . "Pierre")
2867 ("New York" . "Albany")
2868 ("Oregon" . "Salem")
2869 ("Florida" . "Tallahassee"))
2871 ;; After Oregon secedes, we can remove it.
2873 (assoc-remove! capitals "Oregon"))
2875 @result{} (("South Dakota" . "Pierre")
2876 ("New York" . "Albany")
2877 ("Florida" . "Tallahassee"))
2881 @subsection Hash Tables
2882 @tpindex Hash Tables
2884 @c FIXME::martin: Review me!
2886 Hash tables are dictionaries which offer similar functionality as
2887 association lists: They provide a mapping from keys to values. The
2888 difference is that association lists need time linear in the size of
2889 elements when searching for entries, whereas hash tables can normally
2890 search in constant time. The drawback is that hash tables require a
2891 little bit more memory, and that you can not use the normal list
2892 procedures (@pxref{Lists}) for working with them.
2895 * Hash Table Examples:: Demonstration of hash table usage.
2896 * Hash Table Reference:: Hash table procedure descriptions.
2900 @node Hash Table Examples
2901 @subsubsection Hash Table Examples
2903 @c FIXME::martin: Review me!
2905 For demonstration purposes, this section gives a few usage examples of
2906 some hash table procedures, together with some explanation what they do.
2908 First we start by creating a new hash table with 31 slots, and
2909 populate it with two key/value pairs.
2912 (define h (make-hash-table 31))
2914 (hashq-create-handle! h 'foo "bar")
2918 (hashq-create-handle! h 'braz "zonk")
2922 (hashq-create-handle! h 'frob #f)
2927 You can get the value for a given key with the procedure
2928 @code{hashq-ref}, but the problem with this procedure is that you
2929 cannot reliably determine whether a key does exists in the table. The
2930 reason is that the procedure returns @code{#f} if the key is not in
2931 the table, but it will return the same value if the key is in the
2932 table and just happens to have the value @code{#f}, as you can see in
2933 the following examples.
2944 (hashq-ref h 'not-there)
2949 Better is to use the procedure @code{hashq-get-handle}, which makes a
2950 distinction between the two cases. Just like @code{assq}, this
2951 procedure returns a key/value-pair on success, and @code{#f} if the
2955 (hashq-get-handle h 'foo)
2959 (hashq-get-handle h 'not-there)
2964 There is no procedure for calculating the number of key/value-pairs in
2965 a hash table, but @code{hash-fold} can be used for doing exactly that.
2968 (hash-fold (lambda (key value seed) (+ 1 seed)) 0 h)
2973 @node Hash Table Reference
2974 @subsubsection Hash Table Reference
2976 @c FIXME: Describe in broad terms what happens for resizing, and what
2977 @c the initial size means for this.
2979 Like the association list functions, the hash table functions come in
2980 several varieties, according to the equality test used for the keys.
2981 Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
2982 functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
2983 the @code{hashx-} functions use an application supplied test.
2985 A single @code{make-hash-table} creates a hash table suitable for use
2986 with any set of functions, but it's imperative that just one set is
2987 then used consistently, or results will be unpredictable.
2990 Hash tables are implemented as a vector indexed by a hash value formed
2991 from the key, with an association list of key/value pairs for each
2992 bucket in case distinct keys hash together. Direct access to the
2993 pairs in those lists is provided by the @code{-handle-} functions.
2995 When the number of table entries goes above a threshold the vector is
2996 increased and the entries rehashed, to prevent the bucket lists
2997 becoming too long and slowing down accesses. When the number of
2998 entries goes below a threshold the vector is decreased to save space.
3001 For the @code{hashx-} ``extended'' routines, an application supplies a
3002 @var{hash} function producing an integer index like @code{hashq} etc
3003 below, and an @var{assoc} alist search function like @code{assq} etc
3004 (@pxref{Retrieving Alist Entries}). Here's an example of such
3005 functions implementing case-insensitive hashing of string keys,
3008 (use-modules (srfi srfi-1)
3011 (define (my-hash str size)
3012 (remainder (string-hash-ci str) size))
3013 (define (my-assoc str alist)
3014 (find (lambda (pair) (string-ci=? str (car pair))) alist))
3016 (define my-table (make-hash-table))
3017 (hashx-set! my-hash my-assoc my-table "foo" 123)
3019 (hashx-ref my-hash my-assoc my-table "FOO")
3023 In a @code{hashx-} @var{hash} function the aim is to spread keys
3024 across the vector, so bucket lists don't become long. But the actual
3025 values are arbitrary as long as they're in the range 0 to
3026 @math{@var{size}-1}. Helpful functions for forming a hash value, in
3027 addition to @code{hashq} etc below, include @code{symbol-hash}
3028 (@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci}
3029 (@pxref{String Comparison}), and @code{char-set-hash}
3030 (@pxref{Character Set Predicates/Comparison}).
3032 Note that currently, unfortunately, there's no @code{hashx-remove!}
3033 function, which rather limits the usefulness of the @code{hashx-}
3037 @deffn {Scheme Procedure} make-hash-table [size]
3038 Create a new hash table, with an optional minimum vector @var{size}.
3040 When @var{size} is given, the table vector will still grow and shrink
3041 automatically, as described above, but with @var{size} as a minimum.
3042 If an application knows roughly how many entries the table will hold
3043 then it can use @var{size} to avoid rehashing when initial entries are
3047 @deffn {Scheme Procedure} hash-table? obj
3048 @deffnx {C Function} scm_hash_table_p (obj)
3049 Return @code{#t} if @var{obj} is a hash table.
3052 @deffn {Scheme Procedure} hash-clear! table
3053 @deffnx {C Function} scm_hash_clear_x (table)
3054 Remove all items from TABLE (without triggering a resize).
3057 @deffn {Scheme Procedure} hash-ref table key [dflt]
3058 @deffnx {Scheme Procedure} hashq-ref table key [dflt]
3059 @deffnx {Scheme Procedure} hashv-ref table key [dflt]
3060 @deffnx {Scheme Procedure} hashx-ref hash assoc table key [dflt]
3061 @deffnx {C Function} scm_hash_ref (table, key, dflt)
3062 @deffnx {C Function} scm_hashq_ref (table, key, dflt)
3063 @deffnx {C Function} scm_hashv_ref (table, key, dflt)
3064 @deffnx {C Function} scm_hashx_ref (hash, assoc, table, key, dflt)
3065 Lookup @var{key} in the given hash @var{table}, and return the
3066 associated value. If @var{key} is not found, return @var{dflt}, or
3067 @code{#f} if @var{dflt} is not given.
3070 @deffn {Scheme Procedure} hash-set! table key val
3071 @deffnx {Scheme Procedure} hashq-set! table key val
3072 @deffnx {Scheme Procedure} hashv-set! table key val
3073 @deffnx {Scheme Procedure} hashx-set! hash assoc table key val
3074 @deffnx {C Function} scm_hash_set_x (table, key, val)
3075 @deffnx {C Function} scm_hashq_set_x (table, key, val)
3076 @deffnx {C Function} scm_hashv_set_x (table, key, val)
3077 @deffnx {C Function} scm_hashx_set_x (hash, assoc, table, key, val)
3078 Associate @var{val} with @var{key} in the given hash @var{table}. If
3079 @var{key} is already present then it's associated value is changed.
3080 If it's not present then a new entry is created.
3083 @deffn {Scheme Procedure} hash-remove! table key
3084 @deffnx {Scheme Procedure} hashq-remove! table key
3085 @deffnx {Scheme Procedure} hashv-remove! table key
3086 @deffnx {C Function} scm_hash_remove_x (table, key)
3087 @deffnx {C Function} scm_hashq_remove_x (table, key)
3088 @deffnx {C Function} scm_hashv_remove_x (table, key)
3089 Remove any association for @var{key} in the given hash @var{table}.
3090 If @var{key} is not in @var{table} then nothing is done.
3093 @deffn {Scheme Procedure} hash key size
3094 @deffnx {Scheme Procedure} hashq key size
3095 @deffnx {Scheme Procedure} hashv key size
3096 @deffnx {C Function} scm_hash (key, size)
3097 @deffnx {C Function} scm_hashq (key, size)
3098 @deffnx {C Function} scm_hashv (key, size)
3099 Return a hash value for @var{key}. This is a number in the range
3100 @math{0} to @math{@var{size}-1}, which is suitable for use in a hash
3101 table of the given @var{size}.
3103 Note that @code{hashq} and @code{hashv} may use internal addresses of
3104 objects, so if an object is garbage collected and re-created it can
3105 have a different hash value, even when the two are notionally
3106 @code{eq?}. For instance with symbols,
3109 (hashq 'something 123) @result{} 19
3111 (hashq 'something 123) @result{} 62
3114 In normal use this is not a problem, since an object entered into a
3115 hash table won't be garbage collected until removed. It's only if
3116 hashing calculations are somehow separated from normal references that
3117 its lifetime needs to be considered.
3120 @deffn {Scheme Procedure} hash-get-handle table key
3121 @deffnx {Scheme Procedure} hashq-get-handle table key
3122 @deffnx {Scheme Procedure} hashv-get-handle table key
3123 @deffnx {Scheme Procedure} hashx-get-handle hash assoc table key
3124 @deffnx {C Function} scm_hash_get_handle (table, key)
3125 @deffnx {C Function} scm_hashq_get_handle (table, key)
3126 @deffnx {C Function} scm_hashv_get_handle (table, key)
3127 @deffnx {C Function} scm_hashx_get_handle (hash, assoc, table, key)
3128 Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
3129 given hash @var{table}, or @code{#f} if @var{key} is not in
3133 @deffn {Scheme Procedure} hash-create-handle! table key init
3134 @deffnx {Scheme Procedure} hashq-create-handle! table key init
3135 @deffnx {Scheme Procedure} hashv-create-handle! table key init
3136 @deffnx {Scheme Procedure} hashx-create-handle! hash assoc table key init
3137 @deffnx {C Function} scm_hash_create_handle_x (table, key, init)
3138 @deffnx {C Function} scm_hashq_create_handle_x (table, key, init)
3139 @deffnx {C Function} scm_hashv_create_handle_x (table, key, init)
3140 @deffnx {C Function} scm_hashx_create_handle_x (hash, assoc, table, key, init)
3141 Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
3142 given hash @var{table}. If @var{key} is not in @var{table} then
3143 create an entry for it with @var{init} as the value, and return that
3147 @deffn {Scheme Procedure} hash-map->list proc table
3148 @deffnx {Scheme Procedure} hash-for-each proc table
3149 @deffnx {C Function} scm_hash_map_to_list (proc, table)
3150 @deffnx {C Function} scm_hash_for_each (proc, table)
3151 Apply @var{proc} to the entries in the given hash @var{table}. Each
3152 call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map->list}
3153 returns a list of the results from these calls, @code{hash-for-each}
3154 discards the results and returns an unspecified value.
3156 Calls are made over the table entries in an unspecified order, and for
3157 @code{hash-map->list} the order of the values in the returned list is
3158 unspecified. Results will be unpredictable if @var{table} is modified
3161 For example the following returns a new alist comprising all the
3162 entries from @code{mytable}, in no particular order.
3165 (hash-map->list cons mytable)
3169 @deffn {Scheme Procedure} hash-for-each-handle proc table
3170 @deffnx {C Function} scm_hash_for_each_handle (proc, table)
3171 Apply @var{proc} to the entries in the given hash @var{table}. Each
3172 call is @code{(@var{proc} @var{handle})}, where @var{handle} is a
3173 @code{(@var{key} . @var{value})} pair. Return an unspecified value.
3175 @code{hash-for-each-handle} differs from @code{hash-for-each} only in
3176 the argument list of @var{proc}.
3179 @deffn {Scheme Procedure} hash-fold proc init table
3180 @deffnx {C Function} scm_hash_fold (proc, init, table)
3181 Accumulate a result by applying @var{proc} to the elements of the
3182 given hash @var{table}. Each call is @code{(@var{proc} @var{key}
3183 @var{value} @var{prior-result})}, where @var{key} and @var{value} are
3184 from the @var{table} and @var{prior-result} is the return from the
3185 previous @var{proc} call. For the first call, @var{prior-result} is
3186 the given @var{init} value.
3188 Calls are made over the table entries in an unspecified order.
3189 Results will be unpredictable if @var{table} is modified while
3190 @code{hash-fold} is running.
3192 For example, the following returns a count of how many keys in
3193 @code{mytable} are strings.
3196 (hash-fold (lambda (key value prior)
3197 (if (string? key) (1+ prior) prior))
3204 @c TeX-master: "guile.texi"