merge from 1.8 branch
[bpt/guile.git] / doc / ref / api-compound.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @page
8 @node Compound Data Types
9 @section Compound Data Types
10
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.
16
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.
20
21 @menu
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.
29 * Records::
30 * Structures::
31 * Dictionary Types:: About dictionary types in general.
32 * Association Lists:: List-based dictionaries.
33 * Hash Tables:: Table-based dictionaries.
34 @end menu
35
36
37 @node Pairs
38 @subsection Pairs
39 @tpindex Pairs
40
41 Pairs are used to combine two Scheme objects into one compound object.
42 Hence the name: A pair stores a pair of objects.
43
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}).
49
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.
58
59 @lisp
60 (1 . 2)
61 (foo . bar)
62 @end lisp
63
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 @ref{Expression Syntax}. The correct way
67 to try these examples is as follows.
68
69 @lisp
70 '(1 . 2)
71 @result{}
72 (1 . 2)
73 '(foo . bar)
74 @result{}
75 (foo . bar)
76 @end lisp
77
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.
83
84 @rnindex cons
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.
90 @end deffn
91
92 @rnindex pair?
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
96 @code{#f}.
97 @end deffn
98
99 @deftypefn {C Function} int scm_is_pair (SCM x)
100 Return 1 when @var{x} is a pair; otherwise return 0.
101 @end deftypefn
102
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 car of a pair, or the car of
108 the cdr of a pair, etc., the procedures called @code{caar},
109 @code{cadr} and so on are also predefined.
110
111 @rnindex car
112 @rnindex cdr
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.
118 @end deffn
119
120 @deftypefn {C Macro} SCM SCM_CAR (SCM pair)
121 @deftypefnx {C Macro} SCM SCM_CDR (SCM pair)
122 These two macros are the fastest way to access the car or cdr of a
123 pair; they can be thought of as compiling into a single memory
124 reference.
125
126 These macros do no checking at all. The argument @var{pair} must be a
127 valid pair.
128 @end deftypefn
129
130 @deffn {Scheme Procedure} cddr pair
131 @deffnx {Scheme Procedure} cdar pair
132 @deffnx {Scheme Procedure} cadr pair
133 @deffnx {Scheme Procedure} caar pair
134 @deffnx {Scheme Procedure} cdddr pair
135 @deffnx {Scheme Procedure} cddar pair
136 @deffnx {Scheme Procedure} cdadr pair
137 @deffnx {Scheme Procedure} cdaar pair
138 @deffnx {Scheme Procedure} caddr pair
139 @deffnx {Scheme Procedure} cadar pair
140 @deffnx {Scheme Procedure} caadr pair
141 @deffnx {Scheme Procedure} caaar pair
142 @deffnx {Scheme Procedure} cddddr pair
143 @deffnx {Scheme Procedure} cdddar pair
144 @deffnx {Scheme Procedure} cddadr pair
145 @deffnx {Scheme Procedure} cddaar pair
146 @deffnx {Scheme Procedure} cdaddr pair
147 @deffnx {Scheme Procedure} cdadar pair
148 @deffnx {Scheme Procedure} cdaadr pair
149 @deffnx {Scheme Procedure} cdaaar pair
150 @deffnx {Scheme Procedure} cadddr pair
151 @deffnx {Scheme Procedure} caddar pair
152 @deffnx {Scheme Procedure} cadadr pair
153 @deffnx {Scheme Procedure} cadaar pair
154 @deffnx {Scheme Procedure} caaddr pair
155 @deffnx {Scheme Procedure} caadar pair
156 @deffnx {Scheme Procedure} caaadr pair
157 @deffnx {Scheme Procedure} caaaar pair
158 @deffnx {C Function} scm_cddr (pair)
159 @deffnx {C Function} scm_cdar (pair)
160 @deffnx {C Function} scm_cadr (pair)
161 @deffnx {C Function} scm_caar (pair)
162 @deffnx {C Function} scm_cdddr (pair)
163 @deffnx {C Function} scm_cddar (pair)
164 @deffnx {C Function} scm_cdadr (pair)
165 @deffnx {C Function} scm_cdaar (pair)
166 @deffnx {C Function} scm_caddr (pair)
167 @deffnx {C Function} scm_cadar (pair)
168 @deffnx {C Function} scm_caadr (pair)
169 @deffnx {C Function} scm_caaar (pair)
170 @deffnx {C Function} scm_cddddr (pair)
171 @deffnx {C Function} scm_cdddar (pair)
172 @deffnx {C Function} scm_cddadr (pair)
173 @deffnx {C Function} scm_cddaar (pair)
174 @deffnx {C Function} scm_cdaddr (pair)
175 @deffnx {C Function} scm_cdadar (pair)
176 @deffnx {C Function} scm_cdaadr (pair)
177 @deffnx {C Function} scm_cdaaar (pair)
178 @deffnx {C Function} scm_cadddr (pair)
179 @deffnx {C Function} scm_caddar (pair)
180 @deffnx {C Function} scm_cadadr (pair)
181 @deffnx {C Function} scm_cadaar (pair)
182 @deffnx {C Function} scm_caaddr (pair)
183 @deffnx {C Function} scm_caadar (pair)
184 @deffnx {C Function} scm_caaadr (pair)
185 @deffnx {C Function} scm_caaaar (pair)
186 These procedures are compositions of @code{car} and @code{cdr}, where
187 for example @code{caddr} could be defined by
188
189 @lisp
190 (define caddr (lambda (x) (car (cdr (cdr x)))))
191 @end lisp
192 @end deffn
193
194 @rnindex set-car!
195 @deffn {Scheme Procedure} set-car! pair value
196 @deffnx {C Function} scm_set_car_x (pair, value)
197 Stores @var{value} in the car field of @var{pair}. The value returned
198 by @code{set-car!} is unspecified.
199 @end deffn
200
201 @rnindex set-cdr!
202 @deffn {Scheme Procedure} set-cdr! pair value
203 @deffnx {C Function} scm_set_cdr_x (pair, value)
204 Stores @var{value} in the cdr field of @var{pair}. The value returned
205 by @code{set-cdr!} is unspecified.
206 @end deffn
207
208
209 @node Lists
210 @subsection Lists
211 @tpindex Lists
212
213 A very important data type in Scheme---as well as in all other Lisp
214 dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
215 Scheme does not have a real datatype @dfn{list}. Lists are made up of
216 @dfn{chained pairs}, and only exist by definition---a list is a chain
217 of pairs which looks like a list.}
218
219 This is the short definition of what a list is:
220
221 @itemize @bullet
222 @item
223 Either the empty list @code{()},
224
225 @item
226 or a pair which has a list in its cdr.
227 @end itemize
228
229 @c FIXME::martin: Describe the pair chaining in more detail.
230
231 @c FIXME::martin: What is a proper, what an improper list?
232 @c What is a circular list?
233
234 @c FIXME::martin: Maybe steal some graphics from the Elisp reference
235 @c manual?
236
237 @menu
238 * List Syntax:: Writing literal lists.
239 * List Predicates:: Testing lists.
240 * List Constructors:: Creating new lists.
241 * List Selection:: Selecting from lists, getting their length.
242 * Append/Reverse:: Appending and reversing lists.
243 * List Modification:: Modifying existing lists.
244 * List Searching:: Searching for list elements
245 * List Mapping:: Applying procedures to lists.
246 @end menu
247
248 @node List Syntax
249 @subsubsection List Read Syntax
250
251 The syntax for lists is an opening parentheses, then all the elements of
252 the list (separated by whitespace) and finally a closing
253 parentheses.@footnote{Note that there is no separation character between
254 the list elements, like a comma or a semicolon.}.
255
256 @lisp
257 (1 2 3) ; @r{a list of the numbers 1, 2 and 3}
258 ("foo" bar 3.1415) ; @r{a string, a symbol and a real number}
259 () ; @r{the empty list}
260 @end lisp
261
262 The last example needs a bit more explanation. A list with no elements,
263 called the @dfn{empty list}, is special in some ways. It is used for
264 terminating lists by storing it into the cdr of the last pair that makes
265 up a list. An example will clear that up:
266
267 @lisp
268 (car '(1))
269 @result{}
270 1
271 (cdr '(1))
272 @result{}
273 ()
274 @end lisp
275
276 This example also shows that lists have to be quoted when written
277 (@pxref{Expression Syntax}), because they would otherwise be
278 mistakingly taken as procedure applications (@pxref{Simple
279 Invocation}).
280
281
282 @node List Predicates
283 @subsubsection List Predicates
284
285 Often it is useful to test whether a given Scheme object is a list or
286 not. List-processing procedures could use this information to test
287 whether their input is valid, or they could do different things
288 depending on the datatype of their arguments.
289
290 @rnindex list?
291 @deffn {Scheme Procedure} list? x
292 @deffnx {C Function} scm_list_p (x)
293 Return @code{#t} iff @var{x} is a proper list, else @code{#f}.
294 @end deffn
295
296 The predicate @code{null?} is often used in list-processing code to
297 tell whether a given list has run out of elements. That is, a loop
298 somehow deals with the elements of a list until the list satisfies
299 @code{null?}. Then, the algorithm terminates.
300
301 @rnindex null?
302 @deffn {Scheme Procedure} null? x
303 @deffnx {C Function} scm_null_p (x)
304 Return @code{#t} iff @var{x} is the empty list, else @code{#f}.
305 @end deffn
306
307 @deftypefn {C Function} int scm_is_null (SCM x)
308 Return 1 when @var{x} is the empty list; otherwise return 0.
309 @end deftypefn
310
311
312 @node List Constructors
313 @subsubsection List Constructors
314
315 This section describes the procedures for constructing new lists.
316 @code{list} simply returns a list where the elements are the arguments,
317 @code{cons*} is similar, but the last argument is stored in the cdr of
318 the last pair of the list.
319
320 @c C Function scm_list(rest) used to be documented here, but it's a
321 @c no-op since it does nothing but return the list the caller must
322 @c have already created.
323 @c
324 @deffn {Scheme Procedure} list elem1 @dots{} elemN
325 @deffnx {C Function} scm_list_1 (elem1)
326 @deffnx {C Function} scm_list_2 (elem1, elem2)
327 @deffnx {C Function} scm_list_3 (elem1, elem2, elem3)
328 @deffnx {C Function} scm_list_4 (elem1, elem2, elem3, elem4)
329 @deffnx {C Function} scm_list_5 (elem1, elem2, elem3, elem4, elem5)
330 @deffnx {C Function} scm_list_n (elem1, @dots{}, elemN, @nicode{SCM_UNDEFINED})
331 @rnindex list
332 Return a new list containing elements @var{elem1} to @var{elemN}.
333
334 @code{scm_list_n} takes a variable number of arguments, terminated by
335 the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is
336 not included in the list. None of @var{elem1} to @var{elemN} can
337 themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will
338 terminate at that point.
339 @end deffn
340
341 @c C Function scm_cons_star(arg1,rest) used to be documented here,
342 @c but it's not really a useful interface, since it expects the
343 @c caller to have already consed up all but the first argument
344 @c already.
345 @c
346 @deffn {Scheme Procedure} cons* arg1 arg2 @dots{}
347 Like @code{list}, but the last arg provides the tail of the
348 constructed list, returning @code{(cons @var{arg1} (cons
349 @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
350 argument. If given one argument, that argument is returned as
351 result. This function is called @code{list*} in some other
352 Schemes and in Common LISP.
353 @end deffn
354
355 @deffn {Scheme Procedure} list-copy lst
356 @deffnx {C Function} scm_list_copy (lst)
357 Return a (newly-created) copy of @var{lst}.
358 @end deffn
359
360 @deffn {Scheme Procedure} make-list n [init]
361 Create a list containing of @var{n} elements, where each element is
362 initialized to @var{init}. @var{init} defaults to the empty list
363 @code{()} if not given.
364 @end deffn
365
366 Note that @code{list-copy} only makes a copy of the pairs which make up
367 the spine of the lists. The list elements are not copied, which means
368 that modifying the elements of the new list also modifies the elements
369 of the old list. On the other hand, applying procedures like
370 @code{set-cdr!} or @code{delv!} to the new list will not alter the old
371 list. If you also need to copy the list elements (making a deep copy),
372 use the procedure @code{copy-tree} (@pxref{Copying}).
373
374 @node List Selection
375 @subsubsection List Selection
376
377 These procedures are used to get some information about a list, or to
378 retrieve one or more elements of a list.
379
380 @rnindex length
381 @deffn {Scheme Procedure} length lst
382 @deffnx {C Function} scm_length (lst)
383 Return the number of elements in list @var{lst}.
384 @end deffn
385
386 @deffn {Scheme Procedure} last-pair lst
387 @deffnx {C Function} scm_last_pair (lst)
388 Return the last pair in @var{lst}, signalling an error if
389 @var{lst} is circular.
390 @end deffn
391
392 @rnindex list-ref
393 @deffn {Scheme Procedure} list-ref list k
394 @deffnx {C Function} scm_list_ref (list, k)
395 Return the @var{k}th element from @var{list}.
396 @end deffn
397
398 @rnindex list-tail
399 @deffn {Scheme Procedure} list-tail lst k
400 @deffnx {Scheme Procedure} list-cdr-ref lst k
401 @deffnx {C Function} scm_list_tail (lst, k)
402 Return the "tail" of @var{lst} beginning with its @var{k}th element.
403 The first element of the list is considered to be element 0.
404
405 @code{list-tail} and @code{list-cdr-ref} are identical. It may help to
406 think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
407 or returning the results of cdring @var{k} times down @var{lst}.
408 @end deffn
409
410 @deffn {Scheme Procedure} list-head lst k
411 @deffnx {C Function} scm_list_head (lst, k)
412 Copy the first @var{k} elements from @var{lst} into a new list, and
413 return it.
414 @end deffn
415
416 @node Append/Reverse
417 @subsubsection Append and Reverse
418
419 @code{append} and @code{append!} are used to concatenate two or more
420 lists in order to form a new list. @code{reverse} and @code{reverse!}
421 return lists with the same elements as their arguments, but in reverse
422 order. The procedure variants with an @code{!} directly modify the
423 pairs which form the list, whereas the other procedures create new
424 pairs. This is why you should be careful when using the side-effecting
425 variants.
426
427 @rnindex append
428 @deffn {Scheme Procedure} append lst1 @dots{} lstN
429 @deffnx {Scheme Procedure} append! lst1 @dots{} lstN
430 @deffnx {C Function} scm_append (lstlst)
431 @deffnx {C Function} scm_append_x (lstlst)
432 Return a list comprising all the elements of lists @var{lst1} to
433 @var{lstN}.
434
435 @lisp
436 (append '(x) '(y)) @result{} (x y)
437 (append '(a) '(b c d)) @result{} (a b c d)
438 (append '(a (b)) '((c))) @result{} (a (b) (c))
439 @end lisp
440
441 The last argument @var{lstN} may actually be any object; an improper
442 list results if the last argument is not a proper list.
443
444 @lisp
445 (append '(a b) '(c . d)) @result{} (a b c . d)
446 (append '() 'a) @result{} a
447 @end lisp
448
449 @code{append} doesn't modify the given lists, but the return may share
450 structure with the final @var{lstN}. @code{append!} modifies the
451 given lists to form its return.
452
453 For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list
454 of the list operands @var{lst1} @dots{} @var{lstN}. That @var{lstlst}
455 itself is not modified or used in the return.
456 @end deffn
457
458 @rnindex reverse
459 @deffn {Scheme Procedure} reverse lst
460 @deffnx {Scheme Procedure} reverse! lst [newtail]
461 @deffnx {C Function} scm_reverse (lst)
462 @deffnx {C Function} scm_reverse_x (lst, newtail)
463 Return a list comprising the elements of @var{lst}, in reverse order.
464
465 @code{reverse} constructs a new list, @code{reverse!} modifies
466 @var{lst} in constructing its return.
467
468 For @code{reverse!}, the optional @var{newtail} is appended to to the
469 result. @var{newtail} isn't reversed, it simply becomes the list
470 tail. For @code{scm_reverse_x}, the @var{newtail} parameter is
471 mandatory, but can be @code{SCM_EOL} if no further tail is required.
472 @end deffn
473
474 @node List Modification
475 @subsubsection List Modification
476
477 The following procedures modify an existing list, either by changing
478 elements of the list, or by changing the list structure itself.
479
480 @deffn {Scheme Procedure} list-set! list k val
481 @deffnx {C Function} scm_list_set_x (list, k, val)
482 Set the @var{k}th element of @var{list} to @var{val}.
483 @end deffn
484
485 @deffn {Scheme Procedure} list-cdr-set! list k val
486 @deffnx {C Function} scm_list_cdr_set_x (list, k, val)
487 Set the @var{k}th cdr of @var{list} to @var{val}.
488 @end deffn
489
490 @deffn {Scheme Procedure} delq item lst
491 @deffnx {C Function} scm_delq (item, lst)
492 Return a newly-created copy of @var{lst} with elements
493 @code{eq?} to @var{item} removed. This procedure mirrors
494 @code{memq}: @code{delq} compares elements of @var{lst} against
495 @var{item} with @code{eq?}.
496 @end deffn
497
498 @deffn {Scheme Procedure} delv item lst
499 @deffnx {C Function} scm_delv (item, lst)
500 Return a newly-created copy of @var{lst} with elements
501 @code{eqv?} to @var{item} removed. This procedure mirrors
502 @code{memv}: @code{delv} compares elements of @var{lst} against
503 @var{item} with @code{eqv?}.
504 @end deffn
505
506 @deffn {Scheme Procedure} delete item lst
507 @deffnx {C Function} scm_delete (item, lst)
508 Return a newly-created copy of @var{lst} with elements
509 @code{equal?} to @var{item} removed. This procedure mirrors
510 @code{member}: @code{delete} compares elements of @var{lst}
511 against @var{item} with @code{equal?}.
512 @end deffn
513
514 @deffn {Scheme Procedure} delq! item lst
515 @deffnx {Scheme Procedure} delv! item lst
516 @deffnx {Scheme Procedure} delete! item lst
517 @deffnx {C Function} scm_delq_x (item, lst)
518 @deffnx {C Function} scm_delv_x (item, lst)
519 @deffnx {C Function} scm_delete_x (item, lst)
520 These procedures are destructive versions of @code{delq}, @code{delv}
521 and @code{delete}: they modify the pointers in the existing @var{lst}
522 rather than creating a new list. Caveat evaluator: Like other
523 destructive list functions, these functions cannot modify the binding of
524 @var{lst}, and so cannot be used to delete the first element of
525 @var{lst} destructively.
526 @end deffn
527
528 @deffn {Scheme Procedure} delq1! item lst
529 @deffnx {C Function} scm_delq1_x (item, lst)
530 Like @code{delq!}, but only deletes the first occurrence of
531 @var{item} from @var{lst}. Tests for equality using
532 @code{eq?}. See also @code{delv1!} and @code{delete1!}.
533 @end deffn
534
535 @deffn {Scheme Procedure} delv1! item lst
536 @deffnx {C Function} scm_delv1_x (item, lst)
537 Like @code{delv!}, but only deletes the first occurrence of
538 @var{item} from @var{lst}. Tests for equality using
539 @code{eqv?}. See also @code{delq1!} and @code{delete1!}.
540 @end deffn
541
542 @deffn {Scheme Procedure} delete1! item lst
543 @deffnx {C Function} scm_delete1_x (item, lst)
544 Like @code{delete!}, but only deletes the first occurrence of
545 @var{item} from @var{lst}. Tests for equality using
546 @code{equal?}. See also @code{delq1!} and @code{delv1!}.
547 @end deffn
548
549 @deffn {Scheme Procedure} filter pred lst
550 @deffnx {Scheme Procedure} filter! pred lst
551 Return a list containing all elements from @var{lst} which satisfy the
552 predicate @var{pred}. The elements in the result list have the same
553 order as in @var{lst}. The order in which @var{pred} is applied to
554 the list elements is not specified.
555
556 @code{filter} does not change @var{lst}, but the result may share a
557 tail with it. @code{filter!} may modify @var{lst} to construct its
558 return.
559 @end deffn
560
561 @node List Searching
562 @subsubsection List Searching
563
564 The following procedures search lists for particular elements. They use
565 different comparison predicates for comparing list elements with the
566 object to be searched. When they fail, they return @code{#f}, otherwise
567 they return the sublist whose car is equal to the search object, where
568 equality depends on the equality predicate used.
569
570 @rnindex memq
571 @deffn {Scheme Procedure} memq x lst
572 @deffnx {C Function} scm_memq (x, lst)
573 Return the first sublist of @var{lst} whose car is @code{eq?}
574 to @var{x} where the sublists of @var{lst} are the non-empty
575 lists returned by @code{(list-tail @var{lst} @var{k})} for
576 @var{k} less than the length of @var{lst}. If @var{x} does not
577 occur in @var{lst}, then @code{#f} (not the empty list) is
578 returned.
579 @end deffn
580
581 @rnindex memv
582 @deffn {Scheme Procedure} memv x lst
583 @deffnx {C Function} scm_memv (x, lst)
584 Return the first sublist of @var{lst} whose car is @code{eqv?}
585 to @var{x} where the sublists of @var{lst} are the non-empty
586 lists returned by @code{(list-tail @var{lst} @var{k})} for
587 @var{k} less than the length of @var{lst}. If @var{x} does not
588 occur in @var{lst}, then @code{#f} (not the empty list) is
589 returned.
590 @end deffn
591
592 @rnindex member
593 @deffn {Scheme Procedure} member x lst
594 @deffnx {C Function} scm_member (x, lst)
595 Return the first sublist of @var{lst} whose car is
596 @code{equal?} to @var{x} where the sublists of @var{lst} are
597 the non-empty lists returned by @code{(list-tail @var{lst}
598 @var{k})} for @var{k} less than the length of @var{lst}. If
599 @var{x} does not occur in @var{lst}, then @code{#f} (not the
600 empty list) is returned.
601 @end deffn
602
603
604 @node List Mapping
605 @subsubsection List Mapping
606
607 List processing is very convenient in Scheme because the process of
608 iterating over the elements of a list can be highly abstracted. The
609 procedures in this section are the most basic iterating procedures for
610 lists. They take a procedure and one or more lists as arguments, and
611 apply the procedure to each element of the list. They differ in their
612 return value.
613
614 @rnindex map
615 @c begin (texi-doc-string "guile" "map")
616 @deffn {Scheme Procedure} map proc arg1 arg2 @dots{}
617 @deffnx {Scheme Procedure} map-in-order proc arg1 arg2 @dots{}
618 @deffnx {C Function} scm_map (proc, arg1, args)
619 Apply @var{proc} to each element of the list @var{arg1} (if only two
620 arguments are given), or to the corresponding elements of the argument
621 lists (if more than two arguments are given). The result(s) of the
622 procedure applications are saved and returned in a list. For
623 @code{map}, the order of procedure applications is not specified,
624 @code{map-in-order} applies the procedure from left to right to the list
625 elements.
626 @end deffn
627
628 @rnindex for-each
629 @c begin (texi-doc-string "guile" "for-each")
630 @deffn {Scheme Procedure} for-each proc arg1 arg2 @dots{}
631 Like @code{map}, but the procedure is always applied from left to right,
632 and the result(s) of the procedure applications are thrown away. The
633 return value is not specified.
634 @end deffn
635
636
637 @node Vectors
638 @subsection Vectors
639 @tpindex Vectors
640
641 Vectors are sequences of Scheme objects. Unlike lists, the length of a
642 vector, once the vector is created, cannot be changed. The advantage of
643 vectors over lists is that the time required to access one element of a vector
644 given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number,
645 is constant, whereas lists have an access time linear to the position of the
646 accessed element in the list.
647
648 Vectors can contain any kind of Scheme object; it is even possible to
649 have different types of objects in the same vector. For vectors
650 containing vectors, you may wish to use arrays, instead. Note, too,
651 that vectors are the special case of one dimensional non-uniform arrays
652 and that most array procedures operate happily on vectors
653 (@pxref{Arrays}).
654
655 @menu
656 * Vector Syntax:: Read syntax for vectors.
657 * Vector Creation:: Dynamic vector creation and validation.
658 * Vector Accessors:: Accessing and modifying vector contents.
659 * Vector Accessing from C:: Ways to work with vectors from C.
660 @end menu
661
662
663 @node Vector Syntax
664 @subsubsection Read Syntax for Vectors
665
666 Vectors can literally be entered in source code, just like strings,
667 characters or some of the other data types. The read syntax for vectors
668 is as follows: A sharp sign (@code{#}), followed by an opening
669 parentheses, all elements of the vector in their respective read syntax,
670 and finally a closing parentheses. The following are examples of the
671 read syntax for vectors; where the first vector only contains numbers
672 and the second three different object types: a string, a symbol and a
673 number in hexadecimal notation.
674
675 @lisp
676 #(1 2 3)
677 #("Hello" foo #xdeadbeef)
678 @end lisp
679
680 Like lists, vectors have to be quoted:
681
682 @lisp
683 '#(a b c) @result{} #(a b c)
684 @end lisp
685
686 @node Vector Creation
687 @subsubsection Dynamic Vector Creation and Validation
688
689 Instead of creating a vector implicitly by using the read syntax just
690 described, you can create a vector dynamically by calling one of the
691 @code{vector} and @code{list->vector} primitives with the list of Scheme
692 values that you want to place into a vector. The size of the vector
693 thus created is determined implicitly by the number of arguments given.
694
695 @rnindex vector
696 @rnindex list->vector
697 @deffn {Scheme Procedure} vector . l
698 @deffnx {Scheme Procedure} list->vector l
699 @deffnx {C Function} scm_vector (l)
700 Return a newly allocated vector composed of the
701 given arguments. Analogous to @code{list}.
702
703 @lisp
704 (vector 'a 'b 'c) @result{} #(a b c)
705 @end lisp
706 @end deffn
707
708 The inverse operation is @code{vector->list}:
709
710 @rnindex vector->list
711 @deffn {Scheme Procedure} vector->list v
712 @deffnx {C Function} scm_vector_to_list (v)
713 Return a newly allocated list composed of the elements of @var{v}.
714
715 @lisp
716 (vector->list '#(dah dah didah)) @result{} (dah dah didah)
717 (list->vector '(dididit dah)) @result{} #(dididit dah)
718 @end lisp
719 @end deffn
720
721 To allocate a vector with an explicitly specified size, use
722 @code{make-vector}. With this primitive you can also specify an initial
723 value for the vector elements (the same value for all elements, that
724 is):
725
726 @rnindex make-vector
727 @deffn {Scheme Procedure} make-vector len [fill]
728 @deffnx {C Function} scm_make_vector (len, fill)
729 Return a newly allocated vector of @var{len} elements. If a
730 second argument is given, then each position is initialized to
731 @var{fill}. Otherwise the initial contents of each position is
732 unspecified.
733 @end deffn
734
735 @deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill)
736 Like @code{scm_make_vector}, but the length is given as a @code{size_t}.
737 @end deftypefn
738
739 To check whether an arbitrary Scheme value @emph{is} a vector, use the
740 @code{vector?} primitive:
741
742 @rnindex vector?
743 @deffn {Scheme Procedure} vector? obj
744 @deffnx {C Function} scm_vector_p (obj)
745 Return @code{#t} if @var{obj} is a vector, otherwise return
746 @code{#f}.
747 @end deffn
748
749 @deftypefn {C Function} int scm_is_vector (SCM obj)
750 Return non-zero when @var{obj} is a vector, otherwise return
751 @code{zero}.
752 @end deftypefn
753
754 @node Vector Accessors
755 @subsubsection Accessing and Modifying Vector Contents
756
757 @code{vector-length} and @code{vector-ref} return information about a
758 given vector, respectively its size and the elements that are contained
759 in the vector.
760
761 @rnindex vector-length
762 @deffn {Scheme Procedure} vector-length vector
763 @deffnx {C Function} scm_vector_length vector
764 Return the number of elements in @var{vector} as an exact integer.
765 @end deffn
766
767 @deftypefn {C Function} size_t scm_c_vector_length (SCM v)
768 Return the number of elements in @var{vector} as a @code{size_t}.
769 @end deftypefn
770
771 @rnindex vector-ref
772 @deffn {Scheme Procedure} vector-ref vector k
773 @deffnx {C Function} scm_vector_ref vector k
774 Return the contents of position @var{k} of @var{vector}.
775 @var{k} must be a valid index of @var{vector}.
776 @lisp
777 (vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8
778 (vector-ref '#(1 1 2 3 5 8 13 21)
779 (let ((i (round (* 2 (acos -1)))))
780 (if (inexact? i)
781 (inexact->exact i)
782 i))) @result{} 13
783 @end lisp
784 @end deffn
785
786 @deftypefn {C Function} SCM scm_c_vector_ref (SCM v, size_t k)
787 Return the contents of position @var{k} (a @code{size_t}) of
788 @var{vector}.
789 @end deftypefn
790
791 A vector created by one of the dynamic vector constructor procedures
792 (@pxref{Vector Creation}) can be modified using the following
793 procedures.
794
795 @emph{NOTE:} According to R5RS, it is an error to use any of these
796 procedures on a literally read vector, because such vectors should be
797 considered as constants. Currently, however, Guile does not detect this
798 error.
799
800 @rnindex vector-set!
801 @deffn {Scheme Procedure} vector-set! vector k obj
802 @deffnx {C Function} scm_vector_set_x vector k obj
803 Store @var{obj} in position @var{k} of @var{vector}.
804 @var{k} must be a valid index of @var{vector}.
805 The value returned by @samp{vector-set!} is unspecified.
806 @lisp
807 (let ((vec (vector 0 '(2 2 2 2) "Anna")))
808 (vector-set! vec 1 '("Sue" "Sue"))
809 vec) @result{} #(0 ("Sue" "Sue") "Anna")
810 @end lisp
811 @end deffn
812
813 @deftypefn {C Function} void scm_c_vector_set_x (SCM v, size_t k, SCM obj)
814 Store @var{obj} in position @var{k} (a @code{size_t}) of @var{v}.
815 @end deftypefn
816
817 @rnindex vector-fill!
818 @deffn {Scheme Procedure} vector-fill! v fill
819 @deffnx {C Function} scm_vector_fill_x (v, fill)
820 Store @var{fill} in every position of @var{vector}. The value
821 returned by @code{vector-fill!} is unspecified.
822 @end deffn
823
824 @deffn {Scheme Procedure} vector-copy vec
825 @deffnx {C Function} scm_vector_copy (vec)
826 Return a copy of @var{vec}.
827 @end deffn
828
829 @deffn {Scheme Procedure} vector-move-left! vec1 start1 end1 vec2 start2
830 @deffnx {C Function} scm_vector_move_left_x (vec1, start1, end1, vec2, start2)
831 Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
832 to @var{vec2} starting at position @var{start2}. @var{start1} and
833 @var{start2} are inclusive indices; @var{end1} is exclusive.
834
835 @code{vector-move-left!} copies elements in leftmost order.
836 Therefore, in the case where @var{vec1} and @var{vec2} refer to the
837 same vector, @code{vector-move-left!} is usually appropriate when
838 @var{start1} is greater than @var{start2}.
839 @end deffn
840
841 @deffn {Scheme Procedure} vector-move-right! vec1 start1 end1 vec2 start2
842 @deffnx {C Function} scm_vector_move_right_x (vec1, start1, end1, vec2, start2)
843 Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
844 to @var{vec2} starting at position @var{start2}. @var{start1} and
845 @var{start2} are inclusive indices; @var{end1} is exclusive.
846
847 @code{vector-move-right!} copies elements in rightmost order.
848 Therefore, in the case where @var{vec1} and @var{vec2} refer to the
849 same vector, @code{vector-move-right!} is usually appropriate when
850 @var{start1} is less than @var{start2}.
851 @end deffn
852
853 @node Vector Accessing from C
854 @subsubsection Vector Accessing from C
855
856 A vector can be read and modified from C with the functions
857 @code{scm_c_vector_ref} and @code{scm_c_vector_set_x}, for example. In
858 addition to these functions, there are two more ways to access vectors
859 from C that might be more efficient in certain situations: you can
860 restrict yourself to @dfn{simple vectors} and then use the very fast
861 @emph{simple vector macros}; or you can use the very general framework
862 for accessing all kinds of arrays (@pxref{Accessing Arrays from C}),
863 which is more verbose, but can deal efficiently with all kinds of
864 vectors (and arrays). For vectors, you can use the
865 @code{scm_vector_elements} and @code{scm_vector_writable_elements}
866 functions as shortcuts.
867
868 @deftypefn {C Function} int scm_is_simple_vector (SCM obj)
869 Return non-zero if @var{obj} is a simple vector, else return zero. A
870 simple vector is a vector that can be used with the @code{SCM_SIMPLE_*}
871 macros below.
872
873 The following functions are guaranteed to return simple vectors:
874 @code{scm_make_vector}, @code{scm_c_make_vector}, @code{scm_vector},
875 @code{scm_list_to_vector}.
876 @end deftypefn
877
878 @deftypefn {C Macro} size_t SCM_SIMPLE_VECTOR_LENGTH (SCM vec)
879 Evaluates to the length of the simple vector @var{vec}. No type
880 checking is done.
881 @end deftypefn
882
883 @deftypefn {C Macro} SCM SCM_SIMPLE_VECTOR_REF (SCM vec, size_t idx)
884 Evaluates to the element at position @var{idx} in the simple vector
885 @var{vec}. No type or range checking is done.
886 @end deftypefn
887
888 @deftypefn {C Macro} void SCM_SIMPLE_VECTOR_SET (SCM vec, size_t idx, SCM val)
889 Sets the element at position @var{idx} in the simple vector
890 @var{vec} to @var{val}. No type or range checking is done.
891 @end deftypefn
892
893 @deftypefn {C Function} {const SCM *} scm_vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
894 Acquire a handle for the vector @var{vec} and return a pointer to the
895 elements of it. This pointer can only be used to read the elements of
896 @var{vec}. When @var{vec} is not a vector, an error is signaled. The
897 handle mustr eventually be released with
898 @code{scm_array_handle_release}.
899
900 The variables pointed to by @var{lenp} and @var{incp} are filled with
901 the number of elements of the vector and the increment (number of
902 elements) between successive elements, respectively. Successive
903 elements of @var{vec} need not be contiguous in their underlying
904 ``root vector'' returned here; hence the increment is not necessarily
905 equal to 1 and may well be negative too (@pxref{Shared Arrays}).
906
907 The following example shows the typical way to use this function. It
908 creates a list of all elements of @var{vec} (in reverse order).
909
910 @example
911 scm_t_array_handle handle;
912 size_t i, len;
913 ssize_t inc;
914 const SCM *elt;
915 SCM list;
916
917 elt = scm_vector_elements (vec, &handle, &len, &inc);
918 list = SCM_EOL;
919 for (i = 0; i < len; i++, elt += inc)
920 list = scm_cons (*elt, list);
921 scm_array_handle_release (&handle);
922 @end example
923
924 @end deftypefn
925
926 @deftypefn {C Function} {SCM *} scm_vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
927 Like @code{scm_vector_elements} but the pointer can be used to modify
928 the vector.
929
930 The following example shows the typical way to use this function. It
931 fills a vector with @code{#t}.
932
933 @example
934 scm_t_array_handle handle;
935 size_t i, len;
936 ssize_t inc;
937 SCM *elt;
938
939 elt = scm_vector_writable_elements (vec, &handle, &len, &inc);
940 for (i = 0; i < len; i++, elt += inc)
941 *elt = SCM_BOOL_T;
942 scm_array_handle_release (&handle);
943 @end example
944
945 @end deftypefn
946
947 @node Uniform Numeric Vectors
948 @subsection Uniform Numeric Vectors
949
950 A uniform numeric vector is a vector whose elements are all of a single
951 numeric type. Guile offers uniform numeric vectors for signed and
952 unsigned 8-bit, 16-bit, 32-bit, and 64-bit integers, two sizes of
953 floating point values, and complex floating-point numbers of these two
954 sizes.
955
956 Strings could be regarded as uniform vectors of characters,
957 @xref{Strings}. Likewise, bit vectors could be regarded as uniform
958 vectors of bits, @xref{Bit Vectors}. Both are sufficiently different
959 from uniform numeric vectors that the procedures described here do not
960 apply to these two data types. However, both strings and bit vectors
961 are generalized vectors, @xref{Generalized Vectors}, and arrays,
962 @xref{Arrays}.
963
964 Uniform numeric vectors are the special case of one dimensional uniform
965 numeric arrays.
966
967 Uniform numeric vectors can be useful since they consume less memory
968 than the non-uniform, general vectors. Also, since the types they can
969 store correspond directly to C types, it is easier to work with them
970 efficiently on a low level. Consider image processing as an example,
971 where you want to apply a filter to some image. While you could store
972 the pixels of an image in a general vector and write a general
973 convolution function, things are much more efficient with uniform
974 vectors: the convolution function knows that all pixels are unsigned
975 8-bit values (say), and can use a very tight inner loop.
976
977 That is, when it is written in C. Functions for efficiently working
978 with uniform numeric vectors from C are listed at the end of this
979 section.
980
981 Procedures similar to the vector procedures (@pxref{Vectors}) are
982 provided for handling these uniform vectors, but they are distinct
983 datatypes and the two cannot be inter-mixed. If you want to work
984 primarily with uniform numeric vectors, but want to offer support for
985 general vectors as a convenience, you can use one of the
986 @code{scm_any_to_*} functions. They will coerce lists and vectors to
987 the given type of uniform vector. Alternatively, you can write two
988 versions of your code: one that is fast and works only with uniform
989 numeric vectors, and one that works with any kind of vector but is
990 slower.
991
992 One set of the procedures listed below is a generic one: it works with
993 all types of uniform numeric vectors. In addition to that, there is a
994 set of procedures for each type that only works with that type. Unless
995 you really need to the generality of the first set, it is best to use
996 the more specific functions. They might not be that much faster, but
997 their use can serve as a kind of declaration and makes it easier to
998 optimize later on.
999
1000 The generic set of procedures uses @code{uniform} in its names, the
1001 specific ones use the tag from the following table.
1002
1003 @table @nicode
1004 @item u8
1005 unsigned 8-bit integers
1006
1007 @item s8
1008 signed 8-bit integers
1009
1010 @item u16
1011 unsigned 16-bit integers
1012
1013 @item s16
1014 signed 16-bit integers
1015
1016 @item u32
1017 unsigned 32-bit integers
1018
1019 @item s32
1020 signed 32-bit integers
1021
1022 @item u64
1023 unsigned 64-bit integers
1024
1025 @item s64
1026 signed 64-bit integers
1027
1028 @item f32
1029 the C type @code{float}
1030
1031 @item f64
1032 the C type @code{double}
1033
1034 @item c32
1035 complex numbers in rectangular form with the real and imaginary part
1036 being a @code{float}
1037
1038 @item c64
1039 complex numbers in rectangular form with the real and imaginary part
1040 being a @code{double}
1041
1042 @end table
1043
1044 The external representation (ie.@: read syntax) for these vectors is
1045 similar to normal Scheme vectors, but with an additional tag from the
1046 tabel above indiciating the vector's type. For example,
1047
1048 @lisp
1049 #u16(1 2 3)
1050 #f64(3.1415 2.71)
1051 @end lisp
1052
1053 Note that the read syntax for floating-point here conflicts with
1054 @code{#f} for false. In Standard Scheme one can write @code{(1 #f3)}
1055 for a three element list @code{(1 #f 3)}, but for Guile @code{(1 #f3)}
1056 is invalid. @code{(1 #f 3)} is almost certainly what one should write
1057 anyway to make the intention clear, so this is rarely a problem.
1058
1059 @deffn {Scheme Procedure} uniform-vector? obj
1060 @deffnx {Scheme Procedure} u8vector? obj
1061 @deffnx {Scheme Procedure} s8vector? obj
1062 @deffnx {Scheme Procedure} u16vector? obj
1063 @deffnx {Scheme Procedure} s16vector? obj
1064 @deffnx {Scheme Procedure} u32vector? obj
1065 @deffnx {Scheme Procedure} s32vector? obj
1066 @deffnx {Scheme Procedure} u64vector? obj
1067 @deffnx {Scheme Procedure} s64vector? obj
1068 @deffnx {Scheme Procedure} f32vector? obj
1069 @deffnx {Scheme Procedure} f64vector? obj
1070 @deffnx {Scheme Procedure} c32vector? obj
1071 @deffnx {Scheme Procedure} c64vector? obj
1072 @deffnx {C Function} scm_uniform_vector_p (obj)
1073 @deffnx {C Function} scm_u8vector_p (obj)
1074 @deffnx {C Function} scm_s8vector_p (obj)
1075 @deffnx {C Function} scm_u16vector_p (obj)
1076 @deffnx {C Function} scm_s16vector_p (obj)
1077 @deffnx {C Function} scm_u32vector_p (obj)
1078 @deffnx {C Function} scm_s32vector_p (obj)
1079 @deffnx {C Function} scm_u64vector_p (obj)
1080 @deffnx {C Function} scm_s64vector_p (obj)
1081 @deffnx {C Function} scm_f32vector_p (obj)
1082 @deffnx {C Function} scm_f64vector_p (obj)
1083 @deffnx {C Function} scm_c32vector_p (obj)
1084 @deffnx {C Function} scm_c64vector_p (obj)
1085 Return @code{#t} if @var{obj} is a homogeneous numeric vector of the
1086 indicated type.
1087 @end deffn
1088
1089 @deffn {Scheme Procedure} make-u8vector n [value]
1090 @deffnx {Scheme Procedure} make-s8vector n [value]
1091 @deffnx {Scheme Procedure} make-u16vector n [value]
1092 @deffnx {Scheme Procedure} make-s16vector n [value]
1093 @deffnx {Scheme Procedure} make-u32vector n [value]
1094 @deffnx {Scheme Procedure} make-s32vector n [value]
1095 @deffnx {Scheme Procedure} make-u64vector n [value]
1096 @deffnx {Scheme Procedure} make-s64vector n [value]
1097 @deffnx {Scheme Procedure} make-f32vector n [value]
1098 @deffnx {Scheme Procedure} make-f64vector n [value]
1099 @deffnx {Scheme Procedure} make-c32vector n [value]
1100 @deffnx {Scheme Procedure} make-c64vector n [value]
1101 @deffnx {C Function} scm_make_u8vector n [value]
1102 @deffnx {C Function} scm_make_s8vector n [value]
1103 @deffnx {C Function} scm_make_u16vector n [value]
1104 @deffnx {C Function} scm_make_s16vector n [value]
1105 @deffnx {C Function} scm_make_u32vector n [value]
1106 @deffnx {C Function} scm_make_s32vector n [value]
1107 @deffnx {C Function} scm_make_u64vector n [value]
1108 @deffnx {C Function} scm_make_s64vector n [value]
1109 @deffnx {C Function} scm_make_f32vector n [value]
1110 @deffnx {C Function} scm_make_f64vector n [value]
1111 @deffnx {C Function} scm_make_c32vector n [value]
1112 @deffnx {C Function} scm_make_c64vector n [value]
1113 Return a newly allocated homogeneous numeric vector holding @var{n}
1114 elements of the indicated type. If @var{value} is given, the vector
1115 is initialized with that value, otherwise the contents are
1116 unspecified.
1117 @end deffn
1118
1119 @deffn {Scheme Procedure} u8vector value @dots{}
1120 @deffnx {Scheme Procedure} s8vector value @dots{}
1121 @deffnx {Scheme Procedure} u16vector value @dots{}
1122 @deffnx {Scheme Procedure} s16vector value @dots{}
1123 @deffnx {Scheme Procedure} u32vector value @dots{}
1124 @deffnx {Scheme Procedure} s32vector value @dots{}
1125 @deffnx {Scheme Procedure} u64vector value @dots{}
1126 @deffnx {Scheme Procedure} s64vector value @dots{}
1127 @deffnx {Scheme Procedure} f32vector value @dots{}
1128 @deffnx {Scheme Procedure} f64vector value @dots{}
1129 @deffnx {Scheme Procedure} c32vector value @dots{}
1130 @deffnx {Scheme Procedure} c64vector value @dots{}
1131 @deffnx {C Function} scm_u8vector (values)
1132 @deffnx {C Function} scm_s8vector (values)
1133 @deffnx {C Function} scm_u16vector (values)
1134 @deffnx {C Function} scm_s16vector (values)
1135 @deffnx {C Function} scm_u32vector (values)
1136 @deffnx {C Function} scm_s32vector (values)
1137 @deffnx {C Function} scm_u64vector (values)
1138 @deffnx {C Function} scm_s64vector (values)
1139 @deffnx {C Function} scm_f32vector (values)
1140 @deffnx {C Function} scm_f64vector (values)
1141 @deffnx {C Function} scm_c32vector (values)
1142 @deffnx {C Function} scm_c64vector (values)
1143 Return a newly allocated homogeneous numeric vector of the indicated
1144 type, holding the given parameter @var{value}s. The vector length is
1145 the number of parameters given.
1146 @end deffn
1147
1148 @deffn {Scheme Procedure} uniform-vector-length vec
1149 @deffnx {Scheme Procedure} u8vector-length vec
1150 @deffnx {Scheme Procedure} s8vector-length vec
1151 @deffnx {Scheme Procedure} u16vector-length vec
1152 @deffnx {Scheme Procedure} s16vector-length vec
1153 @deffnx {Scheme Procedure} u32vector-length vec
1154 @deffnx {Scheme Procedure} s32vector-length vec
1155 @deffnx {Scheme Procedure} u64vector-length vec
1156 @deffnx {Scheme Procedure} s64vector-length vec
1157 @deffnx {Scheme Procedure} f32vector-length vec
1158 @deffnx {Scheme Procedure} f64vector-length vec
1159 @deffnx {Scheme Procedure} c32vector-length vec
1160 @deffnx {Scheme Procedure} c64vector-length vec
1161 @deffnx {C Function} scm_uniform_vector_length (vec)
1162 @deffnx {C Function} scm_u8vector_length (vec)
1163 @deffnx {C Function} scm_s8vector_length (vec)
1164 @deffnx {C Function} scm_u16vector_length (vec)
1165 @deffnx {C Function} scm_s16vector_length (vec)
1166 @deffnx {C Function} scm_u32vector_length (vec)
1167 @deffnx {C Function} scm_s32vector_length (vec)
1168 @deffnx {C Function} scm_u64vector_length (vec)
1169 @deffnx {C Function} scm_s64vector_length (vec)
1170 @deffnx {C Function} scm_f32vector_length (vec)
1171 @deffnx {C Function} scm_f64vector_length (vec)
1172 @deffnx {C Function} scm_c32vector_length (vec)
1173 @deffnx {C Function} scm_c64vector_length (vec)
1174 Return the number of elements in @var{vec}.
1175 @end deffn
1176
1177 @deffn {Scheme Procedure} uniform-vector-ref vec i
1178 @deffnx {Scheme Procedure} u8vector-ref vec i
1179 @deffnx {Scheme Procedure} s8vector-ref vec i
1180 @deffnx {Scheme Procedure} u16vector-ref vec i
1181 @deffnx {Scheme Procedure} s16vector-ref vec i
1182 @deffnx {Scheme Procedure} u32vector-ref vec i
1183 @deffnx {Scheme Procedure} s32vector-ref vec i
1184 @deffnx {Scheme Procedure} u64vector-ref vec i
1185 @deffnx {Scheme Procedure} s64vector-ref vec i
1186 @deffnx {Scheme Procedure} f32vector-ref vec i
1187 @deffnx {Scheme Procedure} f64vector-ref vec i
1188 @deffnx {Scheme Procedure} c32vector-ref vec i
1189 @deffnx {Scheme Procedure} c64vector-ref vec i
1190 @deffnx {C Function} scm_uniform_vector_ref (vec i)
1191 @deffnx {C Function} scm_u8vector_ref (vec i)
1192 @deffnx {C Function} scm_s8vector_ref (vec i)
1193 @deffnx {C Function} scm_u16vector_ref (vec i)
1194 @deffnx {C Function} scm_s16vector_ref (vec i)
1195 @deffnx {C Function} scm_u32vector_ref (vec i)
1196 @deffnx {C Function} scm_s32vector_ref (vec i)
1197 @deffnx {C Function} scm_u64vector_ref (vec i)
1198 @deffnx {C Function} scm_s64vector_ref (vec i)
1199 @deffnx {C Function} scm_f32vector_ref (vec i)
1200 @deffnx {C Function} scm_f64vector_ref (vec i)
1201 @deffnx {C Function} scm_c32vector_ref (vec i)
1202 @deffnx {C Function} scm_c64vector_ref (vec i)
1203 Return the element at index @var{i} in @var{vec}. The first element
1204 in @var{vec} is index 0.
1205 @end deffn
1206
1207 @deffn {Scheme Procedure} uniform-vector-set! vec i value
1208 @deffnx {Scheme Procedure} u8vector-set! vec i value
1209 @deffnx {Scheme Procedure} s8vector-set! vec i value
1210 @deffnx {Scheme Procedure} u16vector-set! vec i value
1211 @deffnx {Scheme Procedure} s16vector-set! vec i value
1212 @deffnx {Scheme Procedure} u32vector-set! vec i value
1213 @deffnx {Scheme Procedure} s32vector-set! vec i value
1214 @deffnx {Scheme Procedure} u64vector-set! vec i value
1215 @deffnx {Scheme Procedure} s64vector-set! vec i value
1216 @deffnx {Scheme Procedure} f32vector-set! vec i value
1217 @deffnx {Scheme Procedure} f64vector-set! vec i value
1218 @deffnx {Scheme Procedure} c32vector-set! vec i value
1219 @deffnx {Scheme Procedure} c64vector-set! vec i value
1220 @deffnx {C Function} scm_uniform_vector_set_x (vec i value)
1221 @deffnx {C Function} scm_u8vector_set_x (vec i value)
1222 @deffnx {C Function} scm_s8vector_set_x (vec i value)
1223 @deffnx {C Function} scm_u16vector_set_x (vec i value)
1224 @deffnx {C Function} scm_s16vector_set_x (vec i value)
1225 @deffnx {C Function} scm_u32vector_set_x (vec i value)
1226 @deffnx {C Function} scm_s32vector_set_x (vec i value)
1227 @deffnx {C Function} scm_u64vector_set_x (vec i value)
1228 @deffnx {C Function} scm_s64vector_set_x (vec i value)
1229 @deffnx {C Function} scm_f32vector_set_x (vec i value)
1230 @deffnx {C Function} scm_f64vector_set_x (vec i value)
1231 @deffnx {C Function} scm_c32vector_set_x (vec i value)
1232 @deffnx {C Function} scm_c64vector_set_x (vec i value)
1233 Set the element at index @var{i} in @var{vec} to @var{value}. The
1234 first element in @var{vec} is index 0. The return value is
1235 unspecified.
1236 @end deffn
1237
1238 @deffn {Scheme Procedure} uniform-vector->list vec
1239 @deffnx {Scheme Procedure} u8vector->list vec
1240 @deffnx {Scheme Procedure} s8vector->list vec
1241 @deffnx {Scheme Procedure} u16vector->list vec
1242 @deffnx {Scheme Procedure} s16vector->list vec
1243 @deffnx {Scheme Procedure} u32vector->list vec
1244 @deffnx {Scheme Procedure} s32vector->list vec
1245 @deffnx {Scheme Procedure} u64vector->list vec
1246 @deffnx {Scheme Procedure} s64vector->list vec
1247 @deffnx {Scheme Procedure} f32vector->list vec
1248 @deffnx {Scheme Procedure} f64vector->list vec
1249 @deffnx {Scheme Procedure} c32vector->list vec
1250 @deffnx {Scheme Procedure} c64vector->list vec
1251 @deffnx {C Function} scm_uniform_vector_to_list (vec)
1252 @deffnx {C Function} scm_u8vector_to_list (vec)
1253 @deffnx {C Function} scm_s8vector_to_list (vec)
1254 @deffnx {C Function} scm_u16vector_to_list (vec)
1255 @deffnx {C Function} scm_s16vector_to_list (vec)
1256 @deffnx {C Function} scm_u32vector_to_list (vec)
1257 @deffnx {C Function} scm_s32vector_to_list (vec)
1258 @deffnx {C Function} scm_u64vector_to_list (vec)
1259 @deffnx {C Function} scm_s64vector_to_list (vec)
1260 @deffnx {C Function} scm_f32vector_to_list (vec)
1261 @deffnx {C Function} scm_f64vector_to_list (vec)
1262 @deffnx {C Function} scm_c32vector_to_list (vec)
1263 @deffnx {C Function} scm_c64vector_to_list (vec)
1264 Return a newly allocated list holding all elements of @var{vec}.
1265 @end deffn
1266
1267 @deffn {Scheme Procedure} list->u8vector lst
1268 @deffnx {Scheme Procedure} list->s8vector lst
1269 @deffnx {Scheme Procedure} list->u16vector lst
1270 @deffnx {Scheme Procedure} list->s16vector lst
1271 @deffnx {Scheme Procedure} list->u32vector lst
1272 @deffnx {Scheme Procedure} list->s32vector lst
1273 @deffnx {Scheme Procedure} list->u64vector lst
1274 @deffnx {Scheme Procedure} list->s64vector lst
1275 @deffnx {Scheme Procedure} list->f32vector lst
1276 @deffnx {Scheme Procedure} list->f64vector lst
1277 @deffnx {Scheme Procedure} list->c32vector lst
1278 @deffnx {Scheme Procedure} list->c64vector lst
1279 @deffnx {C Function} scm_list_to_u8vector (lst)
1280 @deffnx {C Function} scm_list_to_s8vector (lst)
1281 @deffnx {C Function} scm_list_to_u16vector (lst)
1282 @deffnx {C Function} scm_list_to_s16vector (lst)
1283 @deffnx {C Function} scm_list_to_u32vector (lst)
1284 @deffnx {C Function} scm_list_to_s32vector (lst)
1285 @deffnx {C Function} scm_list_to_u64vector (lst)
1286 @deffnx {C Function} scm_list_to_s64vector (lst)
1287 @deffnx {C Function} scm_list_to_f32vector (lst)
1288 @deffnx {C Function} scm_list_to_f64vector (lst)
1289 @deffnx {C Function} scm_list_to_c32vector (lst)
1290 @deffnx {C Function} scm_list_to_c64vector (lst)
1291 Return a newly allocated homogeneous numeric vector of the indicated type,
1292 initialized with the elements of the list @var{lst}.
1293 @end deffn
1294
1295 @deffn {Scheme Procedure} any->u8vector obj
1296 @deffnx {Scheme Procedure} any->s8vector obj
1297 @deffnx {Scheme Procedure} any->u16vector obj
1298 @deffnx {Scheme Procedure} any->s16vector obj
1299 @deffnx {Scheme Procedure} any->u32vector obj
1300 @deffnx {Scheme Procedure} any->s32vector obj
1301 @deffnx {Scheme Procedure} any->u64vector obj
1302 @deffnx {Scheme Procedure} any->s64vector obj
1303 @deffnx {Scheme Procedure} any->f32vector obj
1304 @deffnx {Scheme Procedure} any->f64vector obj
1305 @deffnx {Scheme Procedure} any->c32vector obj
1306 @deffnx {Scheme Procedure} any->c64vector obj
1307 @deffnx {C Function} scm_any_to_u8vector (obj)
1308 @deffnx {C Function} scm_any_to_s8vector (obj)
1309 @deffnx {C Function} scm_any_to_u16vector (obj)
1310 @deffnx {C Function} scm_any_to_s16vector (obj)
1311 @deffnx {C Function} scm_any_to_u32vector (obj)
1312 @deffnx {C Function} scm_any_to_s32vector (obj)
1313 @deffnx {C Function} scm_any_to_u64vector (obj)
1314 @deffnx {C Function} scm_any_to_s64vector (obj)
1315 @deffnx {C Function} scm_any_to_f32vector (obj)
1316 @deffnx {C Function} scm_any_to_f64vector (obj)
1317 @deffnx {C Function} scm_any_to_c32vector (obj)
1318 @deffnx {C Function} scm_any_to_c64vector (obj)
1319 Return a (maybe newly allocated) uniform numeric vector of the indicated
1320 type, initialized with the elements of @var{obj}, which must be a list,
1321 a vector, or a uniform vector. When @var{obj} is already a suitable
1322 uniform numeric vector, it is returned unchanged.
1323 @end deffn
1324
1325 @deftypefn {C Function} int scm_is_uniform_vector (SCM uvec)
1326 Return non-zero when @var{uvec} is a uniform numeric vector, zero
1327 otherwise.
1328 @end deftypefn
1329
1330 @deftypefn {C Function} SCM scm_take_u8vector (const scm_t_uint8 *data, size_t len)
1331 @deftypefnx {C Function} SCM scm_take_s8vector (const scm_t_int8 *data, size_t len)
1332 @deftypefnx {C Function} SCM scm_take_u16vector (const scm_t_uint16 *data, size_t len)
1333 @deftypefnx {C Function} SCM scm_take_s168vector (const scm_t_int16 *data, size_t len)
1334 @deftypefnx {C Function} SCM scm_take_u32vector (const scm_t_uint32 *data, size_t len)
1335 @deftypefnx {C Function} SCM scm_take_s328vector (const scm_t_int32 *data, size_t len)
1336 @deftypefnx {C Function} SCM scm_take_u64vector (const scm_t_uint64 *data, size_t len)
1337 @deftypefnx {C Function} SCM scm_take_s64vector (const scm_t_int64 *data, size_t len)
1338 @deftypefnx {C Function} SCM scm_take_f32vector (const float *data, size_t len)
1339 @deftypefnx {C Function} SCM scm_take_f64vector (const double *data, size_t len)
1340 @deftypefnx {C Function} SCM scm_take_c32vector (const float *data, size_t len)
1341 @deftypefnx {C Function} SCM scm_take_c64vector (const double *data, size_t len)
1342 Return a new uniform numeric vector of the indicated type and length
1343 that uses the memory pointed to by @var{data} to store its elements.
1344 This memory will eventually be freed with @code{free}. The argument
1345 @var{len} specifies the number of elements in @var{data}, not its size
1346 in bytes.
1347
1348 The @code{c32} and @code{c64} variants take a pointer to a C array of
1349 @code{float}s or @code{double}s. The real parts of the complex numbers
1350 are at even indices in that array, the corresponding imaginary parts are
1351 at the following odd index.
1352 @end deftypefn
1353
1354 @deftypefn {C Function} size_t scm_c_uniform_vector_length (SCM uvec)
1355 Return the number of elements of @var{uvec} as a @code{size_t}.
1356 @end deftypefn
1357
1358 @deftypefn {C Function} {const void *} scm_uniform_vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1359 @deftypefnx {C Function} {const scm_t_uint8 *} scm_u8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1360 @deftypefnx {C Function} {const scm_t_int8 *} scm_s8vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1361 @deftypefnx {C Function} {const scm_t_uint16 *} scm_u16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1362 @deftypefnx {C Function} {const scm_t_int16 *} scm_s16vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1363 @deftypefnx {C Function} {const scm_t_uint32 *} scm_u32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1364 @deftypefnx {C Function} {const scm_t_int32 *} scm_s32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1365 @deftypefnx {C Function} {const scm_t_uint64 *} scm_u64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1366 @deftypefnx {C Function} {const scm_t_int64 *} scm_s64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1367 @deftypefnx {C Function} {const float *} scm_f23vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1368 @deftypefnx {C Function} {const double *} scm_f64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1369 @deftypefnx {C Function} {const float *} scm_c32vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1370 @deftypefnx {C Function} {const double *} scm_c64vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1371 Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
1372 returns a pointer to the elements of a uniform numeric vector of the
1373 indicated kind.
1374 @end deftypefn
1375
1376 @deftypefn {C Function} {void *} scm_uniform_vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1377 @deftypefnx {C Function} {scm_t_uint8 *} scm_u8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1378 @deftypefnx {C Function} {scm_t_int8 *} scm_s8vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1379 @deftypefnx {C Function} {scm_t_uint16 *} scm_u16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1380 @deftypefnx {C Function} {scm_t_int16 *} scm_s16vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1381 @deftypefnx {C Function} {scm_t_uint32 *} scm_u32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1382 @deftypefnx {C Function} {scm_t_int32 *} scm_s32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1383 @deftypefnx {C Function} {scm_t_uint64 *} scm_u64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1384 @deftypefnx {C Function} {scm_t_int64 *} scm_s64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1385 @deftypefnx {C Function} {float *} scm_f23vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1386 @deftypefnx {C Function} {double *} scm_f64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1387 @deftypefnx {C Function} {float *} scm_c32vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1388 @deftypefnx {C Function} {double *} scm_c64vector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp)
1389 Like @code{scm_vector_writable_elements} (@pxref{Vector Accessing from
1390 C}), but returns a pointer to the elements of a uniform numeric vector
1391 of the indicated kind.
1392 @end deftypefn
1393
1394 @deffn {Scheme Procedure} uniform-vector-read! uvec [port_or_fd [start [end]]]
1395 @deffnx {C Function} scm_uniform_vector_read_x (uvec, port_or_fd, start, end)
1396 Fill the elements of @var{uvec} by reading
1397 raw bytes from @var{port-or-fdes}, using host byte order.
1398
1399 The optional arguments @var{start} (inclusive) and @var{end}
1400 (exclusive) allow a specified region to be read,
1401 leaving the remainder of the vector unchanged.
1402
1403 When @var{port-or-fdes} is a port, all specified elements
1404 of @var{uvec} are attempted to be read, potentially blocking
1405 while waiting formore input or end-of-file.
1406 When @var{port-or-fd} is an integer, a single call to
1407 read(2) is made.
1408
1409 An error is signalled when the last element has only
1410 been partially filled before reaching end-of-file or in
1411 the single call to read(2).
1412
1413 @code{uniform-vector-read!} returns the number of elements
1414 read.
1415
1416 @var{port-or-fdes} may be omitted, in which case it defaults
1417 to the value returned by @code{(current-input-port)}.
1418 @end deffn
1419
1420 @deffn {Scheme Procedure} uniform-vector-write uvec [port_or_fd [start [end]]]
1421 @deffnx {C Function} scm_uniform_vector_write (uvec, port_or_fd, start, end)
1422 Write the elements of @var{uvec} as raw bytes to
1423 @var{port-or-fdes}, in the host byte order.
1424
1425 The optional arguments @var{start} (inclusive)
1426 and @var{end} (exclusive) allow
1427 a specified region to be written.
1428
1429 When @var{port-or-fdes} is a port, all specified elements
1430 of @var{uvec} are attempted to be written, potentially blocking
1431 while waiting for more room.
1432 When @var{port-or-fd} is an integer, a single call to
1433 write(2) is made.
1434
1435 An error is signalled when the last element has only
1436 been partially written in the single call to write(2).
1437
1438 The number of objects actually written is returned.
1439 @var{port-or-fdes} may be
1440 omitted, in which case it defaults to the value returned by
1441 @code{(current-output-port)}.
1442 @end deffn
1443
1444
1445 @node Bit Vectors
1446 @subsection Bit Vectors
1447
1448 @noindent
1449 Bit vectors are zero-origin, one-dimensional arrays of booleans. They
1450 are displayed as a sequence of @code{0}s and @code{1}s prefixed by
1451 @code{#*}, e.g.,
1452
1453 @example
1454 (make-bitvector 8 #f) @result{}
1455 #*00000000
1456 @end example
1457
1458 Bit vectors are are also generalized vectors, @xref{Generalized
1459 Vectors}, and can thus be used with the array procedures, @xref{Arrays}.
1460 Bit vectors are the special case of one dimensional bit arrays.
1461
1462 @deffn {Scheme Procedure} bitvector? obj
1463 @deffnx {C Function} scm_bitvector_p (obj)
1464 Return @code{#t} when @var{obj} is a bitvector, else
1465 return @code{#f}.
1466 @end deffn
1467
1468 @deftypefn {C Function} int scm_is_bitvector (SCM obj)
1469 Return @code{1} when @var{obj} is a bitvector, else return @code{0}.
1470 @end deftypefn
1471
1472 @deffn {Scheme Procedure} make-bitvector len [fill]
1473 @deffnx {C Function} scm_make_bitvector (len, fill)
1474 Create a new bitvector of length @var{len} and
1475 optionally initialize all elements to @var{fill}.
1476 @end deffn
1477
1478 @deftypefn {C Function} SCM scm_c_make_bitvector (size_t len, SCM fill)
1479 Like @code{scm_make_bitvector}, but the length is given as a
1480 @code{size_t}.
1481 @end deftypefn
1482
1483 @deffn {Scheme Procedure} bitvector . bits
1484 @deffnx {C Function} scm_bitvector (bits)
1485 Create a new bitvector with the arguments as elements.
1486 @end deffn
1487
1488 @deffn {Scheme Procedure} bitvector-length vec
1489 @deffnx {C Function} scm_bitvector_length (vec)
1490 Return the length of the bitvector @var{vec}.
1491 @end deffn
1492
1493 @deftypefn {C Function} size_t scm_c_bitvector_length (SCM vec)
1494 Like @code{scm_bitvector_length}, but the length is returned as a
1495 @code{size_t}.
1496 @end deftypefn
1497
1498 @deffn {Scheme Procedure} bitvector-ref vec idx
1499 @deffnx {C Function} scm_bitvector_ref (vec, idx)
1500 Return the element at index @var{idx} of the bitvector
1501 @var{vec}.
1502 @end deffn
1503
1504 @deftypefn {C Function} SCM scm_c_bitvector_ref (SCM obj, size_t idx)
1505 Return the element at index @var{idx} of the bitvector
1506 @var{vec}.
1507 @end deftypefn
1508
1509 @deffn {Scheme Procedure} bitvector-set! vec idx val
1510 @deffnx {C Function} scm_bitvector_set_x (vec, idx, val)
1511 Set the element at index @var{idx} of the bitvector
1512 @var{vec} when @var{val} is true, else clear it.
1513 @end deffn
1514
1515 @deftypefn {C Function} SCM scm_c_bitvector_set_x (SCM obj, size_t idx, SCM val)
1516 Set the element at index @var{idx} of the bitvector
1517 @var{vec} when @var{val} is true, else clear it.
1518 @end deftypefn
1519
1520 @deffn {Scheme Procedure} bitvector-fill! vec val
1521 @deffnx {C Function} scm_bitvector_fill_x (vec, val)
1522 Set all elements of the bitvector
1523 @var{vec} when @var{val} is true, else clear them.
1524 @end deffn
1525
1526 @deffn {Scheme Procedure} list->bitvector list
1527 @deffnx {C Function} scm_list_to_bitvector (list)
1528 Return a new bitvector initialized with the elements
1529 of @var{list}.
1530 @end deffn
1531
1532 @deffn {Scheme Procedure} bitvector->list vec
1533 @deffnx {C Function} scm_bitvector_to_list (vec)
1534 Return a new list initialized with the elements
1535 of the bitvector @var{vec}.
1536 @end deffn
1537
1538 @deffn {Scheme Procedure} bit-count bool bitvector
1539 @deffnx {C Function} scm_bit_count (bool, bitvector)
1540 Return a count of how many entries in @var{bitvector} are equal to
1541 @var{bool}. For example,
1542
1543 @example
1544 (bit-count #f #*000111000) @result{} 6
1545 @end example
1546 @end deffn
1547
1548 @deffn {Scheme Procedure} bit-position bool bitvector start
1549 @deffnx {C Function} scm_bit_position (bool, bitvector, start)
1550 Return the index of the first occurrance of @var{bool} in
1551 @var{bitvector}, starting from @var{start}. If there is no @var{bool}
1552 entry between @var{start} and the end of @var{bitvector}, then return
1553 @code{#f}. For example,
1554
1555 @example
1556 (bit-position #t #*000101 0) @result{} 3
1557 (bit-position #f #*0001111 3) @result{} #f
1558 @end example
1559 @end deffn
1560
1561 @deffn {Scheme Procedure} bit-invert! bitvector
1562 @deffnx {C Function} scm_bit_invert_x (bitvector)
1563 Modify @var{bitvector} by replacing each element with its negation.
1564 @end deffn
1565
1566 @deffn {Scheme Procedure} bit-set*! bitvector uvec bool
1567 @deffnx {C Function} scm_bit_set_star_x (bitvector, uvec, bool)
1568 Set entries of @var{bitvector} to @var{bool}, with @var{uvec}
1569 selecting the entries to change. The return value is unspecified.
1570
1571 If @var{uvec} is a bit vector, then those entries where it has
1572 @code{#t} are the ones in @var{bitvector} which are set to @var{bool}.
1573 @var{uvec} and @var{bitvector} must be the same length. When
1574 @var{bool} is @code{#t} it's like @var{uvec} is OR'ed into
1575 @var{bitvector}. Or when @var{bool} is @code{#f} it can be seen as an
1576 ANDNOT.
1577
1578 @example
1579 (define bv #*01000010)
1580 (bit-set*! bv #*10010001 #t)
1581 bv
1582 @result{} #*11010011
1583 @end example
1584
1585 If @var{uvec} is a uniform vector of unsigned long integers, then
1586 they're indexes into @var{bitvector} which are set to @var{bool}.
1587
1588 @example
1589 (define bv #*01000010)
1590 (bit-set*! bv #u(5 2 7) #t)
1591 bv
1592 @result{} #*01100111
1593 @end example
1594 @end deffn
1595
1596 @deffn {Scheme Procedure} bit-count* bitvector uvec bool
1597 @deffnx {C Function} scm_bit_count_star (bitvector, uvec, bool)
1598 Return a count of how many entries in @var{bitvector} are equal to
1599 @var{bool}, with @var{uvec} selecting the entries to consider.
1600
1601 @var{uvec} is interpreted in the same way as for @code{bit-set*!}
1602 above. Namely, if @var{uvec} is a bit vector then entries which have
1603 @code{#t} there are considered in @var{bitvector}. Or if @var{uvec}
1604 is a uniform vector of unsigned long integers then it's the indexes in
1605 @var{bitvector} to consider.
1606
1607 For example,
1608
1609 @example
1610 (bit-count* #*01110111 #*11001101 #t) @result{} 3
1611 (bit-count* #*01110111 #u(7 0 4) #f) @result{} 2
1612 @end example
1613 @end deffn
1614
1615 @deftypefn {C Function} {const scm_t_uint32 *} scm_bitvector_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)
1616 Like @code{scm_vector_elements} (@pxref{Vector Accessing from C}), but
1617 for bitvectors. The variable pointed to by @var{offp} is set to the
1618 value returned by @code{scm_array_handle_bit_elements_offset}. See
1619 @code{scm_array_handle_bit_elements} for how to use the returned
1620 pointer and the offset.
1621 @end deftypefn
1622
1623 @deftypefn {C Function} {scm_t_uint32 *} scm_bitvector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)
1624 Like @code{scm_bitvector_elements}, but the pointer is good for reading
1625 and writing.
1626 @end deftypefn
1627
1628 @node Generalized Vectors
1629 @subsection Generalized Vectors
1630
1631 Guile has a number of data types that are generally vector-like:
1632 strings, uniform numeric vectors, bitvectors, and of course ordinary
1633 vectors of arbitrary Scheme values. These types are disjoint: a
1634 Scheme value belongs to at most one of the four types listed above.
1635
1636 If you want to gloss over this distinction and want to treat all four
1637 types with common code, you can use the procedures in this section.
1638 They work with the @emph{generalized vector} type, which is the union
1639 of the four vector-like types.
1640
1641 @deffn {Scheme Procedure} generalized-vector? obj
1642 @deffnx {C Function} scm_generalized_vector_p (obj)
1643 Return @code{#t} if @var{obj} is a vector, string,
1644 bitvector, or uniform numeric vector.
1645 @end deffn
1646
1647 @deffn {Scheme Procedure} generalized-vector-length v
1648 @deffnx {C Function} scm_generalized_vector_length (v)
1649 Return the length of the generalized vector @var{v}.
1650 @end deffn
1651
1652 @deffn {Scheme Procedure} generalized-vector-ref v idx
1653 @deffnx {C Function} scm_generalized_vector_ref (v, idx)
1654 Return the element at index @var{idx} of the
1655 generalized vector @var{v}.
1656 @end deffn
1657
1658 @deffn {Scheme Procedure} generalized-vector-set! v idx val
1659 @deffnx {C Function} scm_generalized_vector_set_x (v, idx, val)
1660 Set the element at index @var{idx} of the
1661 generalized vector @var{v} to @var{val}.
1662 @end deffn
1663
1664 @deffn {Scheme Procedure} generalized-vector->list v
1665 @deffnx {C Function} scm_generalized_vector_to_list (v)
1666 Return a new list whose elements are the elements of the
1667 generalized vector @var{v}.
1668 @end deffn
1669
1670 @deftypefn {C Function} int scm_is_generalized_vector (SCM obj)
1671 Return @code{1} if @var{obj} is a vector, string,
1672 bitvector, or uniform numeric vector; else return @code{0}.
1673 @end deftypefn
1674
1675 @deftypefn {C Function} size_t scm_c_generalized_vector_length (SCM v)
1676 Return the length of the generalized vector @var{v}.
1677 @end deftypefn
1678
1679 @deftypefn {C Function} SCM scm_c_generalized_vector_ref (SCM v, size_t idx)
1680 Return the element at index @var{idx} of the generalized vector @var{v}.
1681 @end deftypefn
1682
1683 @deftypefn {C Function} void scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val)
1684 Set the element at index @var{idx} of the generalized vector @var{v}
1685 to @var{val}.
1686 @end deftypefn
1687
1688 @deftypefn {C Function} void scm_generalized_vector_get_handle (SCM v, scm_t_array_handle *handle)
1689 Like @code{scm_array_get_handle} but an error is signalled when @var{v}
1690 is not of rank one. You can use @code{scm_array_handle_ref} and
1691 @code{scm_array_handle_set} to read and write the elements of @var{v},
1692 or you can use functions like @code{scm_array_handle_<foo>_elements} to
1693 deal with specific types of vectors.
1694 @end deftypefn
1695
1696 @node Arrays
1697 @subsection Arrays
1698 @tpindex Arrays
1699
1700 @dfn{Arrays} are a collection of cells organized into an arbitrary
1701 number of dimensions. Each cell can be accessed in constant time by
1702 supplying an index for each dimension.
1703
1704 In the current implementation, an array uses a generalized vector for
1705 the actual storage of its elements. Any kind of generalized vector
1706 will do, so you can have arrays of uniform numeric values, arrays of
1707 characters, arrays of bits, and of course, arrays of arbitrary Scheme
1708 values. For example, arrays with an underlying @code{c64vector} might
1709 be nice for digital signal processing, while arrays made from a
1710 @code{u8vector} might be used to hold gray-scale images.
1711
1712 The number of dimensions of an array is called its @dfn{rank}. Thus,
1713 a matrix is an array of rank 2, while a vector has rank 1. When
1714 accessing an array element, you have to specify one exact integer for
1715 each dimension. These integers are called the @dfn{indices} of the
1716 element. An array specifies the allowed range of indices for each
1717 dimension via an inclusive lower and upper bound. These bounds can
1718 well be negative, but the upper bound must be greater than or equal to
1719 the lower bound minus one. When all lower bounds of an array are
1720 zero, it is called a @dfn{zero-origin} array.
1721
1722 Arrays can be of rank 0, which could be interpreted as a scalar.
1723 Thus, a zero-rank array can store exactly one object and the list of
1724 indices of this element is the empty list.
1725
1726 Arrays contain zero elements when one of their dimensions has a zero
1727 length. These empty arrays maintain information about their shape: a
1728 matrix with zero columns and 3 rows is different from a matrix with 3
1729 columns and zero rows, which again is different from a vector of
1730 length zero.
1731
1732 Generalized vectors, such as strings, uniform numeric vectors, bit
1733 vectors and ordinary vectors, are the special case of one dimensional
1734 arrays.
1735
1736 @menu
1737 * Array Syntax::
1738 * Array Procedures::
1739 * Shared Arrays::
1740 * Accessing Arrays from C::
1741 @end menu
1742
1743 @node Array Syntax
1744 @subsubsection Array Syntax
1745
1746 An array is displayed as @code{#} followed by its rank, followed by a
1747 tag that describes the underlying vector, optionally followed by
1748 information about its shape, and finally followed by the cells,
1749 organized into dimensions using parentheses.
1750
1751 In more words, the array tag is of the form
1752
1753 @example
1754 #<rank><vectag><@@lower><:len><@@lower><:len>...
1755 @end example
1756
1757 where @code{<rank>} is a positive integer in decimal giving the rank of
1758 the array. It is omitted when the rank is 1 and the array is non-shared
1759 and has zero-origin (see below). For shared arrays and for a non-zero
1760 origin, the rank is always printed even when it is 1 to dinstinguish
1761 them from ordinary vectors.
1762
1763 The @code{<vectag>} part is the tag for a uniform numeric vector, like
1764 @code{u8}, @code{s16}, etc, @code{b} for bitvectors, or @code{a} for
1765 strings. It is empty for ordinary vectors.
1766
1767 The @code{<@@lower>} part is a @samp{@@} character followed by a signed
1768 integer in decimal giving the lower bound of a dimension. There is one
1769 @code{<@@lower>} for each dimension. When all lower bounds are zero,
1770 all @code{<@@lower>} parts are omitted.
1771
1772 The @code{<:len>} part is a @samp{:} character followed by an unsigned
1773 integer in decimal giving the length of a dimension. Like for the lower
1774 bounds, there is one @code{<:len>} for each dimension, and the
1775 @code{<:len>} part always follows the @code{<@@lower>} part for a
1776 dimension. Lengths are only then printed when they can't be deduced
1777 from the nested lists of elements of the array literal, which can happen
1778 when at least one length is zero.
1779
1780 As a special case, an array of rank 0 is printed as
1781 @code{#0<vectag>(<scalar>)}, where @code{<scalar>} is the result of
1782 printing the single element of the array.
1783
1784 Thus,
1785
1786 @table @code
1787 @item #(1 2 3)
1788 is an ordinary array of rank 1 with lower bound 0 in dimension 0.
1789 (I.e., a regular vector.)
1790
1791 @item #@@2(1 2 3)
1792 is an ordinary array of rank 1 with lower bound 2 in dimension 0.
1793
1794 @item #2((1 2 3) (4 5 6))
1795 is a non-uniform array of rank 2; a 3@cross{}3 matrix with index ranges 0..2
1796 and 0..2.
1797
1798 @item #u32(0 1 2)
1799 is a uniform u8 array of rank 1.
1800
1801 @item #2u32@@2@@3((1 2) (2 3))
1802 is a uniform u8 array of rank 2 with index ranges 2..3 and 3..4.
1803
1804 @item #2()
1805 is a two-dimensional array with index ranges 0..-1 and 0..-1, i.e. both
1806 dimensions have length zero.
1807
1808 @item #2:0:2()
1809 is a two-dimensional array with index ranges 0..-1 and 0..1, i.e. the
1810 first dimension has length zero, but the second has length 2.
1811
1812 @item #0(12)
1813 is a rank-zero array with contents 12.
1814
1815 @end table
1816
1817 @node Array Procedures
1818 @subsubsection Array Procedures
1819
1820 When an array is created, the range of each dimension must be
1821 specified, e.g., to create a 2@cross{}3 array with a zero-based index:
1822
1823 @example
1824 (make-array 'ho 2 3) @result{} #2((ho ho ho) (ho ho ho))
1825 @end example
1826
1827 The range of each dimension can also be given explicitly, e.g., another
1828 way to create the same array:
1829
1830 @example
1831 (make-array 'ho '(0 1) '(0 2)) @result{} #2((ho ho ho) (ho ho ho))
1832 @end example
1833
1834 The following procedures can be used with arrays (or vectors). An
1835 argument shown as @var{idx}@dots{} means one parameter for each
1836 dimension in the array. A @var{idxlist} argument means a list of such
1837 values, one for each dimension.
1838
1839
1840 @deffn {Scheme Procedure} array? obj
1841 @deffnx {C Function} scm_array_p (obj, unused)
1842 Return @code{#t} if the @var{obj} is an array, and @code{#f} if
1843 not.
1844
1845 The second argument to scm_array_p is there for historical reasons,
1846 but it is not used. You should always pass @code{SCM_UNDEFINED} as
1847 its value.
1848 @end deffn
1849
1850 @deffn {Scheme Procedure} typed-array? obj type
1851 @deffnx {C Function} scm_typed_array_p (obj, type)
1852 Return @code{#t} if the @var{obj} is an array of type @var{type}, and
1853 @code{#f} if not.
1854 @end deffn
1855
1856 @deftypefn {C Function} int scm_is_array (SCM obj)
1857 Return @code{1} if the @var{obj} is an array and @code{0} if not.
1858 @end deftypefn
1859
1860 @deftypefn {C Function} int scm_is_typed_array (SCM obj, SCM type)
1861 Return @code{0} if the @var{obj} is an array of type @var{type}, and
1862 @code{1} if not.
1863 @end deftypefn
1864
1865 @deffn {Scheme Procedure} make-array fill bound @dots{}
1866 @deffnx {C Function} scm_make_array (fill, bounds)
1867 Equivalent to @code{(make-typed-array #t @var{fill} @var{bound} ...)}.
1868 @end deffn
1869
1870 @deffn {Scheme Procedure} make-typed-array type fill bound @dots{}
1871 @deffnx {C Function} scm_make_typed_array (type, fill, bounds)
1872 Create and return an array that has as many dimensions as there are
1873 @var{bound}s and (maybe) fill it with @var{fill}.
1874
1875 The underlaying storage vector is created according to @var{type},
1876 which must be a symbol whose name is the `vectag' of the array as
1877 explained above, or @code{#t} for ordinary, non-specialized arrays.
1878
1879 For example, using the symbol @code{f64} for @var{type} will create an
1880 array that uses a @code{f64vector} for storing its elements, and
1881 @code{a} will use a string.
1882
1883 When @var{fill} is not the special @emph{unspecified} value, the new
1884 array is filled with @var{fill}. Otherwise, the initial contents of
1885 the array is unspecified. The special @emph{unspecified} value is
1886 stored in the variable @code{*unspecified*} so that for example
1887 @code{(make-typed-array 'u32 *unspecified* 4)} creates a uninitialized
1888 @code{u32} vector of length 4.
1889
1890 Each @var{bound} may be a positive non-zero integer @var{N}, in which
1891 case the index for that dimension can range from 0 through @var{N-1}; or
1892 an explicit index range specifier in the form @code{(LOWER UPPER)},
1893 where both @var{lower} and @var{upper} are integers, possibly less than
1894 zero, and possibly the same number (however, @var{lower} cannot be
1895 greater than @var{upper}).
1896 @end deffn
1897
1898 @deffn {Scheme Procedure} list->array dimspec list
1899 Equivalent to @code{(list->typed-array #t @var{dimspec}
1900 @var{list})}.
1901 @end deffn
1902
1903 @deffn {Scheme Procedure} list->typed-array type dimspec list
1904 @deffnx {C Function} scm_list_to_typed_array (type, dimspec, list)
1905 Return an array of the type indicated by @var{type} with elements the
1906 same as those of @var{list}.
1907
1908 The argument @var{dimspec} determines the number of dimensions of the
1909 array and their lower bounds. When @var{dimspec} is an exact integer,
1910 it gives the number of dimensions directly and all lower bounds are
1911 zero. When it is a list of exact integers, then each element is the
1912 lower index bound of a dimension, and there will be as many dimensions
1913 as elements in the list.
1914 @end deffn
1915
1916 @deffn {Scheme Procedure} array-type array
1917 Return the type of @var{array}. This is the `vectag' used for
1918 printing @var{array} (or @code{#t} for ordinary arrays) and can be
1919 used with @code{make-typed-array} to create an array of the same kind
1920 as @var{array}.
1921 @end deffn
1922
1923 @deffn {Scheme Procedure} array-ref array idx @dots{}
1924 Return the element at @code{(idx @dots{})} in @var{array}.
1925
1926 @example
1927 (define a (make-array 999 '(1 2) '(3 4)))
1928 (array-ref a 2 4) @result{} 999
1929 @end example
1930 @end deffn
1931
1932 @deffn {Scheme Procedure} array-in-bounds? array idx @dots{}
1933 @deffnx {C Function} scm_array_in_bounds_p (array, idxlist)
1934 Return @code{#t} if the given index would be acceptable to
1935 @code{array-ref}.
1936
1937 @example
1938 (define a (make-array #f '(1 2) '(3 4)))
1939 (array-in-bounds? a 2 3) @result{} #t
1940 (array-in-bounds? a 0 0) @result{} #f
1941 @end example
1942 @end deffn
1943
1944 @deffn {Scheme Procedure} array-set! array obj idx @dots{}
1945 @deffnx {C Function} scm_array_set_x (array, obj, idxlist)
1946 Set the element at @code{(idx @dots{})} in @var{array} to @var{obj}.
1947 The return value is unspecified.
1948
1949 @example
1950 (define a (make-array #f '(0 1) '(0 1)))
1951 (array-set! a #t 1 1)
1952 a @result{} #2((#f #f) (#f #t))
1953 @end example
1954 @end deffn
1955
1956 @deffn {Scheme Procedure} enclose-array array dim1 @dots{}
1957 @deffnx {C Function} scm_enclose_array (array, dimlist)
1958 @var{dim1}, @var{dim2} @dots{} should be nonnegative integers less than
1959 the rank of @var{array}. @code{enclose-array} returns an array
1960 resembling an array of shared arrays. The dimensions of each shared
1961 array are the same as the @var{dim}th dimensions of the original array,
1962 the dimensions of the outer array are the same as those of the original
1963 array that did not match a @var{dim}.
1964
1965 An enclosed array is not a general Scheme array. Its elements may not
1966 be set using @code{array-set!}. Two references to the same element of
1967 an enclosed array will be @code{equal?} but will not in general be
1968 @code{eq?}. The value returned by @code{array-prototype} when given an
1969 enclosed array is unspecified.
1970
1971 For example,
1972
1973 @lisp
1974 (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1)
1975 @result{}
1976 #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>
1977
1978 (enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0)
1979 @result{}
1980 #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>
1981 @end lisp
1982 @end deffn
1983
1984 @deffn {Scheme Procedure} array-shape array
1985 @deffnx {Scheme Procedure} array-dimensions array
1986 @deffnx {C Function} scm_array_dimensions (array)
1987 Return a list of the bounds for each dimenson of @var{array}.
1988
1989 @code{array-shape} gives @code{(@var{lower} @var{upper})} for each
1990 dimension. @code{array-dimensions} instead returns just
1991 @math{@var{upper}+1} for dimensions with a 0 lower bound. Both are
1992 suitable as input to @code{make-array}.
1993
1994 For example,
1995
1996 @example
1997 (define a (make-array 'foo '(-1 3) 5))
1998 (array-shape a) @result{} ((-1 3) (0 4))
1999 (array-dimensions a) @result{} ((-1 3) 5)
2000 @end example
2001 @end deffn
2002
2003 @deffn {Scheme Procedure} array-rank obj
2004 @deffnx {C Function} scm_array_rank (obj)
2005 Return the rank of @var{array}.
2006 @end deffn
2007
2008 @deftypefn {C Function} size_t scm_c_array_rank (SCM array)
2009 Return the rank of @var{array} as a @code{size_t}.
2010 @end deftypefn
2011
2012 @deffn {Scheme Procedure} array->list array
2013 @deffnx {C Function} scm_array_to_list (array)
2014 Return a list consisting of all the elements, in order, of
2015 @var{array}.
2016 @end deffn
2017
2018 @c FIXME: Describe how the order affects the copying (it matters for
2019 @c shared arrays with the same underlying root vector, presumably).
2020 @c
2021 @deffn {Scheme Procedure} array-copy! src dst
2022 @deffnx {Scheme Procedure} array-copy-in-order! src dst
2023 @deffnx {C Function} scm_array_copy_x (src, dst)
2024 Copy every element from vector or array @var{src} to the corresponding
2025 element of @var{dst}. @var{dst} must have the same rank as @var{src},
2026 and be at least as large in each dimension. The return value is
2027 unspecified.
2028 @end deffn
2029
2030 @deffn {Scheme Procedure} array-fill! array fill
2031 @deffnx {C Function} scm_array_fill_x (array, fill)
2032 Store @var{fill} in every element of @var{array}. The value returned
2033 is unspecified.
2034 @end deffn
2035
2036 @c begin (texi-doc-string "guile" "array-equal?")
2037 @deffn {Scheme Procedure} array-equal? array1 array2 @dots{}
2038 Return @code{#t} if all arguments are arrays with the same shape, the
2039 same type, and have corresponding elements which are either
2040 @code{equal?} or @code{array-equal?}. This function differs from
2041 @code{equal?} (@pxref{Equality}) in that a one dimensional shared
2042 array may be @code{array-equal?} but not @code{equal?} to a vector or
2043 uniform vector.
2044 @end deffn
2045
2046 @c FIXME: array-map! accepts no source arrays at all, and in that
2047 @c case makes calls "(proc)". Is that meant to be a documented
2048 @c feature?
2049 @c
2050 @c FIXME: array-for-each doesn't say what happens if the sources have
2051 @c different index ranges. The code currently iterates over the
2052 @c indices of the first and expects the others to cover those. That
2053 @c at least vaguely matches array-map!, but is is meant to be a
2054 @c documented feature?
2055
2056 @deffn {Scheme Procedure} array-map! dst proc src1 @dots{} srcN
2057 @deffnx {Scheme Procedure} array-map-in-order! dst proc src1 @dots{} srcN
2058 @deffnx {C Function} scm_array_map_x (dst, proc, srclist)
2059 Set each element of the @var{dst} array to values obtained from calls
2060 to @var{proc}. The value returned is unspecified.
2061
2062 Each call is @code{(@var{proc} @var{elem1} @dots{} @var{elemN})},
2063 where each @var{elem} is from the corresponding @var{src} array, at
2064 the @var{dst} index. @code{array-map-in-order!} makes the calls in
2065 row-major order, @code{array-map!} makes them in an unspecified order.
2066
2067 The @var{src} arrays must have the same number of dimensions as
2068 @var{dst}, and must have a range for each dimension which covers the
2069 range in @var{dst}. This ensures all @var{dst} indices are valid in
2070 each @var{src}.
2071 @end deffn
2072
2073 @deffn {Scheme Procedure} array-for-each proc src1 @dots{} srcN
2074 @deffnx {C Function} scm_array_for_each (proc, src1, srclist)
2075 Apply @var{proc} to each tuple of elements of @var{src1} @dots{}
2076 @var{srcN}, in row-major order. The value returned is unspecified.
2077 @end deffn
2078
2079 @deffn {Scheme Procedure} array-index-map! dst proc
2080 @deffnx {C Function} scm_array_index_map_x (dst, proc)
2081 Set each element of the @var{dst} array to values returned by calls to
2082 @var{proc}. The value returned is unspecified.
2083
2084 Each call is @code{(@var{proc} @var{i1} @dots{} @var{iN})}, where
2085 @var{i1}@dots{}@var{iN} is the destination index, one parameter for
2086 each dimension. The order in which the calls are made is unspecified.
2087
2088 For example, to create a @m{4\times4, 4x4} matrix representing a
2089 cyclic group,
2090
2091 @tex
2092 \advance\leftskip by 2\lispnarrowing {
2093 $\left(\matrix{%
2094 0 & 1 & 2 & 3 \cr
2095 1 & 2 & 3 & 0 \cr
2096 2 & 3 & 0 & 1 \cr
2097 3 & 0 & 1 & 2 \cr
2098 }\right)$} \par
2099 @end tex
2100 @ifnottex
2101 @example
2102 / 0 1 2 3 \
2103 | 1 2 3 0 |
2104 | 2 3 0 1 |
2105 \ 3 0 1 2 /
2106 @end example
2107 @end ifnottex
2108
2109 @example
2110 (define a (make-array #f 4 4))
2111 (array-index-map! a (lambda (i j)
2112 (modulo (+ i j) 4)))
2113 @end example
2114 @end deffn
2115
2116 @deffn {Scheme Procedure} uniform-array-read! ra [port_or_fd [start [end]]]
2117 @deffnx {C Function} scm_uniform_array_read_x (ra, port_or_fd, start, end)
2118 Attempt to read all elements of @var{ura}, in lexicographic order, as
2119 binary objects from @var{port-or-fdes}.
2120 If an end of file is encountered,
2121 the objects up to that point are put into @var{ura}
2122 (starting at the beginning) and the remainder of the array is
2123 unchanged.
2124
2125 The optional arguments @var{start} and @var{end} allow
2126 a specified region of a vector (or linearized array) to be read,
2127 leaving the remainder of the vector unchanged.
2128
2129 @code{uniform-array-read!} returns the number of objects read.
2130 @var{port-or-fdes} may be omitted, in which case it defaults to the value
2131 returned by @code{(current-input-port)}.
2132 @end deffn
2133
2134 @deffn {Scheme Procedure} uniform-array-write v [port_or_fd [start [end]]]
2135 @deffnx {C Function} scm_uniform_array_write (v, port_or_fd, start, end)
2136 Writes all elements of @var{ura} as binary objects to
2137 @var{port-or-fdes}.
2138
2139 The optional arguments @var{start}
2140 and @var{end} allow
2141 a specified region of a vector (or linearized array) to be written.
2142
2143 The number of objects actually written is returned.
2144 @var{port-or-fdes} may be
2145 omitted, in which case it defaults to the value returned by
2146 @code{(current-output-port)}.
2147 @end deffn
2148
2149 @node Shared Arrays
2150 @subsubsection Shared Arrays
2151
2152 @deffn {Scheme Procedure} make-shared-array oldarray mapfunc bound @dots{}
2153 @deffnx {C Function} scm_make_shared_array (oldarray, mapfunc, boundlist)
2154 Return a new array which shares the storage of @var{oldarray}.
2155 Changes made through either affect the same underlying storage. The
2156 @var{bound@dots{}} arguments are the shape of the new array, the same
2157 as @code{make-array} (@pxref{Array Procedures}).
2158
2159 @var{mapfunc} translates coordinates from the new array to the
2160 @var{oldarray}. It's called as @code{(@var{mapfunc} newidx1 @dots{})}
2161 with one parameter for each dimension of the new array, and should
2162 return a list of indices for @var{oldarray}, one for each dimension of
2163 @var{oldarray}.
2164
2165 @var{mapfunc} must be affine linear, meaning that each @var{oldarray}
2166 index must be formed by adding integer multiples (possibly negative)
2167 of some or all of @var{newidx1} etc, plus a possible integer offset.
2168 The multiples and offset must be the same in each call.
2169
2170 @sp 1
2171 One good use for a shared array is to restrict the range of some
2172 dimensions, so as to apply say @code{array-for-each} or
2173 @code{array-fill!} to only part of an array. The plain @code{list}
2174 function can be used for @var{mapfunc} in this case, making no changes
2175 to the index values. For example,
2176
2177 @example
2178 (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
2179 @result{} #2((a b) (d e) (g h))
2180 @end example
2181
2182 The new array can have fewer dimensions than @var{oldarray}, for
2183 example to take a column from an array.
2184
2185 @example
2186 (make-shared-array #2((a b c) (d e f) (g h i))
2187 (lambda (i) (list i 2))
2188 '(0 2))
2189 @result{} #1(c f i)
2190 @end example
2191
2192 A diagonal can be taken by using the single new array index for both
2193 row and column in the old array. For example,
2194
2195 @example
2196 (make-shared-array #2((a b c) (d e f) (g h i))
2197 (lambda (i) (list i i))
2198 '(0 2))
2199 @result{} #1(a e i)
2200 @end example
2201
2202 Dimensions can be increased by for instance considering portions of a
2203 one dimensional array as rows in a two dimensional array.
2204 (@code{array-contents} below can do the opposite, flattening an
2205 array.)
2206
2207 @example
2208 (make-shared-array #1(a b c d e f g h i j k l)
2209 (lambda (i j) (list (+ (* i 3) j)))
2210 4 3)
2211 @result{} #2((a b c) (d e f) (g h i) (j k l))
2212 @end example
2213
2214 By negating an index the order that elements appear can be reversed.
2215 The following just reverses the column order,
2216
2217 @example
2218 (make-shared-array #2((a b c) (d e f) (g h i))
2219 (lambda (i j) (list i (- 2 j)))
2220 3 3)
2221 @result{} #2((c b a) (f e d) (i h g))
2222 @end example
2223
2224 A fixed offset on indexes allows for instance a change from a 0 based
2225 to a 1 based array,
2226
2227 @example
2228 (define x #2((a b c) (d e f) (g h i)))
2229 (define y (make-shared-array x
2230 (lambda (i j) (list (1- i) (1- j)))
2231 '(1 3) '(1 3)))
2232 (array-ref x 0 0) @result{} a
2233 (array-ref y 1 1) @result{} a
2234 @end example
2235
2236 A multiple on an index allows every Nth element of an array to be
2237 taken. The following is every third element,
2238
2239 @example
2240 (make-shared-array #1(a b c d e f g h i j k l)
2241 (lambda (i) (list (* i 3)))
2242 4)
2243 @result{} #1(a d g j)
2244 @end example
2245
2246 The above examples can be combined to make weird and wonderful
2247 selections from an array, but it's important to note that because
2248 @var{mapfunc} must be affine linear, arbitrary permutations are not
2249 possible.
2250
2251 In the current implementation, @var{mapfunc} is not called for every
2252 access to the new array but only on some sample points to establish a
2253 base and stride for new array indices in @var{oldarray} data. A few
2254 sample points are enough because @var{mapfunc} is linear.
2255 @end deffn
2256
2257 @deffn {Scheme Procedure} shared-array-increments array
2258 @deffnx {C Function} scm_shared_array_increments (array)
2259 For each dimension, return the distance between elements in the root vector.
2260 @end deffn
2261
2262 @deffn {Scheme Procedure} shared-array-offset array
2263 @deffnx {C Function} scm_shared_array_offset (array)
2264 Return the root vector index of the first element in the array.
2265 @end deffn
2266
2267 @deffn {Scheme Procedure} shared-array-root array
2268 @deffnx {C Function} scm_shared_array_root (array)
2269 Return the root vector of a shared array.
2270 @end deffn
2271
2272 @deffn {Scheme Procedure} array-contents array [strict]
2273 @deffnx {C Function} scm_array_contents (array, strict)
2274 If @var{array} may be @dfn{unrolled} into a one dimensional shared array
2275 without changing their order (last subscript changing fastest), then
2276 @code{array-contents} returns that shared array, otherwise it returns
2277 @code{#f}. All arrays made by @code{make-array} and
2278 @code{make-typed-array} may be unrolled, some arrays made by
2279 @code{make-shared-array} may not be.
2280
2281 If the optional argument @var{strict} is provided, a shared array will
2282 be returned only if its elements are stored internally contiguous in
2283 memory.
2284 @end deffn
2285
2286 @deffn {Scheme Procedure} transpose-array array dim1 @dots{}
2287 @deffnx {C Function} scm_transpose_array (array, dimlist)
2288 Return an array sharing contents with @var{array}, but with
2289 dimensions arranged in a different order. There must be one
2290 @var{dim} argument for each dimension of @var{array}.
2291 @var{dim1}, @var{dim2}, @dots{} should be integers between 0
2292 and the rank of the array to be returned. Each integer in that
2293 range must appear at least once in the argument list.
2294
2295 The values of @var{dim1}, @var{dim2}, @dots{} correspond to
2296 dimensions in the array to be returned, and their positions in the
2297 argument list to dimensions of @var{array}. Several @var{dim}s
2298 may have the same value, in which case the returned array will
2299 have smaller rank than @var{array}.
2300
2301 @lisp
2302 (transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))
2303 (transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)
2304 (transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}
2305 #2((a 4) (b 5) (c 6))
2306 @end lisp
2307 @end deffn
2308
2309 @node Accessing Arrays from C
2310 @subsubsection Accessing Arrays from C
2311
2312 Arrays, especially uniform numeric arrays, are useful to efficiently
2313 represent large amounts of rectangularily organized information, such as
2314 matrices, images, or generally blobs of binary data. It is desirable to
2315 access these blobs in a C like manner so that they can be handed to
2316 external C code such as linear algebra libraries or image processing
2317 routines.
2318
2319 While pointers to the elements of an array are in use, the array itself
2320 must be protected so that the pointer remains valid. Such a protected
2321 array is said to be @dfn{reserved}. A reserved array can be read but
2322 modifications to it that would cause the pointer to its elements to
2323 become invalid are prevented. When you attempt such a modification, an
2324 error is signalled.
2325
2326 (This is similar to locking the array while it is in use, but without
2327 the danger of a deadlock. In a multi-threaded program, you will need
2328 additional synchronization to avoid modifying reserved arrays.)
2329
2330 You must take care to always unreserve an array after reserving it,
2331 also in the presence of non-local exits. To simplify this, reserving
2332 and unreserving work like a dynwind context (@pxref{Dynamic Wind}): a
2333 call to @code{scm_array_get_handle} can be thought of as beginning a
2334 dynwind context and @code{scm_array_handle_release} as ending it.
2335 When a non-local exit happens between these two calls, the array is
2336 implicitely unreserved.
2337
2338 That is, you need to properly pair reserving and unreserving in your
2339 code, but you don't need to worry about non-local exits.
2340
2341 These calls and other pairs of calls that establish dynwind contexts
2342 need to be properly nested. If you begin a context prior to reserving
2343 an array, you need to unreserve the array before ending the context.
2344 Likewise, when reserving two or more arrays in a certain order, you
2345 need to unreserve them in the opposite order.
2346
2347 Once you have reserved an array and have retrieved the pointer to its
2348 elements, you must figure out the layout of the elements in memory.
2349 Guile allows slices to be taken out of arrays without actually making a
2350 copy, such as making an alias for the diagonal of a matrix that can be
2351 treated as a vector. Arrays that result from such an operation are not
2352 stored contiguously in memory and when working with their elements
2353 directly, you need to take this into account.
2354
2355 The layout of array elements in memory can be defined via a
2356 @emph{mapping function} that computes a scalar position from a vector of
2357 indices. The scalar position then is the offset of the element with the
2358 given indices from the start of the storage block of the array.
2359
2360 In Guile, this mapping function is restricted to be @dfn{affine}: all
2361 mapping functions of Guile arrays can be written as @code{p = b +
2362 c[0]*i[0] + c[1]*i[1] + ... + c[n-1]*i[n-1]} where @code{i[k]} is the
2363 @nicode{k}th index and @code{n} is the rank of the array. For
2364 example, a matrix of size 3x3 would have @code{b == 0}, @code{c[0] ==
2365 3} and @code{c[1] == 1}. When you transpose this matrix (with
2366 @code{transpose-array}, say), you will get an array whose mapping
2367 function has @code{b == 0}, @code{c[0] == 1} and @code{c[1] == 3}.
2368
2369 The function @code{scm_array_handle_dims} gives you (indirect) access to
2370 the coefficients @code{c[k]}.
2371
2372 @c XXX
2373 Note that there are no functions for accessing the elements of a
2374 character array yet. Once the string implementation of Guile has been
2375 changed to use Unicode, we will provide them.
2376
2377 @deftp {C Type} scm_t_array_handle
2378 This is a structure type that holds all information necessary to manage
2379 the reservation of arrays as explained above. Structures of this type
2380 must be allocated on the stack and must only be accessed by the
2381 functions listed below.
2382 @end deftp
2383
2384 @deftypefn {C Function} void scm_array_get_handle (SCM array, scm_t_array_handle *handle)
2385 Reserve @var{array}, which must be an array, and prepare @var{handle} to
2386 be used with the functions below. You must eventually call
2387 @code{scm_array_handle_release} on @var{handle}, and do this in a
2388 properly nested fashion, as explained above. The structure pointed to
2389 by @var{handle} does not need to be initialized before calling this
2390 function.
2391 @end deftypefn
2392
2393 @deftypefn {C Function} void scm_array_handle_release (scm_t_array_handle *handle)
2394 End the array reservation represented by @var{handle}. After a call to
2395 this function, @var{handle} might be used for another reservation.
2396 @end deftypefn
2397
2398 @deftypefn {C Function} size_t scm_array_handle_rank (scm_t_array_handle *handle)
2399 Return the rank of the array represented by @var{handle}.
2400 @end deftypefn
2401
2402 @deftp {C Type} scm_t_array_dim
2403 This structure type holds information about the layout of one dimension
2404 of an array. It includes the following fields:
2405
2406 @table @code
2407 @item ssize_t lbnd
2408 @itemx ssize_t ubnd
2409 The lower and upper bounds (both inclusive) of the permissible index
2410 range for the given dimension. Both values can be negative, but
2411 @var{lbnd} is always less than or equal to @var{ubnd}.
2412
2413 @item ssize_t inc
2414 The distance from one element of this dimension to the next. Note, too,
2415 that this can be negative.
2416 @end table
2417 @end deftp
2418
2419 @deftypefn {C Function} {const scm_t_array_dim *} scm_array_handle_dims (scm_t_array_handle *handle)
2420 Return a pointer to a C vector of information about the dimensions of
2421 the array represented by @var{handle}. This pointer is valid as long as
2422 the array remains reserved. As explained above, the
2423 @code{scm_t_array_dim} structures returned by this function can be used
2424 calculate the position of an element in the storage block of the array
2425 from its indices.
2426
2427 This position can then be used as an index into the C array pointer
2428 returned by the various @code{scm_array_handle_<foo>_elements}
2429 functions, or with @code{scm_array_handle_ref} and
2430 @code{scm_array_handle_set}.
2431
2432 Here is how one can compute the position @var{pos} of an element given
2433 its indices in the vector @var{indices}:
2434
2435 @example
2436 ssize_t indices[RANK];
2437 scm_t_array_dim *dims;
2438 ssize_t pos;
2439 size_t i;
2440
2441 pos = 0;
2442 for (i = 0; i < RANK; i++)
2443 @{
2444 if (indices[i] < dims[i].lbnd || indices[i] > dims[i].ubnd)
2445 out_of_range ();
2446 pos += (indices[i] - dims[i].lbnd) * dims[i].inc;
2447 @}
2448 @end example
2449 @end deftypefn
2450
2451 @deftypefn {C Function} ssize_t scm_array_handle_pos (scm_t_array_handle *handle, SCM indices)
2452 Compute the position corresponding to @var{indices}, a list of
2453 indices. The position is computed as described above for
2454 @code{scm_array_handle_dims}. The number of the indices and their
2455 range is checked and an approrpiate error is signalled for invalid
2456 indices.
2457 @end deftypefn
2458
2459 @deftypefn {C Function} SCM scm_array_handle_ref (scm_t_array_handle *handle, ssize_t pos)
2460 Return the element at position @var{pos} in the storage block of the
2461 array represented by @var{handle}. Any kind of array is acceptable. No
2462 range checking is done on @var{pos}.
2463 @end deftypefn
2464
2465 @deftypefn {C Function} void scm_array_handle_set (scm_t_array_handle *handle, ssize_t pos, SCM val)
2466 Set the element at position @var{pos} in the storage block of the array
2467 represented by @var{handle} to @var{val}. Any kind of array is
2468 acceptable. No range checking is done on @var{pos}. An error is
2469 signalled when the array can not store @var{val}.
2470 @end deftypefn
2471
2472 @deftypefn {C Function} {const SCM *} scm_array_handle_elements (scm_t_array_handle *handle)
2473 Return a pointer to the elements of a ordinary array of general Scheme
2474 values (i.e., a non-uniform array) for reading. This pointer is valid
2475 as long as the array remains reserved.
2476 @end deftypefn
2477
2478 @deftypefn {C Function} {SCM *} scm_array_handle_writable_elements (scm_t_array_handle *handle)
2479 Like @code{scm_array_handle_elements}, but the pointer is good for
2480 reading and writing.
2481 @end deftypefn
2482
2483 @deftypefn {C Function} {const void *} scm_array_handle_uniform_elements (scm_t_array_handle *handle)
2484 Return a pointer to the elements of a uniform numeric array for reading.
2485 This pointer is valid as long as the array remains reserved. The size
2486 of each element is given by @code{scm_array_handle_uniform_element_size}.
2487 @end deftypefn
2488
2489 @deftypefn {C Function} {void *} scm_array_handle_uniform_writable_elements (scm_t_array_handle *handle)
2490 Like @code{scm_array_handle_uniform_elements}, but the pointer is good
2491 reading and writing.
2492 @end deftypefn
2493
2494 @deftypefn {C Function} size_t scm_array_handle_uniform_element_size (scm_t_array_handle *handle)
2495 Return the size of one element of the uniform numeric array represented
2496 by @var{handle}.
2497 @end deftypefn
2498
2499 @deftypefn {C Function} {const scm_t_uint8 *} scm_array_handle_u8_elements (scm_t_array_handle *handle)
2500 @deftypefnx {C Function} {const scm_t_int8 *} scm_array_handle_s8_elements (scm_t_array_handle *handle)
2501 @deftypefnx {C Function} {const scm_t_uint16 *} scm_array_handle_u16_elements (scm_t_array_handle *handle)
2502 @deftypefnx {C Function} {const scm_t_int16 *} scm_array_handle_s16_elements (scm_t_array_handle *handle)
2503 @deftypefnx {C Function} {const scm_t_uint32 *} scm_array_handle_u32_elements (scm_t_array_handle *handle)
2504 @deftypefnx {C Function} {const scm_t_int32 *} scm_array_handle_s32_elements (scm_t_array_handle *handle)
2505 @deftypefnx {C Function} {const scm_t_uint64 *} scm_array_handle_u64_elements (scm_t_array_handle *handle)
2506 @deftypefnx {C Function} {const scm_t_int64 *} scm_array_handle_s64_elements (scm_t_array_handle *handle)
2507 @deftypefnx {C Function} {const float *} scm_array_handle_f32_elements (scm_t_array_handle *handle)
2508 @deftypefnx {C Function} {const double *} scm_array_handle_f64_elements (scm_t_array_handle *handle)
2509 @deftypefnx {C Function} {const float *} scm_array_handle_c32_elements (scm_t_array_handle *handle)
2510 @deftypefnx {C Function} {const double *} scm_array_handle_c64_elements (scm_t_array_handle *handle)
2511 Return a pointer to the elements of a uniform numeric array of the
2512 indicated kind for reading. This pointer is valid as long as the array
2513 remains reserved.
2514
2515 The pointers for @code{c32} and @code{c64} uniform numeric arrays point
2516 to pairs of floating point numbers. The even index holds the real part,
2517 the odd index the imaginary part of the complex number.
2518 @end deftypefn
2519
2520 @deftypefn {C Function} {scm_t_uint8 *} scm_array_handle_u8_writable_elements (scm_t_array_handle *handle)
2521 @deftypefnx {C Function} {scm_t_int8 *} scm_array_handle_s8_writable_elements (scm_t_array_handle *handle)
2522 @deftypefnx {C Function} {scm_t_uint16 *} scm_array_handle_u16_writable_elements (scm_t_array_handle *handle)
2523 @deftypefnx {C Function} {scm_t_int16 *} scm_array_handle_s16_writable_elements (scm_t_array_handle *handle)
2524 @deftypefnx {C Function} {scm_t_uint32 *} scm_array_handle_u32_writable_elements (scm_t_array_handle *handle)
2525 @deftypefnx {C Function} {scm_t_int32 *} scm_array_handle_s32_writable_elements (scm_t_array_handle *handle)
2526 @deftypefnx {C Function} {scm_t_uint64 *} scm_array_handle_u64_writable_elements (scm_t_array_handle *handle)
2527 @deftypefnx {C Function} {scm_t_int64 *} scm_array_handle_s64_writable_elements (scm_t_array_handle *handle)
2528 @deftypefnx {C Function} {float *} scm_array_handle_f32_writable_elements (scm_t_array_handle *handle)
2529 @deftypefnx {C Function} {double *} scm_array_handle_f64_writable_elements (scm_t_array_handle *handle)
2530 @deftypefnx {C Function} {float *} scm_array_handle_c32_writable_elements (scm_t_array_handle *handle)
2531 @deftypefnx {C Function} {double *} scm_array_handle_c64_writable_elements (scm_t_array_handle *handle)
2532 Like @code{scm_array_handle_<kind>_elements}, but the pointer is good
2533 for reading and writing.
2534 @end deftypefn
2535
2536 @deftypefn {C Function} {const scm_t_uint32 *} scm_array_handle_bit_elements (scm_t_array_handle *handle)
2537 Return a pointer to the words that store the bits of the represented
2538 array, which must be a bit array.
2539
2540 Unlike other arrays, bit arrays have an additional offset that must be
2541 figured into index calculations. That offset is returned by
2542 @code{scm_array_handle_bit_elements_offset}.
2543
2544 To find a certain bit you first need to calculate its position as
2545 explained above for @code{scm_array_handle_dims} and then add the
2546 offset. This gives the absolute position of the bit, which is always a
2547 non-negative integer.
2548
2549 Each word of the bit array storage block contains exactly 32 bits, with
2550 the least significant bit in that word having the lowest absolute
2551 position number. The next word contains the next 32 bits.
2552
2553 Thus, the following code can be used to access a bit whose position
2554 according to @code{scm_array_handle_dims} is given in @var{pos}:
2555
2556 @example
2557 SCM bit_array;
2558 scm_t_array_handle handle;
2559 scm_t_uint32 *bits;
2560 ssize_t pos;
2561 size_t abs_pos;
2562 size_t word_pos, mask;
2563
2564 scm_array_get_handle (&bit_array, &handle);
2565 bits = scm_array_handle_bit_elements (&handle);
2566
2567 pos = ...
2568 abs_pos = pos + scm_array_handle_bit_elements_offset (&handle);
2569 word_pos = abs_pos / 32;
2570 mask = 1L << (abs_pos % 32);
2571
2572 if (bits[word_pos] & mask)
2573 /* bit is set. */
2574
2575 scm_array_handle_release (&handle);
2576 @end example
2577
2578 @end deftypefn
2579
2580 @deftypefn {C Function} {scm_t_uint32 *} scm_array_handle_bit_writable_elements (scm_t_array_handle *handle)
2581 Like @code{scm_array_handle_bit_elements} but the pointer is good for
2582 reading and writing. You must take care not to modify bits outside of
2583 the allowed index range of the array, even for contiguous arrays.
2584 @end deftypefn
2585
2586 @node Records
2587 @subsection Records
2588
2589 A @dfn{record type} is a first class object representing a user-defined
2590 data type. A @dfn{record} is an instance of a record type.
2591
2592 @deffn {Scheme Procedure} record? obj
2593 Return @code{#t} if @var{obj} is a record of any type and @code{#f}
2594 otherwise.
2595
2596 Note that @code{record?} may be true of any Scheme value; there is no
2597 promise that records are disjoint with other Scheme types.
2598 @end deffn
2599
2600 @deffn {Scheme Procedure} make-record-type type-name field-names
2601 Return a @dfn{record-type descriptor}, a value representing a new data
2602 type disjoint from all others. The @var{type-name} argument must be a
2603 string, but is only used for debugging purposes (such as the printed
2604 representation of a record of the new type). The @var{field-names}
2605 argument is a list of symbols naming the @dfn{fields} of a record of the
2606 new type. It is an error if the list contains any duplicates. It is
2607 unspecified how record-type descriptors are represented.
2608 @end deffn
2609
2610 @deffn {Scheme Procedure} record-constructor rtd [field-names]
2611 Return a procedure for constructing new members of the type represented
2612 by @var{rtd}. The returned procedure accepts exactly as many arguments
2613 as there are symbols in the given list, @var{field-names}; these are
2614 used, in order, as the initial values of those fields in a new record,
2615 which is returned by the constructor procedure. The values of any
2616 fields not named in that list are unspecified. The @var{field-names}
2617 argument defaults to the list of field names in the call to
2618 @code{make-record-type} that created the type represented by @var{rtd};
2619 if the @var{field-names} argument is provided, it is an error if it
2620 contains any duplicates or any symbols not in the default list.
2621 @end deffn
2622
2623 @deffn {Scheme Procedure} record-predicate rtd
2624 Return a procedure for testing membership in the type represented by
2625 @var{rtd}. The returned procedure accepts exactly one argument and
2626 returns a true value if the argument is a member of the indicated record
2627 type; it returns a false value otherwise.
2628 @end deffn
2629
2630 @deffn {Scheme Procedure} record-accessor rtd field-name
2631 Return a procedure for reading the value of a particular field of a
2632 member of the type represented by @var{rtd}. The returned procedure
2633 accepts exactly one argument which must be a record of the appropriate
2634 type; it returns the current value of the field named by the symbol
2635 @var{field-name} in that record. The symbol @var{field-name} must be a
2636 member of the list of field-names in the call to @code{make-record-type}
2637 that created the type represented by @var{rtd}.
2638 @end deffn
2639
2640 @deffn {Scheme Procedure} record-modifier rtd field-name
2641 Return a procedure for writing the value of a particular field of a
2642 member of the type represented by @var{rtd}. The returned procedure
2643 accepts exactly two arguments: first, a record of the appropriate type,
2644 and second, an arbitrary Scheme value; it modifies the field named by
2645 the symbol @var{field-name} in that record to contain the given value.
2646 The returned value of the modifier procedure is unspecified. The symbol
2647 @var{field-name} must be a member of the list of field-names in the call
2648 to @code{make-record-type} that created the type represented by
2649 @var{rtd}.
2650 @end deffn
2651
2652 @deffn {Scheme Procedure} record-type-descriptor record
2653 Return a record-type descriptor representing the type of the given
2654 record. That is, for example, if the returned descriptor were passed to
2655 @code{record-predicate}, the resulting predicate would return a true
2656 value when passed the given record. Note that it is not necessarily the
2657 case that the returned descriptor is the one that was passed to
2658 @code{record-constructor} in the call that created the constructor
2659 procedure that created the given record.
2660 @end deffn
2661
2662 @deffn {Scheme Procedure} record-type-name rtd
2663 Return the type-name associated with the type represented by rtd. The
2664 returned value is @code{eqv?} to the @var{type-name} argument given in
2665 the call to @code{make-record-type} that created the type represented by
2666 @var{rtd}.
2667 @end deffn
2668
2669 @deffn {Scheme Procedure} record-type-fields rtd
2670 Return a list of the symbols naming the fields in members of the type
2671 represented by @var{rtd}. The returned value is @code{equal?} to the
2672 field-names argument given in the call to @code{make-record-type} that
2673 created the type represented by @var{rtd}.
2674 @end deffn
2675
2676
2677 @node Structures
2678 @subsection Structures
2679 @tpindex Structures
2680
2681 [FIXME: this is pasted in from Tom Lord's original guile.texi and should
2682 be reviewed]
2683
2684 A @dfn{structure type} is a first class user-defined data type. A
2685 @dfn{structure} is an instance of a structure type. A structure type is
2686 itself a structure.
2687
2688 Structures are less abstract and more general than traditional records.
2689 In fact, in Guile Scheme, records are implemented using structures.
2690
2691 @menu
2692 * Structure Concepts:: The structure of Structures
2693 * Structure Layout:: Defining the layout of structure types
2694 * Structure Basics:: make-, -ref and -set! procedures for structs
2695 * Vtables:: Accessing type-specific data
2696 @end menu
2697
2698 @node Structure Concepts
2699 @subsubsection Structure Concepts
2700
2701 A structure object consists of a handle, structure data, and a vtable.
2702 The handle is a Scheme value which points to both the vtable and the
2703 structure's data. Structure data is a dynamically allocated region of
2704 memory, private to the structure, divided up into typed fields. A
2705 vtable is another structure used to hold type-specific data. Multiple
2706 structures can share a common vtable.
2707
2708 Three concepts are key to understanding structures.
2709
2710 @itemize @bullet{}
2711 @item @dfn{layout specifications}
2712
2713 Layout specifications determine how memory allocated to structures is
2714 divided up into fields. Programmers must write a layout specification
2715 whenever a new type of structure is defined.
2716
2717 @item @dfn{structural accessors}
2718
2719 Structure access is by field number. There is only one set of
2720 accessors common to all structure objects.
2721
2722 @item @dfn{vtables}
2723
2724 Vtables, themselves structures, are first class representations of
2725 disjoint sub-types of structures in general. In most cases, when a
2726 new structure is created, programmers must specify a vtable for the
2727 new structure. Each vtable has a field describing the layout of its
2728 instances. Vtables can have additional, user-defined fields as well.
2729 @end itemize
2730
2731
2732
2733 @node Structure Layout
2734 @subsubsection Structure Layout
2735
2736 When a structure is created, a region of memory is allocated to hold its
2737 state. The @dfn{layout} of the structure's type determines how that
2738 memory is divided into fields.
2739
2740 Each field has a specified type. There are only three types allowed, each
2741 corresponding to a one letter code. The allowed types are:
2742
2743 @itemize @bullet{}
2744 @item 'u' -- unprotected
2745
2746 The field holds binary data that is not GC protected.
2747
2748 @item 'p' -- protected
2749
2750 The field holds a Scheme value and is GC protected.
2751
2752 @item 's' -- self
2753
2754 The field holds a Scheme value and is GC protected. When a structure is
2755 created with this type of field, the field is initialized to refer to
2756 the structure's own handle. This kind of field is mainly useful when
2757 mixing Scheme and C code in which the C code may need to compute a
2758 structure's handle given only the address of its malloc'd data.
2759 @end itemize
2760
2761
2762 Each field also has an associated access protection. There are only
2763 three kinds of protection, each corresponding to a one letter code.
2764 The allowed protections are:
2765
2766 @itemize @bullet{}
2767 @item 'w' -- writable
2768
2769 The field can be read and written.
2770
2771 @item 'r' -- readable
2772
2773 The field can be read, but not written.
2774
2775 @item 'o' -- opaque
2776
2777 The field can be neither read nor written. This kind
2778 of protection is for fields useful only to built-in routines.
2779 @end itemize
2780
2781 A layout specification is described by stringing together pairs
2782 of letters: one to specify a field type and one to specify a field
2783 protection. For example, a traditional cons pair type object could
2784 be described as:
2785
2786 @example
2787 ; cons pairs have two writable fields of Scheme data
2788 "pwpw"
2789 @end example
2790
2791 A pair object in which the first field is held constant could be:
2792
2793 @example
2794 "prpw"
2795 @end example
2796
2797 Binary fields, (fields of type "u"), hold one @dfn{word} each. The
2798 size of a word is a machine dependent value defined to be equal to the
2799 value of the C expression: @code{sizeof (long)}.
2800
2801 The last field of a structure layout may specify a tail array.
2802 A tail array is indicated by capitalizing the field's protection
2803 code ('W', 'R' or 'O'). A tail-array field is replaced by
2804 a read-only binary data field containing an array size. The array
2805 size is determined at the time the structure is created. It is followed
2806 by a corresponding number of fields of the type specified for the
2807 tail array. For example, a conventional Scheme vector can be
2808 described as:
2809
2810 @example
2811 ; A vector is an arbitrary number of writable fields holding Scheme
2812 ; values:
2813 "pW"
2814 @end example
2815
2816 In the above example, field 0 contains the size of the vector and
2817 fields beginning at 1 contain the vector elements.
2818
2819 A kind of tagged vector (a constant tag followed by conventional
2820 vector elements) might be:
2821
2822 @example
2823 "prpW"
2824 @end example
2825
2826
2827 Structure layouts are represented by specially interned symbols whose
2828 name is a string of type and protection codes. To create a new
2829 structure layout, use this procedure:
2830
2831 @deffn {Scheme Procedure} make-struct-layout fields
2832 @deffnx {C Function} scm_make_struct_layout (fields)
2833 Return a new structure layout object.
2834
2835 @var{fields} must be a string made up of pairs of characters
2836 strung together. The first character of each pair describes a field
2837 type, the second a field protection. Allowed types are 'p' for
2838 GC-protected Scheme data, 'u' for unprotected binary data, and 's' for
2839 a field that points to the structure itself. Allowed protections
2840 are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque
2841 fields. The last field protection specification may be capitalized to
2842 indicate that the field is a tail-array.
2843 @end deffn
2844
2845
2846
2847 @node Structure Basics
2848 @subsubsection Structure Basics
2849
2850 This section describes the basic procedures for creating and accessing
2851 structures.
2852
2853 @deffn {Scheme Procedure} make-struct vtable tail_array_size . init
2854 @deffnx {C Function} scm_make_struct (vtable, tail_array_size, init)
2855 Create a new structure.
2856
2857 @var{type} must be a vtable structure (@pxref{Vtables}).
2858
2859 @var{tail-elts} must be a non-negative integer. If the layout
2860 specification indicated by @var{type} includes a tail-array,
2861 this is the number of elements allocated to that array.
2862
2863 The @var{init1}, @dots{} are optional arguments describing how
2864 successive fields of the structure should be initialized. Only fields
2865 with protection 'r' or 'w' can be initialized, except for fields of
2866 type 's', which are automatically initialized to point to the new
2867 structure itself; fields with protection 'o' can not be initialized by
2868 Scheme programs.
2869
2870 If fewer optional arguments than initializable fields are supplied,
2871 fields of type 'p' get default value #f while fields of type 'u' are
2872 initialized to 0.
2873
2874 Structs are currently the basic representation for record-like data
2875 structures in Guile. The plan is to eventually replace them with a
2876 new representation which will at the same time be easier to use and
2877 more powerful.
2878
2879 For more information, see the documentation for @code{make-vtable-vtable}.
2880 @end deffn
2881
2882 @deffn {Scheme Procedure} struct? x
2883 @deffnx {C Function} scm_struct_p (x)
2884 Return @code{#t} iff @var{x} is a structure object, else
2885 @code{#f}.
2886 @end deffn
2887
2888
2889 @deffn {Scheme Procedure} struct-ref handle pos
2890 @deffnx {Scheme Procedure} struct-set! struct n value
2891 @deffnx {C Function} scm_struct_ref (handle, pos)
2892 @deffnx {C Function} scm_struct_set_x (struct, n, value)
2893 Access (or modify) the @var{n}th field of @var{struct}.
2894
2895 If the field is of type 'p', then it can be set to an arbitrary value.
2896
2897 If the field is of type 'u', then it can only be set to a non-negative
2898 integer value small enough to fit in one machine word.
2899 @end deffn
2900
2901
2902
2903 @node Vtables
2904 @subsubsection Vtables
2905
2906 Vtables are structures that are used to represent structure types. Each
2907 vtable contains a layout specification in field
2908 @code{vtable-index-layout} -- instances of the type are laid out
2909 according to that specification. Vtables contain additional fields
2910 which are used only internally to libguile. The variable
2911 @code{vtable-offset-user} is bound to a field number. Vtable fields
2912 at that position or greater are user definable.
2913
2914 @deffn {Scheme Procedure} struct-vtable handle
2915 @deffnx {C Function} scm_struct_vtable (handle)
2916 Return the vtable structure that describes the type of @var{struct}.
2917 @end deffn
2918
2919 @deffn {Scheme Procedure} struct-vtable? x
2920 @deffnx {C Function} scm_struct_vtable_p (x)
2921 Return @code{#t} iff @var{x} is a vtable structure.
2922 @end deffn
2923
2924 If you have a vtable structure, @code{V}, you can create an instance of
2925 the type it describes by using @code{(make-struct V ...)}. But where
2926 does @code{V} itself come from? One possibility is that @code{V} is an
2927 instance of a user-defined vtable type, @code{V'}, so that @code{V} is
2928 created by using @code{(make-struct V' ...)}. Another possibility is
2929 that @code{V} is an instance of the type it itself describes. Vtable
2930 structures of the second sort are created by this procedure:
2931
2932 @deffn {Scheme Procedure} make-vtable-vtable user_fields tail_array_size . init
2933 @deffnx {C Function} scm_make_vtable_vtable (user_fields, tail_array_size, init)
2934 Return a new, self-describing vtable structure.
2935
2936 @var{user-fields} is a string describing user defined fields of the
2937 vtable beginning at index @code{vtable-offset-user}
2938 (see @code{make-struct-layout}).
2939
2940 @var{tail-size} specifies the size of the tail-array (if any) of
2941 this vtable.
2942
2943 @var{init1}, @dots{} are the optional initializers for the fields of
2944 the vtable.
2945
2946 Vtables have one initializable system field---the struct printer.
2947 This field comes before the user fields in the initializers passed
2948 to @code{make-vtable-vtable} and @code{make-struct}, and thus works as
2949 a third optional argument to @code{make-vtable-vtable} and a fourth to
2950 @code{make-struct} when creating vtables:
2951
2952 If the value is a procedure, it will be called instead of the standard
2953 printer whenever a struct described by this vtable is printed.
2954 The procedure will be called with arguments STRUCT and PORT.
2955
2956 The structure of a struct is described by a vtable, so the vtable is
2957 in essence the type of the struct. The vtable is itself a struct with
2958 a vtable. This could go on forever if it weren't for the
2959 vtable-vtables which are self-describing vtables, and thus terminate
2960 the chain.
2961
2962 There are several potential ways of using structs, but the standard
2963 one is to use three kinds of structs, together building up a type
2964 sub-system: one vtable-vtable working as the root and one or several
2965 "types", each with a set of "instances". (The vtable-vtable should be
2966 compared to the class <class> which is the class of itself.)
2967
2968 @lisp
2969 (define ball-root (make-vtable-vtable "pr" 0))
2970
2971 (define (make-ball-type ball-color)
2972 (make-struct ball-root 0
2973 (make-struct-layout "pw")
2974 (lambda (ball port)
2975 (format port "#<a ~A ball owned by ~A>"
2976 (color ball)
2977 (owner ball)))
2978 ball-color))
2979 (define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))
2980 (define (owner ball) (struct-ref ball 0))
2981
2982 (define red (make-ball-type 'red))
2983 (define green (make-ball-type 'green))
2984
2985 (define (make-ball type owner) (make-struct type 0 owner))
2986
2987 (define ball (make-ball green 'Nisse))
2988 ball @result{} #<a green ball owned by Nisse>
2989 @end lisp
2990 @end deffn
2991
2992 @deffn {Scheme Procedure} struct-vtable-name vtable
2993 @deffnx {C Function} scm_struct_vtable_name (vtable)
2994 Return the name of the vtable @var{vtable}.
2995 @end deffn
2996
2997 @deffn {Scheme Procedure} set-struct-vtable-name! vtable name
2998 @deffnx {C Function} scm_set_struct_vtable_name_x (vtable, name)
2999 Set the name of the vtable @var{vtable} to @var{name}.
3000 @end deffn
3001
3002 @deffn {Scheme Procedure} struct-vtable-tag handle
3003 @deffnx {C Function} scm_struct_vtable_tag (handle)
3004 Return the vtable tag of the structure @var{handle}.
3005 @end deffn
3006
3007
3008 @node Dictionary Types
3009 @subsection Dictionary Types
3010
3011 A @dfn{dictionary} object is a data structure used to index
3012 information in a user-defined way. In standard Scheme, the main
3013 aggregate data types are lists and vectors. Lists are not really
3014 indexed at all, and vectors are indexed only by number
3015 (e.g. @code{(vector-ref foo 5)}). Often you will find it useful
3016 to index your data on some other type; for example, in a library
3017 catalog you might want to look up a book by the name of its
3018 author. Dictionaries are used to help you organize information in
3019 such a way.
3020
3021 An @dfn{association list} (or @dfn{alist} for short) is a list of
3022 key-value pairs. Each pair represents a single quantity or
3023 object; the @code{car} of the pair is a key which is used to
3024 identify the object, and the @code{cdr} is the object's value.
3025
3026 A @dfn{hash table} also permits you to index objects with
3027 arbitrary keys, but in a way that makes looking up any one object
3028 extremely fast. A well-designed hash system makes hash table
3029 lookups almost as fast as conventional array or vector references.
3030
3031 Alists are popular among Lisp programmers because they use only
3032 the language's primitive operations (lists, @dfn{car}, @dfn{cdr}
3033 and the equality primitives). No changes to the language core are
3034 necessary. Therefore, with Scheme's built-in list manipulation
3035 facilities, it is very convenient to handle data stored in an
3036 association list. Also, alists are highly portable and can be
3037 easily implemented on even the most minimal Lisp systems.
3038
3039 However, alists are inefficient, especially for storing large
3040 quantities of data. Because we want Guile to be useful for large
3041 software systems as well as small ones, Guile provides a rich set
3042 of tools for using either association lists or hash tables.
3043
3044 @node Association Lists
3045 @subsection Association Lists
3046 @tpindex Association Lists
3047 @tpindex Alist
3048 @cindex association List
3049 @cindex alist
3050 @cindex aatabase
3051
3052 An association list is a conventional data structure that is often used
3053 to implement simple key-value databases. It consists of a list of
3054 entries in which each entry is a pair. The @dfn{key} of each entry is
3055 the @code{car} of the pair and the @dfn{value} of each entry is the
3056 @code{cdr}.
3057
3058 @example
3059 ASSOCIATION LIST ::= '( (KEY1 . VALUE1)
3060 (KEY2 . VALUE2)
3061 (KEY3 . VALUE3)
3062 @dots{}
3063 )
3064 @end example
3065
3066 @noindent
3067 Association lists are also known, for short, as @dfn{alists}.
3068
3069 The structure of an association list is just one example of the infinite
3070 number of possible structures that can be built using pairs and lists.
3071 As such, the keys and values in an association list can be manipulated
3072 using the general list structure procedures @code{cons}, @code{car},
3073 @code{cdr}, @code{set-car!}, @code{set-cdr!} and so on. However,
3074 because association lists are so useful, Guile also provides specific
3075 procedures for manipulating them.
3076
3077 @menu
3078 * Alist Key Equality::
3079 * Adding or Setting Alist Entries::
3080 * Retrieving Alist Entries::
3081 * Removing Alist Entries::
3082 * Sloppy Alist Functions::
3083 * Alist Example::
3084 @end menu
3085
3086 @node Alist Key Equality
3087 @subsubsection Alist Key Equality
3088
3089 All of Guile's dedicated association list procedures, apart from
3090 @code{acons}, come in three flavours, depending on the level of equality
3091 that is required to decide whether an existing key in the association
3092 list is the same as the key that the procedure call uses to identify the
3093 required entry.
3094
3095 @itemize @bullet
3096 @item
3097 Procedures with @dfn{assq} in their name use @code{eq?} to determine key
3098 equality.
3099
3100 @item
3101 Procedures with @dfn{assv} in their name use @code{eqv?} to determine
3102 key equality.
3103
3104 @item
3105 Procedures with @dfn{assoc} in their name use @code{equal?} to
3106 determine key equality.
3107 @end itemize
3108
3109 @code{acons} is an exception because it is used to build association
3110 lists which do not require their entries' keys to be unique.
3111
3112 @node Adding or Setting Alist Entries
3113 @subsubsection Adding or Setting Alist Entries
3114
3115 @code{acons} adds a new entry to an association list and returns the
3116 combined association list. The combined alist is formed by consing the
3117 new entry onto the head of the alist specified in the @code{acons}
3118 procedure call. So the specified alist is not modified, but its
3119 contents become shared with the tail of the combined alist that
3120 @code{acons} returns.
3121
3122 In the most common usage of @code{acons}, a variable holding the
3123 original association list is updated with the combined alist:
3124
3125 @example
3126 (set! address-list (acons name address address-list))
3127 @end example
3128
3129 In such cases, it doesn't matter that the old and new values of
3130 @code{address-list} share some of their contents, since the old value is
3131 usually no longer independently accessible.
3132
3133 Note that @code{acons} adds the specified new entry regardless of
3134 whether the alist may already contain entries with keys that are, in
3135 some sense, the same as that of the new entry. Thus @code{acons} is
3136 ideal for building alists where there is no concept of key uniqueness.
3137
3138 @example
3139 (set! task-list (acons 3 "pay gas bill" '()))
3140 task-list
3141 @result{}
3142 ((3 . "pay gas bill"))
3143
3144 (set! task-list (acons 3 "tidy bedroom" task-list))
3145 task-list
3146 @result{}
3147 ((3 . "tidy bedroom") (3 . "pay gas bill"))
3148 @end example
3149
3150 @code{assq-set!}, @code{assv-set!} and @code{assoc-set!} are used to add
3151 or replace an entry in an association list where there @emph{is} a
3152 concept of key uniqueness. If the specified association list already
3153 contains an entry whose key is the same as that specified in the
3154 procedure call, the existing entry is replaced by the new one.
3155 Otherwise, the new entry is consed onto the head of the old association
3156 list to create the combined alist. In all cases, these procedures
3157 return the combined alist.
3158
3159 @code{assq-set!} and friends @emph{may} destructively modify the
3160 structure of the old association list in such a way that an existing
3161 variable is correctly updated without having to @code{set!} it to the
3162 value returned:
3163
3164 @example
3165 address-list
3166 @result{}
3167 (("mary" . "34 Elm Road") ("james" . "16 Bow Street"))
3168
3169 (assoc-set! address-list "james" "1a London Road")
3170 @result{}
3171 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
3172
3173 address-list
3174 @result{}
3175 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
3176 @end example
3177
3178 Or they may not:
3179
3180 @example
3181 (assoc-set! address-list "bob" "11 Newington Avenue")
3182 @result{}
3183 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
3184 ("james" . "1a London Road"))
3185
3186 address-list
3187 @result{}
3188 (("mary" . "34 Elm Road") ("james" . "1a London Road"))
3189 @end example
3190
3191 The only safe way to update an association list variable when adding or
3192 replacing an entry like this is to @code{set!} the variable to the
3193 returned value:
3194
3195 @example
3196 (set! address-list
3197 (assoc-set! address-list "bob" "11 Newington Avenue"))
3198 address-list
3199 @result{}
3200 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
3201 ("james" . "1a London Road"))
3202 @end example
3203
3204 Because of this slight inconvenience, you may find it more convenient to
3205 use hash tables to store dictionary data. If your application will not
3206 be modifying the contents of an alist very often, this may not make much
3207 difference to you.
3208
3209 If you need to keep the old value of an association list in a form
3210 independent from the list that results from modification by
3211 @code{acons}, @code{assq-set!}, @code{assv-set!} or @code{assoc-set!},
3212 use @code{list-copy} to copy the old association list before modifying
3213 it.
3214
3215 @deffn {Scheme Procedure} acons key value alist
3216 @deffnx {C Function} scm_acons (key, value, alist)
3217 Add a new key-value pair to @var{alist}. A new pair is
3218 created whose car is @var{key} and whose cdr is @var{value}, and the
3219 pair is consed onto @var{alist}, and the new list is returned. This
3220 function is @emph{not} destructive; @var{alist} is not modified.
3221 @end deffn
3222
3223 @deffn {Scheme Procedure} assq-set! alist key val
3224 @deffnx {Scheme Procedure} assv-set! alist key value
3225 @deffnx {Scheme Procedure} assoc-set! alist key value
3226 @deffnx {C Function} scm_assq_set_x (alist, key, val)
3227 @deffnx {C Function} scm_assv_set_x (alist, key, val)
3228 @deffnx {C Function} scm_assoc_set_x (alist, key, val)
3229 Reassociate @var{key} in @var{alist} with @var{value}: find any existing
3230 @var{alist} entry for @var{key} and associate it with the new
3231 @var{value}. If @var{alist} does not contain an entry for @var{key},
3232 add a new one. Return the (possibly new) alist.
3233
3234 These functions do not attempt to verify the structure of @var{alist},
3235 and so may cause unusual results if passed an object that is not an
3236 association list.
3237 @end deffn
3238
3239 @node Retrieving Alist Entries
3240 @subsubsection Retrieving Alist Entries
3241 @rnindex assq
3242 @rnindex assv
3243 @rnindex assoc
3244
3245 @code{assq}, @code{assv} and @code{assoc} find the entry in an alist
3246 for a given key, and return the @code{(@var{key} . @var{value})} pair.
3247 @code{assq-ref}, @code{assv-ref} and @code{assoc-ref} do a similar
3248 lookup, but return just the @var{value}.
3249
3250 @deffn {Scheme Procedure} assq key alist
3251 @deffnx {Scheme Procedure} assv key alist
3252 @deffnx {Scheme Procedure} assoc key alist
3253 @deffnx {C Function} scm_assq (key, alist)
3254 @deffnx {C Function} scm_assv (key, alist)
3255 @deffnx {C Function} scm_assoc (key, alist)
3256 Return the first entry in @var{alist} with the given @var{key}. The
3257 return is the pair @code{(KEY . VALUE)} from @var{alist}. If there's
3258 no matching entry the return is @code{#f}.
3259
3260 @code{assq} compares keys with @code{eq?}, @code{assv} uses
3261 @code{eqv?} and @code{assoc} uses @code{equal?}.
3262 @end deffn
3263
3264 @deffn {Scheme Procedure} assq-ref alist key
3265 @deffnx {Scheme Procedure} assv-ref alist key
3266 @deffnx {Scheme Procedure} assoc-ref alist key
3267 @deffnx {C Function} scm_assq_ref (alist, key)
3268 @deffnx {C Function} scm_assv_ref (alist, key)
3269 @deffnx {C Function} scm_assoc_ref (alist, key)
3270 Return the value from the first entry in @var{alist} with the given
3271 @var{key}, or @code{#f} if there's no such entry.
3272
3273 @code{assq-ref} compares keys with @code{eq?}, @code{assv-ref} uses
3274 @code{eqv?} and @code{assoc-ref} uses @code{equal?}.
3275
3276 Notice these functions have the @var{key} argument last, like other
3277 @code{-ref} functions, but this is opposite to what what @code{assq}
3278 etc above use.
3279
3280 When the return is @code{#f} it can be either @var{key} not found, or
3281 an entry which happens to have value @code{#f} in the @code{cdr}. Use
3282 @code{assq} etc above if you need to differentiate these cases.
3283 @end deffn
3284
3285
3286 @node Removing Alist Entries
3287 @subsubsection Removing Alist Entries
3288
3289 To remove the element from an association list whose key matches a
3290 specified key, use @code{assq-remove!}, @code{assv-remove!} or
3291 @code{assoc-remove!} (depending, as usual, on the level of equality
3292 required between the key that you specify and the keys in the
3293 association list).
3294
3295 As with @code{assq-set!} and friends, the specified alist may or may not
3296 be modified destructively, and the only safe way to update a variable
3297 containing the alist is to @code{set!} it to the value that
3298 @code{assq-remove!} and friends return.
3299
3300 @example
3301 address-list
3302 @result{}
3303 (("bob" . "11 Newington Avenue") ("mary" . "34 Elm Road")
3304 ("james" . "1a London Road"))
3305
3306 (set! address-list (assoc-remove! address-list "mary"))
3307 address-list
3308 @result{}
3309 (("bob" . "11 Newington Avenue") ("james" . "1a London Road"))
3310 @end example
3311
3312 Note that, when @code{assq/v/oc-remove!} is used to modify an
3313 association list that has been constructed only using the corresponding
3314 @code{assq/v/oc-set!}, there can be at most one matching entry in the
3315 alist, so the question of multiple entries being removed in one go does
3316 not arise. If @code{assq/v/oc-remove!} is applied to an association
3317 list that has been constructed using @code{acons}, or an
3318 @code{assq/v/oc-set!} with a different level of equality, or any mixture
3319 of these, it removes only the first matching entry from the alist, even
3320 if the alist might contain further matching entries. For example:
3321
3322 @example
3323 (define address-list '())
3324 (set! address-list (assq-set! address-list "mary" "11 Elm Street"))
3325 (set! address-list (assq-set! address-list "mary" "57 Pine Drive"))
3326 address-list
3327 @result{}
3328 (("mary" . "57 Pine Drive") ("mary" . "11 Elm Street"))
3329
3330 (set! address-list (assoc-remove! address-list "mary"))
3331 address-list
3332 @result{}
3333 (("mary" . "11 Elm Street"))
3334 @end example
3335
3336 In this example, the two instances of the string "mary" are not the same
3337 when compared using @code{eq?}, so the two @code{assq-set!} calls add
3338 two distinct entries to @code{address-list}. When compared using
3339 @code{equal?}, both "mary"s in @code{address-list} are the same as the
3340 "mary" in the @code{assoc-remove!} call, but @code{assoc-remove!} stops
3341 after removing the first matching entry that it finds, and so one of the
3342 "mary" entries is left in place.
3343
3344 @deffn {Scheme Procedure} assq-remove! alist key
3345 @deffnx {Scheme Procedure} assv-remove! alist key
3346 @deffnx {Scheme Procedure} assoc-remove! alist key
3347 @deffnx {C Function} scm_assq_remove_x (alist, key)
3348 @deffnx {C Function} scm_assv_remove_x (alist, key)
3349 @deffnx {C Function} scm_assoc_remove_x (alist, key)
3350 Delete the first entry in @var{alist} associated with @var{key}, and return
3351 the resulting alist.
3352 @end deffn
3353
3354 @node Sloppy Alist Functions
3355 @subsubsection Sloppy Alist Functions
3356
3357 @code{sloppy-assq}, @code{sloppy-assv} and @code{sloppy-assoc} behave
3358 like the corresponding non-@code{sloppy-} procedures, except that they
3359 return @code{#f} when the specified association list is not well-formed,
3360 where the non-@code{sloppy-} versions would signal an error.
3361
3362 Specifically, there are two conditions for which the non-@code{sloppy-}
3363 procedures signal an error, which the @code{sloppy-} procedures handle
3364 instead by returning @code{#f}. Firstly, if the specified alist as a
3365 whole is not a proper list:
3366
3367 @example
3368 (assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
3369 @result{}
3370 ERROR: In procedure assoc in expression (assoc "mary" (quote #)):
3371 ERROR: Wrong type argument in position 2 (expecting association list): ((1 . 2) ("key" . "door") . "open sesame")
3372
3373 (sloppy-assoc "mary" '((1 . 2) ("key" . "door") . "open sesame"))
3374 @result{}
3375 #f
3376 @end example
3377
3378 @noindent
3379 Secondly, if one of the entries in the specified alist is not a pair:
3380
3381 @example
3382 (assoc 2 '((1 . 1) 2 (3 . 9)))
3383 @result{}
3384 ERROR: In procedure assoc in expression (assoc 2 (quote #)):
3385 ERROR: Wrong type argument in position 2 (expecting association list): ((1 . 1) 2 (3 . 9))
3386
3387 (sloppy-assoc 2 '((1 . 1) 2 (3 . 9)))
3388 @result{}
3389 #f
3390 @end example
3391
3392 Unless you are explicitly working with badly formed association lists,
3393 it is much safer to use the non-@code{sloppy-} procedures, because they
3394 help to highlight coding and data errors that the @code{sloppy-}
3395 versions would silently cover up.
3396
3397 @deffn {Scheme Procedure} sloppy-assq key alist
3398 @deffnx {C Function} scm_sloppy_assq (key, alist)
3399 Behaves like @code{assq} but does not do any error checking.
3400 Recommended only for use in Guile internals.
3401 @end deffn
3402
3403 @deffn {Scheme Procedure} sloppy-assv key alist
3404 @deffnx {C Function} scm_sloppy_assv (key, alist)
3405 Behaves like @code{assv} but does not do any error checking.
3406 Recommended only for use in Guile internals.
3407 @end deffn
3408
3409 @deffn {Scheme Procedure} sloppy-assoc key alist
3410 @deffnx {C Function} scm_sloppy_assoc (key, alist)
3411 Behaves like @code{assoc} but does not do any error checking.
3412 Recommended only for use in Guile internals.
3413 @end deffn
3414
3415 @node Alist Example
3416 @subsubsection Alist Example
3417
3418 Here is a longer example of how alists may be used in practice.
3419
3420 @lisp
3421 (define capitals '(("New York" . "Albany")
3422 ("Oregon" . "Salem")
3423 ("Florida" . "Miami")))
3424
3425 ;; What's the capital of Oregon?
3426 (assoc "Oregon" capitals) @result{} ("Oregon" . "Salem")
3427 (assoc-ref capitals "Oregon") @result{} "Salem"
3428
3429 ;; We left out South Dakota.
3430 (set! capitals
3431 (assoc-set! capitals "South Dakota" "Pierre"))
3432 capitals
3433 @result{} (("South Dakota" . "Pierre")
3434 ("New York" . "Albany")
3435 ("Oregon" . "Salem")
3436 ("Florida" . "Miami"))
3437
3438 ;; And we got Florida wrong.
3439 (set! capitals
3440 (assoc-set! capitals "Florida" "Tallahassee"))
3441 capitals
3442 @result{} (("South Dakota" . "Pierre")
3443 ("New York" . "Albany")
3444 ("Oregon" . "Salem")
3445 ("Florida" . "Tallahassee"))
3446
3447 ;; After Oregon secedes, we can remove it.
3448 (set! capitals
3449 (assoc-remove! capitals "Oregon"))
3450 capitals
3451 @result{} (("South Dakota" . "Pierre")
3452 ("New York" . "Albany")
3453 ("Florida" . "Tallahassee"))
3454 @end lisp
3455
3456 @node Hash Tables
3457 @subsection Hash Tables
3458 @tpindex Hash Tables
3459
3460 Hash tables are dictionaries which offer similar functionality as
3461 association lists: They provide a mapping from keys to values. The
3462 difference is that association lists need time linear in the size of
3463 elements when searching for entries, whereas hash tables can normally
3464 search in constant time. The drawback is that hash tables require a
3465 little bit more memory, and that you can not use the normal list
3466 procedures (@pxref{Lists}) for working with them.
3467
3468 Guile provides two types of hashtables. One is an abstract data type
3469 that can only be manipulated with the functions in this section. The
3470 other type is concrete: it uses a normal vector with alists as
3471 elements. The advantage of the abstract hash tables is that they will
3472 be automatically resized when they become too full or too empty.
3473
3474 @menu
3475 * Hash Table Examples:: Demonstration of hash table usage.
3476 * Hash Table Reference:: Hash table procedure descriptions.
3477 @end menu
3478
3479
3480 @node Hash Table Examples
3481 @subsubsection Hash Table Examples
3482
3483 For demonstration purposes, this section gives a few usage examples of
3484 some hash table procedures, together with some explanation what they do.
3485
3486 First we start by creating a new hash table with 31 slots, and
3487 populate it with two key/value pairs.
3488
3489 @lisp
3490 (define h (make-hash-table 31))
3491
3492 ;; This is an opaque object
3493 h
3494 @result{}
3495 #<hash-table 0/31>
3496
3497 ;; We can also use a vector of alists.
3498 (define h (make-vector 7 '()))
3499
3500 h
3501 @result{}
3502 #(() () () () () () ())
3503
3504 ;; Inserting into a hash table can be done with hashq-set!
3505 (hashq-set! h 'foo "bar")
3506 @result{}
3507 "bar"
3508
3509 (hashq-set! h 'braz "zonk")
3510 @result{}
3511 "zonk"
3512
3513 ;; Or with hash-create-handle!
3514 (hashq-create-handle! h 'frob #f)
3515 @result{}
3516 (frob . #f)
3517
3518 ;; The vector now contains three elements in the alists and the frob
3519 ;; entry is at index (hashq 'frob).
3520 h
3521 @result{}
3522 #(() () () () ((frob . #f) (braz . "zonk")) () ((foo . "bar")))
3523
3524 (hashq 'frob)
3525 @result{}
3526 4
3527
3528 @end lisp
3529
3530 You can get the value for a given key with the procedure
3531 @code{hashq-ref}, but the problem with this procedure is that you
3532 cannot reliably determine whether a key does exists in the table. The
3533 reason is that the procedure returns @code{#f} if the key is not in
3534 the table, but it will return the same value if the key is in the
3535 table and just happens to have the value @code{#f}, as you can see in
3536 the following examples.
3537
3538 @lisp
3539 (hashq-ref h 'foo)
3540 @result{}
3541 "bar"
3542
3543 (hashq-ref h 'frob)
3544 @result{}
3545 #f
3546
3547 (hashq-ref h 'not-there)
3548 @result{}
3549 #f
3550 @end lisp
3551
3552 Better is to use the procedure @code{hashq-get-handle}, which makes a
3553 distinction between the two cases. Just like @code{assq}, this
3554 procedure returns a key/value-pair on success, and @code{#f} if the
3555 key is not found.
3556
3557 @lisp
3558 (hashq-get-handle h 'foo)
3559 @result{}
3560 (foo . "bar")
3561
3562 (hashq-get-handle h 'not-there)
3563 @result{}
3564 #f
3565 @end lisp
3566
3567 There is no procedure for calculating the number of key/value-pairs in
3568 a hash table, but @code{hash-fold} can be used for doing exactly that.
3569
3570 @lisp
3571 (hash-fold (lambda (key value seed) (+ 1 seed)) 0 h)
3572 @result{}
3573 3
3574 @end lisp
3575
3576 @node Hash Table Reference
3577 @subsubsection Hash Table Reference
3578
3579 @c FIXME: Describe in broad terms what happens for resizing, and what
3580 @c the initial size means for this.
3581
3582 Like the association list functions, the hash table functions come in
3583 several varieties, according to the equality test used for the keys.
3584 Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
3585 functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
3586 the @code{hashx-} functions use an application supplied test.
3587
3588 A single @code{make-hash-table} creates a hash table suitable for use
3589 with any set of functions, but it's imperative that just one set is
3590 then used consistently, or results will be unpredictable.
3591
3592 Hash tables are implemented as a vector indexed by a hash value formed
3593 from the key, with an association list of key/value pairs for each
3594 bucket in case distinct keys hash together. Direct access to the
3595 pairs in those lists is provided by the @code{-handle-} functions.
3596 The abstract kind of hash tables hide the vector in an opaque object
3597 that represents the hash table, while for the concrete kind the vector
3598 @emph{is} the hashtable.
3599
3600 When the number of table entries in an abstract hash table goes above
3601 a threshold, the vector is made larger and the entries are rehashed,
3602 to prevent the bucket lists from becoming too long and slowing down
3603 accesses. When the number of entries goes below a threshold, the
3604 vector is shrunk to save space.
3605
3606 A abstract hash table is created with @code{make-hash-table}. To
3607 create a vector that is suitable as a hash table, use
3608 @code{(make-vector @var{size} '())}, for example.
3609
3610 For the @code{hashx-} ``extended'' routines, an application supplies a
3611 @var{hash} function producing an integer index like @code{hashq} etc
3612 below, and an @var{assoc} alist search function like @code{assq} etc
3613 (@pxref{Retrieving Alist Entries}). Here's an example of such
3614 functions implementing case-insensitive hashing of string keys,
3615
3616 @example
3617 (use-modules (srfi srfi-1)
3618 (srfi srfi-13))
3619
3620 (define (my-hash str size)
3621 (remainder (string-hash-ci str) size))
3622 (define (my-assoc str alist)
3623 (find (lambda (pair) (string-ci=? str (car pair))) alist))
3624
3625 (define my-table (make-hash-table))
3626 (hashx-set! my-hash my-assoc my-table "foo" 123)
3627
3628 (hashx-ref my-hash my-assoc my-table "FOO")
3629 @result{} 123
3630 @end example
3631
3632 In a @code{hashx-} @var{hash} function the aim is to spread keys
3633 across the vector, so bucket lists don't become long. But the actual
3634 values are arbitrary as long as they're in the range 0 to
3635 @math{@var{size}-1}. Helpful functions for forming a hash value, in
3636 addition to @code{hashq} etc below, include @code{symbol-hash}
3637 (@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci}
3638 (@pxref{String Comparison}), and @code{char-set-hash}
3639 (@pxref{Character Set Predicates/Comparison}).
3640
3641 @sp 1
3642 @deffn {Scheme Procedure} make-hash-table [size]
3643 Create a new abstract hash table object, with an optional minimum
3644 vector @var{size}.
3645
3646 When @var{size} is given, the table vector will still grow and shrink
3647 automatically, as described above, but with @var{size} as a minimum.
3648 If an application knows roughly how many entries the table will hold
3649 then it can use @var{size} to avoid rehashing when initial entries are
3650 added.
3651 @end deffn
3652
3653 @deffn {Scheme Procedure} hash-table? obj
3654 @deffnx {C Function} scm_hash_table_p (obj)
3655 Return @code{#t} if @var{obj} is a abstract hash table object.
3656 @end deffn
3657
3658 @deffn {Scheme Procedure} hash-clear! table
3659 @deffnx {C Function} scm_hash_clear_x (table)
3660 Remove all items from @var{table} (without triggering a resize).
3661 @end deffn
3662
3663 @deffn {Scheme Procedure} hash-ref table key [dflt]
3664 @deffnx {Scheme Procedure} hashq-ref table key [dflt]
3665 @deffnx {Scheme Procedure} hashv-ref table key [dflt]
3666 @deffnx {Scheme Procedure} hashx-ref hash assoc table key [dflt]
3667 @deffnx {C Function} scm_hash_ref (table, key, dflt)
3668 @deffnx {C Function} scm_hashq_ref (table, key, dflt)
3669 @deffnx {C Function} scm_hashv_ref (table, key, dflt)
3670 @deffnx {C Function} scm_hashx_ref (hash, assoc, table, key, dflt)
3671 Lookup @var{key} in the given hash @var{table}, and return the
3672 associated value. If @var{key} is not found, return @var{dflt}, or
3673 @code{#f} if @var{dflt} is not given.
3674 @end deffn
3675
3676 @deffn {Scheme Procedure} hash-set! table key val
3677 @deffnx {Scheme Procedure} hashq-set! table key val
3678 @deffnx {Scheme Procedure} hashv-set! table key val
3679 @deffnx {Scheme Procedure} hashx-set! hash assoc table key val
3680 @deffnx {C Function} scm_hash_set_x (table, key, val)
3681 @deffnx {C Function} scm_hashq_set_x (table, key, val)
3682 @deffnx {C Function} scm_hashv_set_x (table, key, val)
3683 @deffnx {C Function} scm_hashx_set_x (hash, assoc, table, key, val)
3684 Associate @var{val} with @var{key} in the given hash @var{table}. If
3685 @var{key} is already present then it's associated value is changed.
3686 If it's not present then a new entry is created.
3687 @end deffn
3688
3689 @deffn {Scheme Procedure} hash-remove! table key
3690 @deffnx {Scheme Procedure} hashq-remove! table key
3691 @deffnx {Scheme Procedure} hashv-remove! table key
3692 @deffnx {Scheme Procedure} hashx-remove! hash assoc table key
3693 @deffnx {C Function} scm_hash_remove_x (table, key)
3694 @deffnx {C Function} scm_hashq_remove_x (table, key)
3695 @deffnx {C Function} scm_hashv_remove_x (table, key)
3696 @deffnx {C Function} scm_hashx_remove_x (hash, assoc, table, key)
3697 Remove any association for @var{key} in the given hash @var{table}.
3698 If @var{key} is not in @var{table} then nothing is done.
3699 @end deffn
3700
3701 @deffn {Scheme Procedure} hash key size
3702 @deffnx {Scheme Procedure} hashq key size
3703 @deffnx {Scheme Procedure} hashv key size
3704 @deffnx {C Function} scm_hash (key, size)
3705 @deffnx {C Function} scm_hashq (key, size)
3706 @deffnx {C Function} scm_hashv (key, size)
3707 Return a hash value for @var{key}. This is a number in the range
3708 @math{0} to @math{@var{size}-1}, which is suitable for use in a hash
3709 table of the given @var{size}.
3710
3711 Note that @code{hashq} and @code{hashv} may use internal addresses of
3712 objects, so if an object is garbage collected and re-created it can
3713 have a different hash value, even when the two are notionally
3714 @code{eq?}. For instance with symbols,
3715
3716 @example
3717 (hashq 'something 123) @result{} 19
3718 (gc)
3719 (hashq 'something 123) @result{} 62
3720 @end example
3721
3722 In normal use this is not a problem, since an object entered into a
3723 hash table won't be garbage collected until removed. It's only if
3724 hashing calculations are somehow separated from normal references that
3725 its lifetime needs to be considered.
3726 @end deffn
3727
3728 @deffn {Scheme Procedure} hash-get-handle table key
3729 @deffnx {Scheme Procedure} hashq-get-handle table key
3730 @deffnx {Scheme Procedure} hashv-get-handle table key
3731 @deffnx {Scheme Procedure} hashx-get-handle hash assoc table key
3732 @deffnx {C Function} scm_hash_get_handle (table, key)
3733 @deffnx {C Function} scm_hashq_get_handle (table, key)
3734 @deffnx {C Function} scm_hashv_get_handle (table, key)
3735 @deffnx {C Function} scm_hashx_get_handle (hash, assoc, table, key)
3736 Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
3737 given hash @var{table}, or @code{#f} if @var{key} is not in
3738 @var{table}.
3739 @end deffn
3740
3741 @deffn {Scheme Procedure} hash-create-handle! table key init
3742 @deffnx {Scheme Procedure} hashq-create-handle! table key init
3743 @deffnx {Scheme Procedure} hashv-create-handle! table key init
3744 @deffnx {Scheme Procedure} hashx-create-handle! hash assoc table key init
3745 @deffnx {C Function} scm_hash_create_handle_x (table, key, init)
3746 @deffnx {C Function} scm_hashq_create_handle_x (table, key, init)
3747 @deffnx {C Function} scm_hashv_create_handle_x (table, key, init)
3748 @deffnx {C Function} scm_hashx_create_handle_x (hash, assoc, table, key, init)
3749 Return the @code{(@var{key} . @var{value})} pair for @var{key} in the
3750 given hash @var{table}. If @var{key} is not in @var{table} then
3751 create an entry for it with @var{init} as the value, and return that
3752 pair.
3753 @end deffn
3754
3755 @deffn {Scheme Procedure} hash-map->list proc table
3756 @deffnx {Scheme Procedure} hash-for-each proc table
3757 @deffnx {C Function} scm_hash_map_to_list (proc, table)
3758 @deffnx {C Function} scm_hash_for_each (proc, table)
3759 Apply @var{proc} to the entries in the given hash @var{table}. Each
3760 call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map->list}
3761 returns a list of the results from these calls, @code{hash-for-each}
3762 discards the results and returns an unspecified value.
3763
3764 Calls are made over the table entries in an unspecified order, and for
3765 @code{hash-map->list} the order of the values in the returned list is
3766 unspecified. Results will be unpredictable if @var{table} is modified
3767 while iterating.
3768
3769 For example the following returns a new alist comprising all the
3770 entries from @code{mytable}, in no particular order.
3771
3772 @example
3773 (hash-map->list cons mytable)
3774 @end example
3775 @end deffn
3776
3777 @deffn {Scheme Procedure} hash-for-each-handle proc table
3778 @deffnx {C Function} scm_hash_for_each_handle (proc, table)
3779 Apply @var{proc} to the entries in the given hash @var{table}. Each
3780 call is @code{(@var{proc} @var{handle})}, where @var{handle} is a
3781 @code{(@var{key} . @var{value})} pair. Return an unspecified value.
3782
3783 @code{hash-for-each-handle} differs from @code{hash-for-each} only in
3784 the argument list of @var{proc}.
3785 @end deffn
3786
3787 @deffn {Scheme Procedure} hash-fold proc init table
3788 @deffnx {C Function} scm_hash_fold (proc, init, table)
3789 Accumulate a result by applying @var{proc} to the elements of the
3790 given hash @var{table}. Each call is @code{(@var{proc} @var{key}
3791 @var{value} @var{prior-result})}, where @var{key} and @var{value} are
3792 from the @var{table} and @var{prior-result} is the return from the
3793 previous @var{proc} call. For the first call, @var{prior-result} is
3794 the given @var{init} value.
3795
3796 Calls are made over the table entries in an unspecified order.
3797 Results will be unpredictable if @var{table} is modified while
3798 @code{hash-fold} is running.
3799
3800 For example, the following returns a count of how many keys in
3801 @code{mytable} are strings.
3802
3803 @example
3804 (hash-fold (lambda (key value prior)
3805 (if (string? key) (1+ prior) prior))
3806 0 mytable)
3807 @end example
3808 @end deffn
3809
3810
3811 @c Local Variables:
3812 @c TeX-master: "guile.texi"
3813 @c End: