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