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