Commit | Line | Data |
---|---|---|
07d83abe MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004 | |
4 | @c Free Software Foundation, Inc. | |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
7 | @page | |
8 | @node Compound Data Types | |
9 | @section Compound Data Types | |
10 | ||
11 | This chapter describes Guile's compound data types. By @dfn{compound} | |
12 | we mean that the primary purpose of these data types is to act as | |
13 | containers for other kinds of data (including other compound objects). | |
14 | For instance, a (non-uniform) vector with length 5 is a container that | |
15 | can hold five arbitrary Scheme objects. | |
16 | ||
17 | The various kinds of container object differ from each other in how | |
18 | their memory is allocated, how they are indexed, and how particular | |
19 | values can be looked up within them. | |
20 | ||
21 | @menu | |
22 | * Pairs:: Scheme's basic building block. | |
23 | * Lists:: Special list functions supported by Guile. | |
24 | * Vectors:: One-dimensional arrays of Scheme objects. | |
61eed960 | 25 | * Uniform Numeric Vectors:: Vectors with elements of a single numeric type. |
e6b226b9 | 26 | * Bit Vectors:: Vectors of bits. |
61eed960 | 27 | * Generalized Vectors:: Treating all vector-like things uniformly. |
e6b226b9 | 28 | * Arrays:: Matrices, etc. |
07d83abe MV |
29 | * Records:: |
30 | * Structures:: | |
07d83abe MV |
31 | * Dictionary Types:: About dictionary types in general. |
32 | * Association Lists:: List-based dictionaries. | |
33 | * Hash Tables:: Table-based dictionaries. | |
34 | @end menu | |
35 | ||
36 | ||
37 | @node Pairs | |
38 | @subsection Pairs | |
39 | @tpindex Pairs | |
40 | ||
41 | Pairs are used to combine two Scheme objects into one compound object. | |
42 | Hence the name: A pair stores a pair of objects. | |
43 | ||
44 | The data type @dfn{pair} is extremely important in Scheme, just like in | |
45 | any other Lisp dialect. The reason is that pairs are not only used to | |
46 | make two values available as one object, but that pairs are used for | |
47 | constructing lists of values. Because lists are so important in Scheme, | |
48 | they are described in a section of their own (@pxref{Lists}). | |
49 | ||
50 | Pairs can literally get entered in source code or at the REPL, in the | |
51 | so-called @dfn{dotted list} syntax. This syntax consists of an opening | |
52 | parentheses, the first element of the pair, a dot, the second element | |
53 | and a closing parentheses. The following example shows how a pair | |
54 | consisting of the two numbers 1 and 2, and a pair containing the symbols | |
55 | @code{foo} and @code{bar} can be entered. It is very important to write | |
56 | the whitespace before and after the dot, because otherwise the Scheme | |
57 | parser would not be able to figure out where to split the tokens. | |
58 | ||
59 | @lisp | |
60 | (1 . 2) | |
61 | (foo . bar) | |
62 | @end lisp | |
63 | ||
64 | But beware, if you want to try out these examples, you have to | |
65 | @dfn{quote} the expressions. More information about quotation is | |
66 | available in the section (REFFIXME). The correct way to try these | |
67 | examples is as follows. | |
68 | ||
69 | @lisp | |
70 | '(1 . 2) | |
71 | @result{} | |
72 | (1 . 2) | |
73 | '(foo . bar) | |
74 | @result{} | |
75 | (foo . bar) | |
76 | @end lisp | |
77 | ||
78 | A new pair is made by calling the procedure @code{cons} with two | |
79 | arguments. Then the argument values are stored into a newly allocated | |
80 | pair, and the pair is returned. The name @code{cons} stands for | |
81 | "construct". Use the procedure @code{pair?} to test whether a | |
82 | given Scheme object is a pair or not. | |
83 | ||
84 | @rnindex cons | |
85 | @deffn {Scheme Procedure} cons x y | |
86 | @deffnx {C Function} scm_cons (x, y) | |
87 | Return a newly allocated pair whose car is @var{x} and whose | |
88 | cdr is @var{y}. The pair is guaranteed to be different (in the | |
89 | sense of @code{eq?}) from every previously existing object. | |
90 | @end deffn | |
91 | ||
92 | @rnindex pair? | |
93 | @deffn {Scheme Procedure} pair? x | |
94 | @deffnx {C Function} scm_pair_p (x) | |
95 | Return @code{#t} if @var{x} is a pair; otherwise return | |
96 | @code{#f}. | |
97 | @end deffn | |
98 | ||
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 | |
107 | Scheme programs is to access the car of a pair, or the car of the cdr of | |
108 | a pair, etc., the procedures called @code{caar}, @code{cadr} and so on | |
109 | are also predefined. | |
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 | ||
7f4c83e3 MV |
120 | @deffn {Scheme Procedure} cddr pair |
121 | @deffnx {Scheme Procedure} cdar pair | |
122 | @deffnx {Scheme Procedure} cadr pair | |
123 | @deffnx {Scheme Procedure} caar pair | |
124 | @deffnx {Scheme Procedure} cdddr pair | |
125 | @deffnx {Scheme Procedure} cddar pair | |
126 | @deffnx {Scheme Procedure} cdadr pair | |
127 | @deffnx {Scheme Procedure} cdaar pair | |
128 | @deffnx {Scheme Procedure} caddr pair | |
129 | @deffnx {Scheme Procedure} cadar pair | |
130 | @deffnx {Scheme Procedure} caadr pair | |
131 | @deffnx {Scheme Procedure} caaar pair | |
07d83abe | 132 | @deffnx {Scheme Procedure} cddddr pair |
7f4c83e3 MV |
133 | @deffnx {Scheme Procedure} cdddar pair |
134 | @deffnx {Scheme Procedure} cddadr pair | |
135 | @deffnx {Scheme Procedure} cddaar pair | |
136 | @deffnx {Scheme Procedure} cdaddr pair | |
137 | @deffnx {Scheme Procedure} cdadar pair | |
138 | @deffnx {Scheme Procedure} cdaadr pair | |
139 | @deffnx {Scheme Procedure} cdaaar pair | |
140 | @deffnx {Scheme Procedure} cadddr pair | |
141 | @deffnx {Scheme Procedure} caddar pair | |
142 | @deffnx {Scheme Procedure} cadadr pair | |
143 | @deffnx {Scheme Procedure} cadaar pair | |
144 | @deffnx {Scheme Procedure} caaddr pair | |
145 | @deffnx {Scheme Procedure} caadar pair | |
146 | @deffnx {Scheme Procedure} caaadr pair | |
147 | @deffnx {Scheme Procedure} caaaar pair | |
148 | @deffnx {C Function} scm_cddr (pair) | |
149 | @deffnx {C Function} scm_cdar (pair) | |
150 | @deffnx {C Function} scm_cadr (pair) | |
151 | @deffnx {C Function} scm_caar (pair) | |
152 | @deffnx {C Function} scm_cdddr (pair) | |
153 | @deffnx {C Function} scm_cddar (pair) | |
154 | @deffnx {C Function} scm_cdadr (pair) | |
155 | @deffnx {C Function} scm_cdaar (pair) | |
156 | @deffnx {C Function} scm_caddr (pair) | |
157 | @deffnx {C Function} scm_cadar (pair) | |
158 | @deffnx {C Function} scm_caadr (pair) | |
159 | @deffnx {C Function} scm_caaar (pair) | |
160 | @deffnx {C Function} scm_cddddr (pair) | |
161 | @deffnx {C Function} scm_cdddar (pair) | |
162 | @deffnx {C Function} scm_cddadr (pair) | |
163 | @deffnx {C Function} scm_cddaar (pair) | |
164 | @deffnx {C Function} scm_cdaddr (pair) | |
165 | @deffnx {C Function} scm_cdadar (pair) | |
166 | @deffnx {C Function} scm_cdaadr (pair) | |
167 | @deffnx {C Function} scm_cdaaar (pair) | |
168 | @deffnx {C Function} scm_cadddr (pair) | |
169 | @deffnx {C Function} scm_caddar (pair) | |
170 | @deffnx {C Function} scm_cadadr (pair) | |
171 | @deffnx {C Function} scm_cadaar (pair) | |
172 | @deffnx {C Function} scm_caaddr (pair) | |
173 | @deffnx {C Function} scm_caadar (pair) | |
174 | @deffnx {C Function} scm_caaadr (pair) | |
175 | @deffnx {C Function} scm_caaaar (pair) | |
07d83abe MV |
176 | These procedures are compositions of @code{car} and @code{cdr}, where |
177 | for example @code{caddr} could be defined by | |
178 | ||
179 | @lisp | |
180 | (define caddr (lambda (x) (car (cdr (cdr x))))) | |
181 | @end lisp | |
182 | @end deffn | |
183 | ||
184 | @rnindex set-car! | |
185 | @deffn {Scheme Procedure} set-car! pair value | |
186 | @deffnx {C Function} scm_set_car_x (pair, value) | |
187 | Stores @var{value} in the car field of @var{pair}. The value returned | |
188 | by @code{set-car!} is unspecified. | |
189 | @end deffn | |
190 | ||
191 | @rnindex set-cdr! | |
192 | @deffn {Scheme Procedure} set-cdr! pair value | |
193 | @deffnx {C Function} scm_set_cdr_x (pair, value) | |
194 | Stores @var{value} in the cdr field of @var{pair}. The value returned | |
195 | by @code{set-cdr!} is unspecified. | |
196 | @end deffn | |
197 | ||
198 | ||
199 | @node Lists | |
200 | @subsection Lists | |
201 | @tpindex Lists | |
202 | ||
203 | A very important data type in Scheme---as well as in all other Lisp | |
204 | dialects---is the data type @dfn{list}.@footnote{Strictly speaking, | |
205 | Scheme does not have a real datatype @dfn{list}. Lists are made up of | |
206 | @dfn{chained pairs}, and only exist by definition---a list is a chain | |
207 | of pairs which looks like a list.} | |
208 | ||
209 | This is the short definition of what a list is: | |
210 | ||
211 | @itemize @bullet | |
212 | @item | |
213 | Either the empty list @code{()}, | |
214 | ||
215 | @item | |
216 | or a pair which has a list in its cdr. | |
217 | @end itemize | |
218 | ||
219 | @c FIXME::martin: Describe the pair chaining in more detail. | |
220 | ||
221 | @c FIXME::martin: What is a proper, what an improper list? | |
222 | @c What is a circular list? | |
223 | ||
224 | @c FIXME::martin: Maybe steal some graphics from the Elisp reference | |
225 | @c manual? | |
226 | ||
227 | @menu | |
228 | * List Syntax:: Writing literal lists. | |
229 | * List Predicates:: Testing lists. | |
230 | * List Constructors:: Creating new lists. | |
231 | * List Selection:: Selecting from lists, getting their length. | |
232 | * Append/Reverse:: Appending and reversing lists. | |
233 | * List Modification:: Modifying existing lists. | |
234 | * List Searching:: Searching for list elements | |
235 | * List Mapping:: Applying procedures to lists. | |
236 | @end menu | |
237 | ||
238 | @node List Syntax | |
239 | @subsubsection List Read Syntax | |
240 | ||
241 | The syntax for lists is an opening parentheses, then all the elements of | |
242 | the list (separated by whitespace) and finally a closing | |
243 | parentheses.@footnote{Note that there is no separation character between | |
244 | the list elements, like a comma or a semicolon.}. | |
245 | ||
246 | @lisp | |
247 | (1 2 3) ; @r{a list of the numbers 1, 2 and 3} | |
248 | ("foo" bar 3.1415) ; @r{a string, a symbol and a real number} | |
249 | () ; @r{the empty list} | |
250 | @end lisp | |
251 | ||
252 | The last example needs a bit more explanation. A list with no elements, | |
253 | called the @dfn{empty list}, is special in some ways. It is used for | |
254 | terminating lists by storing it into the cdr of the last pair that makes | |
255 | up a list. An example will clear that up: | |
256 | ||
257 | @lisp | |
258 | (car '(1)) | |
259 | @result{} | |
260 | 1 | |
261 | (cdr '(1)) | |
262 | @result{} | |
263 | () | |
264 | @end lisp | |
265 | ||
266 | This example also shows that lists have to be quoted (REFFIXME) when | |
267 | written, because they would otherwise be mistakingly taken as procedure | |
268 | applications (@pxref{Simple Invocation}). | |
269 | ||
270 | ||
271 | @node List Predicates | |
272 | @subsubsection List Predicates | |
273 | ||
274 | Often it is useful to test whether a given Scheme object is a list or | |
275 | not. List-processing procedures could use this information to test | |
276 | whether their input is valid, or they could do different things | |
277 | depending on the datatype of their arguments. | |
278 | ||
279 | @rnindex list? | |
280 | @deffn {Scheme Procedure} list? x | |
281 | @deffnx {C Function} scm_list_p (x) | |
282 | Return @code{#t} iff @var{x} is a proper list, else @code{#f}. | |
283 | @end deffn | |
284 | ||
285 | The predicate @code{null?} is often used in list-processing code to | |
286 | tell whether a given list has run out of elements. That is, a loop | |
287 | somehow deals with the elements of a list until the list satisfies | |
288 | @code{null?}. Then, the algorithm terminates. | |
289 | ||
290 | @rnindex null? | |
291 | @deffn {Scheme Procedure} null? x | |
292 | @deffnx {C Function} scm_null_p (x) | |
293 | Return @code{#t} iff @var{x} is the empty list, else @code{#f}. | |
294 | @end deffn | |
295 | ||
7f4c83e3 MV |
296 | @deftypefn {C Function} int scm_is_null (SCM x) |
297 | Return 1 when @var{x} is the empty list; otherwise return 0. | |
298 | @end deftypefn | |
299 | ||
300 | ||
07d83abe MV |
301 | @node List Constructors |
302 | @subsubsection List Constructors | |
303 | ||
304 | This section describes the procedures for constructing new lists. | |
305 | @code{list} simply returns a list where the elements are the arguments, | |
306 | @code{cons*} is similar, but the last argument is stored in the cdr of | |
307 | the last pair of the list. | |
308 | ||
309 | @c C Function scm_list(rest) used to be documented here, but it's a | |
310 | @c no-op since it does nothing but return the list the caller must | |
311 | @c have already created. | |
312 | @c | |
313 | @deffn {Scheme Procedure} list elem1 @dots{} elemN | |
314 | @deffnx {C Function} scm_list_1 (elem1) | |
315 | @deffnx {C Function} scm_list_2 (elem1, elem2) | |
316 | @deffnx {C Function} scm_list_3 (elem1, elem2, elem3) | |
317 | @deffnx {C Function} scm_list_4 (elem1, elem2, elem3, elem4) | |
318 | @deffnx {C Function} scm_list_5 (elem1, elem2, elem3, elem4, elem5) | |
319 | @deffnx {C Function} scm_list_n (elem1, @dots{}, elemN, @nicode{SCM_UNDEFINED}) | |
320 | @rnindex list | |
321 | Return a new list containing elements @var{elem1} to @var{elemN}. | |
322 | ||
323 | @code{scm_list_n} takes a variable number of arguments, terminated by | |
324 | the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is | |
325 | not included in the list. None of @var{elem1} to @var{elemN} can | |
326 | themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will | |
327 | terminate at that point. | |
328 | @end deffn | |
329 | ||
330 | @c C Function scm_cons_star(arg1,rest) used to be documented here, | |
331 | @c but it's not really a useful interface, since it expects the | |
332 | @c caller to have already consed up all but the first argument | |
333 | @c already. | |
334 | @c | |
335 | @deffn {Scheme Procedure} cons* arg1 arg2 @dots{} | |
336 | Like @code{list}, but the last arg provides the tail of the | |
337 | constructed list, returning @code{(cons @var{arg1} (cons | |
338 | @var{arg2} (cons @dots{} @var{argn})))}. Requires at least one | |
339 | argument. If given one argument, that argument is returned as | |
340 | result. This function is called @code{list*} in some other | |
341 | Schemes and in Common LISP. | |
342 | @end deffn | |
343 | ||
344 | @deffn {Scheme Procedure} list-copy lst | |
345 | @deffnx {C Function} scm_list_copy (lst) | |
346 | Return a (newly-created) copy of @var{lst}. | |
347 | @end deffn | |
348 | ||
349 | @deffn {Scheme Procedure} make-list n [init] | |
350 | Create a list containing of @var{n} elements, where each element is | |
351 | initialized to @var{init}. @var{init} defaults to the empty list | |
352 | @code{()} if not given. | |
353 | @end deffn | |
354 | ||
355 | Note that @code{list-copy} only makes a copy of the pairs which make up | |
356 | the spine of the lists. The list elements are not copied, which means | |
357 | that modifying the elements of the new list also modifies the elements | |
358 | of the old list. On the other hand, applying procedures like | |
359 | @code{set-cdr!} or @code{delv!} to the new list will not alter the old | |
360 | list. If you also need to copy the list elements (making a deep copy), | |
361 | use the procedure @code{copy-tree} (@pxref{Copying}). | |
362 | ||
363 | @node List Selection | |
364 | @subsubsection List Selection | |
365 | ||
366 | These procedures are used to get some information about a list, or to | |
367 | retrieve one or more elements of a list. | |
368 | ||
369 | @rnindex length | |
370 | @deffn {Scheme Procedure} length lst | |
371 | @deffnx {C Function} scm_length (lst) | |
372 | Return the number of elements in list @var{lst}. | |
373 | @end deffn | |
374 | ||
375 | @deffn {Scheme Procedure} last-pair lst | |
376 | @deffnx {C Function} scm_last_pair (lst) | |
cdf1ad3b | 377 | Return the last pair in @var{lst}, signalling an error if |
07d83abe MV |
378 | @var{lst} is circular. |
379 | @end deffn | |
380 | ||
381 | @rnindex list-ref | |
382 | @deffn {Scheme Procedure} list-ref list k | |
383 | @deffnx {C Function} scm_list_ref (list, k) | |
384 | Return the @var{k}th element from @var{list}. | |
385 | @end deffn | |
386 | ||
387 | @rnindex list-tail | |
388 | @deffn {Scheme Procedure} list-tail lst k | |
389 | @deffnx {Scheme Procedure} list-cdr-ref lst k | |
390 | @deffnx {C Function} scm_list_tail (lst, k) | |
391 | Return the "tail" of @var{lst} beginning with its @var{k}th element. | |
392 | The first element of the list is considered to be element 0. | |
393 | ||
394 | @code{list-tail} and @code{list-cdr-ref} are identical. It may help to | |
395 | think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list, | |
396 | or returning the results of cdring @var{k} times down @var{lst}. | |
397 | @end deffn | |
398 | ||
399 | @deffn {Scheme Procedure} list-head lst k | |
400 | @deffnx {C Function} scm_list_head (lst, k) | |
401 | Copy the first @var{k} elements from @var{lst} into a new list, and | |
402 | return it. | |
403 | @end deffn | |
404 | ||
405 | @node Append/Reverse | |
406 | @subsubsection Append and Reverse | |
407 | ||
408 | @code{append} and @code{append!} are used to concatenate two or more | |
409 | lists in order to form a new list. @code{reverse} and @code{reverse!} | |
410 | return lists with the same elements as their arguments, but in reverse | |
411 | order. The procedure variants with an @code{!} directly modify the | |
412 | pairs which form the list, whereas the other procedures create new | |
413 | pairs. This is why you should be careful when using the side-effecting | |
414 | variants. | |
415 | ||
416 | @rnindex append | |
417 | @deffn {Scheme Procedure} append lst1 @dots{} lstN | |
418 | @deffnx {Scheme Procedure} append! lst1 @dots{} lstN | |
419 | @deffnx {C Function} scm_append (lstlst) | |
420 | @deffnx {C Function} scm_append_x (lstlst) | |
421 | Return a list comprising all the elements of lists @var{lst1} to | |
422 | @var{lstN}. | |
423 | ||
424 | @lisp | |
425 | (append '(x) '(y)) @result{} (x y) | |
426 | (append '(a) '(b c d)) @result{} (a b c d) | |
427 | (append '(a (b)) '((c))) @result{} (a (b) (c)) | |
428 | @end lisp | |
429 | ||
430 | The last argument @var{lstN} may actually be any object; an improper | |
431 | list results if the last argument is not a proper list. | |
432 | ||
433 | @lisp | |
434 | (append '(a b) '(c . d)) @result{} (a b c . d) | |
435 | (append '() 'a) @result{} a | |
436 | @end lisp | |
437 | ||
438 | @code{append} doesn't modify the given lists, but the return may share | |
439 | structure with the final @var{lstN}. @code{append!} modifies the | |
440 | given lists to form its return. | |
441 | ||
442 | For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list | |
443 | of the list operands @var{lst1} @dots{} @var{lstN}. That @var{lstlst} | |
444 | itself is not modified or used in the return. | |
445 | @end deffn | |
446 | ||
447 | @rnindex reverse | |
448 | @deffn {Scheme Procedure} reverse lst | |
449 | @deffnx {Scheme Procedure} reverse! lst [newtail] | |
450 | @deffnx {C Function} scm_reverse (lst) | |
451 | @deffnx {C Function} scm_reverse_x (lst, newtail) | |
452 | Return a list comprising the elements of @var{lst}, in reverse order. | |
453 | ||
454 | @code{reverse} constructs a new list, @code{reverse!} modifies | |
455 | @var{lst} in constructing its return. | |
456 | ||
457 | For @code{reverse!}, the optional @var{newtail} is appended to to the | |
458 | result. @var{newtail} isn't reversed, it simply becomes the list | |
459 | tail. For @code{scm_reverse_x}, the @var{newtail} parameter is | |
460 | mandatory, but can be @code{SCM_EOL} if no further tail is required. | |
461 | @end deffn | |
462 | ||
463 | @node List Modification | |
464 | @subsubsection List Modification | |
465 | ||
466 | The following procedures modify an existing list, either by changing | |
467 | elements of the list, or by changing the list structure itself. | |
468 | ||
469 | @deffn {Scheme Procedure} list-set! list k val | |
470 | @deffnx {C Function} scm_list_set_x (list, k, val) | |
471 | Set the @var{k}th element of @var{list} to @var{val}. | |
472 | @end deffn | |
473 | ||
474 | @deffn {Scheme Procedure} list-cdr-set! list k val | |
475 | @deffnx {C Function} scm_list_cdr_set_x (list, k, val) | |
476 | Set the @var{k}th cdr of @var{list} to @var{val}. | |
477 | @end deffn | |
478 | ||
479 | @deffn {Scheme Procedure} delq item lst | |
480 | @deffnx {C Function} scm_delq (item, lst) | |
481 | Return a newly-created copy of @var{lst} with elements | |
482 | @code{eq?} to @var{item} removed. This procedure mirrors | |
483 | @code{memq}: @code{delq} compares elements of @var{lst} against | |
484 | @var{item} with @code{eq?}. | |
485 | @end deffn | |
486 | ||
487 | @deffn {Scheme Procedure} delv item lst | |
488 | @deffnx {C Function} scm_delv (item, lst) | |
489 | Return a newly-created copy of @var{lst} with elements | |
490 | @code{eqv?} to @var{item} removed. This procedure mirrors | |
491 | @code{memv}: @code{delv} compares elements of @var{lst} against | |
492 | @var{item} with @code{eqv?}. | |
493 | @end deffn | |
494 | ||
495 | @deffn {Scheme Procedure} delete item lst | |
496 | @deffnx {C Function} scm_delete (item, lst) | |
497 | Return a newly-created copy of @var{lst} with elements | |
498 | @code{equal?} to @var{item} removed. This procedure mirrors | |
499 | @code{member}: @code{delete} compares elements of @var{lst} | |
500 | against @var{item} with @code{equal?}. | |
501 | @end deffn | |
502 | ||
503 | @deffn {Scheme Procedure} delq! item lst | |
504 | @deffnx {Scheme Procedure} delv! item lst | |
505 | @deffnx {Scheme Procedure} delete! item lst | |
506 | @deffnx {C Function} scm_delq_x (item, lst) | |
507 | @deffnx {C Function} scm_delv_x (item, lst) | |
508 | @deffnx {C Function} scm_delete_x (item, lst) | |
509 | These procedures are destructive versions of @code{delq}, @code{delv} | |
510 | and @code{delete}: they modify the pointers in the existing @var{lst} | |
511 | rather than creating a new list. Caveat evaluator: Like other | |
512 | destructive list functions, these functions cannot modify the binding of | |
513 | @var{lst}, and so cannot be used to delete the first element of | |
514 | @var{lst} destructively. | |
515 | @end deffn | |
516 | ||
517 | @deffn {Scheme Procedure} delq1! item lst | |
518 | @deffnx {C Function} scm_delq1_x (item, lst) | |
519 | Like @code{delq!}, but only deletes the first occurrence of | |
520 | @var{item} from @var{lst}. Tests for equality using | |
521 | @code{eq?}. See also @code{delv1!} and @code{delete1!}. | |
522 | @end deffn | |
523 | ||
524 | @deffn {Scheme Procedure} delv1! item lst | |
525 | @deffnx {C Function} scm_delv1_x (item, lst) | |
526 | Like @code{delv!}, but only deletes the first occurrence of | |
527 | @var{item} from @var{lst}. Tests for equality using | |
528 | @code{eqv?}. See also @code{delq1!} and @code{delete1!}. | |
529 | @end deffn | |
530 | ||
531 | @deffn {Scheme Procedure} delete1! item lst | |
532 | @deffnx {C Function} scm_delete1_x (item, lst) | |
533 | Like @code{delete!}, but only deletes the first occurrence of | |
534 | @var{item} from @var{lst}. Tests for equality using | |
535 | @code{equal?}. See also @code{delq1!} and @code{delv1!}. | |
536 | @end deffn | |
537 | ||
538 | @deffn {Scheme Procedure} filter pred lst | |
539 | @deffnx {Scheme Procedure} filter! pred lst | |
540 | Return a list containing all elements from @var{lst} which satisfy the | |
541 | predicate @var{pred}. The elements in the result list have the same | |
542 | order as in @var{lst}. The order in which @var{pred} is applied to | |
543 | the list elements is not specified. | |
544 | ||
545 | @code{filter!} is allowed, but not required to modify the structure of | |
546 | @end deffn | |
547 | ||
548 | @node List Searching | |
549 | @subsubsection List Searching | |
550 | ||
551 | The following procedures search lists for particular elements. They use | |
552 | different comparison predicates for comparing list elements with the | |
553 | object to be searched. When they fail, they return @code{#f}, otherwise | |
554 | they return the sublist whose car is equal to the search object, where | |
555 | equality depends on the equality predicate used. | |
556 | ||
557 | @rnindex memq | |
558 | @deffn {Scheme Procedure} memq x lst | |
559 | @deffnx {C Function} scm_memq (x, lst) | |
560 | Return the first sublist of @var{lst} whose car is @code{eq?} | |
561 | to @var{x} where the sublists of @var{lst} are the non-empty | |
562 | lists returned by @code{(list-tail @var{lst} @var{k})} for | |
563 | @var{k} less than the length of @var{lst}. If @var{x} does not | |
564 | occur in @var{lst}, then @code{#f} (not the empty list) is | |
565 | returned. | |
566 | @end deffn | |
567 | ||
568 | @rnindex memv | |
569 | @deffn {Scheme Procedure} memv x lst | |
570 | @deffnx {C Function} scm_memv (x, lst) | |
571 | Return the first sublist of @var{lst} whose car is @code{eqv?} | |
572 | to @var{x} where the sublists of @var{lst} are the non-empty | |
573 | lists returned by @code{(list-tail @var{lst} @var{k})} for | |
574 | @var{k} less than the length of @var{lst}. If @var{x} does not | |
575 | occur in @var{lst}, then @code{#f} (not the empty list) is | |
576 | returned. | |
577 | @end deffn | |
578 | ||
579 | @rnindex member | |
580 | @deffn {Scheme Procedure} member x lst | |
581 | @deffnx {C Function} scm_member (x, lst) | |
582 | Return the first sublist of @var{lst} whose car is | |
583 | @code{equal?} to @var{x} where the sublists of @var{lst} are | |
584 | the non-empty lists returned by @code{(list-tail @var{lst} | |
585 | @var{k})} for @var{k} less than the length of @var{lst}. If | |
586 | @var{x} does not occur in @var{lst}, then @code{#f} (not the | |
587 | empty list) is returned. | |
588 | @end deffn | |
589 | ||
590 | ||
591 | @node List Mapping | |
592 | @subsubsection List Mapping | |
593 | ||
594 | List processing is very convenient in Scheme because the process of | |
595 | iterating over the elements of a list can be highly abstracted. The | |
596 | procedures in this section are the most basic iterating procedures for | |
597 | lists. They take a procedure and one or more lists as arguments, and | |
598 | apply the procedure to each element of the list. They differ in their | |
599 | return value. | |
600 | ||
601 | @rnindex map | |
602 | @c begin (texi-doc-string "guile" "map") | |
603 | @deffn {Scheme Procedure} map proc arg1 arg2 @dots{} | |
604 | @deffnx {Scheme Procedure} map-in-order proc arg1 arg2 @dots{} | |
605 | @deffnx {C Function} scm_map (proc, arg1, args) | |
606 | Apply @var{proc} to each element of the list @var{arg1} (if only two | |
607 | arguments are given), or to the corresponding elements of the argument | |
608 | lists (if more than two arguments are given). The result(s) of the | |
609 | procedure applications are saved and returned in a list. For | |
610 | @code{map}, the order of procedure applications is not specified, | |
611 | @code{map-in-order} applies the procedure from left to right to the list | |
612 | elements. | |
613 | @end deffn | |
614 | ||
615 | @rnindex for-each | |
616 | @c begin (texi-doc-string "guile" "for-each") | |
617 | @deffn {Scheme Procedure} for-each proc arg1 arg2 @dots{} | |
618 | Like @code{map}, but the procedure is always applied from left to right, | |
619 | and the result(s) of the procedure applications are thrown away. The | |
620 | return value is not specified. | |
621 | @end deffn | |
622 | ||
623 | ||
624 | @node Vectors | |
625 | @subsection Vectors | |
626 | @tpindex Vectors | |
627 | ||
628 | Vectors are sequences of Scheme objects. Unlike lists, the length of a | |
629 | vector, once the vector is created, cannot be changed. The advantage of | |
630 | vectors over lists is that the time required to access one element of a vector | |
631 | given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number, | |
632 | is constant, whereas lists have an access time linear to the position of the | |
633 | accessed element in the list. | |
634 | ||
e6b226b9 MV |
635 | Vectors can contain any kind of Scheme object; it is even possible to |
636 | have different types of objects in the same vector. For vectors | |
637 | containing vectors, you may wish to use arrays, instead. Note, too, | |
52d28fc2 MV |
638 | that vectors are the special case of one dimensional non-uniform arrays |
639 | and that most array procedures operate happily on vectors | |
01e6d0ec MV |
640 | (@pxref{Arrays}). |
641 | ||
07d83abe MV |
642 | @menu |
643 | * Vector Syntax:: Read syntax for vectors. | |
644 | * Vector Creation:: Dynamic vector creation and validation. | |
645 | * Vector Accessors:: Accessing and modifying vector contents. | |
52d28fc2 | 646 | * Vector Accessing from C:: Ways to work with vectors from C. |
07d83abe MV |
647 | @end menu |
648 | ||
649 | ||
650 | @node Vector Syntax | |
651 | @subsubsection Read Syntax for Vectors | |
652 | ||
653 | Vectors can literally be entered in source code, just like strings, | |
654 | characters or some of the other data types. The read syntax for vectors | |
655 | is as follows: A sharp sign (@code{#}), followed by an opening | |
656 | parentheses, all elements of the vector in their respective read syntax, | |
657 | and finally a closing parentheses. The following are examples of the | |
658 | read syntax for vectors; where the first vector only contains numbers | |
659 | and the second three different object types: a string, a symbol and a | |
660 | number in hexadecimal notation. | |
661 | ||
662 | @lisp | |
663 | #(1 2 3) | |
664 | #("Hello" foo #xdeadbeef) | |
665 | @end lisp | |
666 | ||
52d28fc2 | 667 | Like lists, vectors have to be quoted: |
07d83abe MV |
668 | |
669 | @lisp | |
670 | '#(a b c) @result{} #(a b c) | |
671 | @end lisp | |
672 | ||
673 | @node Vector Creation | |
674 | @subsubsection Dynamic Vector Creation and Validation | |
675 | ||
676 | Instead of creating a vector implicitly by using the read syntax just | |
677 | described, you can create a vector dynamically by calling one of the | |
678 | @code{vector} and @code{list->vector} primitives with the list of Scheme | |
679 | values that you want to place into a vector. The size of the vector | |
680 | thus created is determined implicitly by the number of arguments given. | |
681 | ||
682 | @rnindex vector | |
683 | @rnindex list->vector | |
684 | @deffn {Scheme Procedure} vector . l | |
685 | @deffnx {Scheme Procedure} list->vector l | |
686 | @deffnx {C Function} scm_vector (l) | |
687 | Return a newly allocated vector composed of the | |
688 | given arguments. Analogous to @code{list}. | |
689 | ||
690 | @lisp | |
691 | (vector 'a 'b 'c) @result{} #(a b c) | |
692 | @end lisp | |
693 | @end deffn | |
694 | ||
07d83abe MV |
695 | The inverse operation is @code{vector->list}: |
696 | ||
697 | @rnindex vector->list | |
698 | @deffn {Scheme Procedure} vector->list v | |
699 | @deffnx {C Function} scm_vector_to_list (v) | |
700 | Return a newly allocated list composed of the elements of @var{v}. | |
701 | ||
702 | @lisp | |
703 | (vector->list '#(dah dah didah)) @result{} (dah dah didah) | |
704 | (list->vector '(dididit dah)) @result{} #(dididit dah) | |
705 | @end lisp | |
706 | @end deffn | |
707 | ||
708 | To allocate a vector with an explicitly specified size, use | |
709 | @code{make-vector}. With this primitive you can also specify an initial | |
710 | value for the vector elements (the same value for all elements, that | |
711 | is): | |
712 | ||
713 | @rnindex make-vector | |
61eed960 MV |
714 | @deffn {Scheme Procedure} make-vector len [fill] |
715 | @deffnx {C Function} scm_make_vector (len, fill) | |
716 | Return a newly allocated vector of @var{len} elements. If a | |
07d83abe MV |
717 | second argument is given, then each position is initialized to |
718 | @var{fill}. Otherwise the initial contents of each position is | |
719 | unspecified. | |
720 | @end deffn | |
721 | ||
61eed960 MV |
722 | @deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill) |
723 | Like @code{scm_make_vector}, but the length is given as a @code{size_t}. | |
724 | @end deftypefn | |
725 | ||
07d83abe MV |
726 | To check whether an arbitrary Scheme value @emph{is} a vector, use the |
727 | @code{vector?} primitive: | |
728 | ||
729 | @rnindex vector? | |
730 | @deffn {Scheme Procedure} vector? obj | |
731 | @deffnx {C Function} scm_vector_p (obj) | |
732 | Return @code{#t} if @var{obj} is a vector, otherwise return | |
733 | @code{#f}. | |
734 | @end deffn | |
735 | ||
61eed960 MV |
736 | @deftypefn {C Function} int scm_is_vector (SCM obj) |
737 | Return non-zero when @var{obj} is a vector, otherwise return | |
738 | @code{zero}. | |
739 | @end deftypefn | |
07d83abe MV |
740 | |
741 | @node Vector Accessors | |
742 | @subsubsection Accessing and Modifying Vector Contents | |
743 | ||
744 | @code{vector-length} and @code{vector-ref} return information about a | |
745 | given vector, respectively its size and the elements that are contained | |
746 | in the vector. | |
747 | ||
748 | @rnindex vector-length | |
749 | @deffn {Scheme Procedure} vector-length vector | |
750 | @deffnx {C Function} scm_vector_length vector | |
751 | Return the number of elements in @var{vector} as an exact integer. | |
752 | @end deffn | |
753 | ||
61eed960 MV |
754 | @deftypefn {C Function} size_t scm_c_vector_length (SCM v) |
755 | Return the number of elements in @var{vector} as a @code{size_t}. | |
756 | @end deftypefn | |
757 | ||
07d83abe MV |
758 | @rnindex vector-ref |
759 | @deffn {Scheme Procedure} vector-ref vector k | |
760 | @deffnx {C Function} scm_vector_ref vector k | |
761 | Return the contents of position @var{k} of @var{vector}. | |
762 | @var{k} must be a valid index of @var{vector}. | |
763 | @lisp | |
764 | (vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8 | |
765 | (vector-ref '#(1 1 2 3 5 8 13 21) | |
766 | (let ((i (round (* 2 (acos -1))))) | |
767 | (if (inexact? i) | |
768 | (inexact->exact i) | |
769 | i))) @result{} 13 | |
770 | @end lisp | |
771 | @end deffn | |
772 | ||
61eed960 | 773 | @deftypefn {C Function} SCM scm_c_vector_ref (SCM v, size_t k) |
52d28fc2 | 774 | Return the contents of position @var{k} (a @code{size_t}) of |
61eed960 MV |
775 | @var{vector}. |
776 | @end deftypefn | |
777 | ||
07d83abe MV |
778 | A vector created by one of the dynamic vector constructor procedures |
779 | (@pxref{Vector Creation}) can be modified using the following | |
780 | procedures. | |
781 | ||
782 | @emph{NOTE:} According to R5RS, it is an error to use any of these | |
783 | procedures on a literally read vector, because such vectors should be | |
784 | considered as constants. Currently, however, Guile does not detect this | |
785 | error. | |
786 | ||
787 | @rnindex vector-set! | |
788 | @deffn {Scheme Procedure} vector-set! vector k obj | |
789 | @deffnx {C Function} scm_vector_set_x vector k obj | |
790 | Store @var{obj} in position @var{k} of @var{vector}. | |
791 | @var{k} must be a valid index of @var{vector}. | |
792 | The value returned by @samp{vector-set!} is unspecified. | |
793 | @lisp | |
794 | (let ((vec (vector 0 '(2 2 2 2) "Anna"))) | |
795 | (vector-set! vec 1 '("Sue" "Sue")) | |
796 | vec) @result{} #(0 ("Sue" "Sue") "Anna") | |
797 | @end lisp | |
798 | @end deffn | |
799 | ||
de26705f | 800 | @deftypefn {C Function} void scm_c_vector_set_x (SCM v, size_t k, SCM obj) |
61eed960 MV |
801 | Store @var{obj} in position @var{k} (a @code{size_t}) of @var{v}. |
802 | @end deftypefn | |
803 | ||
07d83abe MV |
804 | @rnindex vector-fill! |
805 | @deffn {Scheme Procedure} vector-fill! v fill | |
806 | @deffnx {C Function} scm_vector_fill_x (v, fill) | |
807 | Store @var{fill} in every position of @var{vector}. The value | |
808 | returned by @code{vector-fill!} is unspecified. | |
809 | @end deffn | |
810 | ||
811 | @deffn {Scheme Procedure} vector-move-left! vec1 start1 end1 vec2 start2 | |
812 | @deffnx {C Function} scm_vector_move_left_x (vec1, start1, end1, vec2, start2) | |
813 | Copy elements from @var{vec1}, positions @var{start1} to @var{end1}, | |
814 | to @var{vec2} starting at position @var{start2}. @var{start1} and | |
815 | @var{start2} are inclusive indices; @var{end1} is exclusive. | |
816 | ||
817 | @code{vector-move-left!} copies elements in leftmost order. | |
818 | Therefore, in the case where @var{vec1} and @var{vec2} refer to the | |
819 | same vector, @code{vector-move-left!} is usually appropriate when | |
820 | @var{start1} is greater than @var{start2}. | |
821 | @end deffn | |
822 | ||
823 | @deffn {Scheme Procedure} vector-move-right! vec1 start1 end1 vec2 start2 | |
824 | @deffnx {C Function} scm_vector_move_right_x (vec1, start1, end1, vec2, start2) | |
825 | Copy elements from @var{vec1}, positions @var{start1} to @var{end1}, | |
826 | to @var{vec2} starting at position @var{start2}. @var{start1} and | |
827 | @var{start2} are inclusive indices; @var{end1} is exclusive. | |
828 | ||
829 | @code{vector-move-right!} copies elements in rightmost order. | |
830 | Therefore, in the case where @var{vec1} and @var{vec2} refer to the | |
831 | same vector, @code{vector-move-right!} is usually appropriate when | |
832 | @var{start1} is less than @var{start2}. | |
833 | @end deffn | |
834 | ||
52d28fc2 MV |
835 | @node Vector Accessing from C |
836 | @subsubsection Vector Accessing from C | |
01e6d0ec | 837 | |
52d28fc2 MV |
838 | A vector can be read and modified from C with the functions |
839 | @code{scm_c_vector_ref} and @code{scm_c_vector_set_x}, for example. In | |
840 | addition to these functions, there are two more ways to access vectors | |
841 | from C that might be more efficient in certain situations: you can | |
842 | restrict yourself to @dfn{simple vectors} and then use the very fast | |
843 | @emph{simple vector macros}; or you can use the very general framework | |
844 | for accessing all kinds of arrays (@pxref{Accessing Arrays from C}), | |
845 | which is more verbose, but can deal efficiently with all kinds of | |
846 | vectors (and arrays). | |
847 | ||
848 | @deftypefn {C Function} int scm_is_simple_vector (SCM obj) | |
849 | Return non-zero if @var{obj} is a simple vector, else return zero. A | |
850 | simple vector is a vector that can be used with the @code{SCM_SIMPLE_*} | |
851 | macros below. | |
01e6d0ec | 852 | |
52d28fc2 MV |
853 | The following functions are guaranteed to return simple vectors: |
854 | @code{scm_make_vector}, @code{scm_c_make_vector}, @code{scm_vector}, | |
855 | @code{scm_list_to_vector}. | |
01e6d0ec MV |
856 | @end deftypefn |
857 | ||
52d28fc2 MV |
858 | @deftypefn {C Macro} size_t SCM_SIMPLE_VECTOR_LENGTH (SCM vec) |
859 | Evaluates to the length of the simple vector @var{vec}. No type | |
860 | checking is done. | |
01e6d0ec MV |
861 | @end deftypefn |
862 | ||
52d28fc2 MV |
863 | @deftypefn {C Macro} SCM SCM_SIMPLE_VECTOR_REF (SCM vec, size_t idx) |
864 | Evaluates to the element at position @var{idx} in the simple vector | |
865 | @var{vec}. No type or range checking is done. | |
866 | @end deftypefn | |
01e6d0ec | 867 | |
52d28fc2 MV |
868 | @deftypefn {C Macro} void SCM_SIMPLE_VECTOR_SET_X (SCM vec, size_t idx, SCM val) |
869 | Sets the element at position @var{idx} in the simple vector | |
870 | @var{vec} to @var{val}. No type or range checking is done. | |
01e6d0ec MV |
871 | @end deftypefn |
872 | ||
52d28fc2 MV |
873 | @deftypefn {C Function} const SCM *scm_vector_elements (SCM vec, scm_t_array_handle *handle, size_t *lenp, ssize_t *incp) |
874 |