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