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