One more incremental step towards the new array API.
[bpt/guile.git] / doc / ref / api-compound.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
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
11This chapter describes Guile's compound data types. By @dfn{compound}
12we mean that the primary purpose of these data types is to act as
13containers for other kinds of data (including other compound objects).
14For instance, a (non-uniform) vector with length 5 is a container that
15can hold five arbitrary Scheme objects.
16
17The various kinds of container object differ from each other in how
18their memory is allocated, how they are indexed, and how particular
19values 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
41Pairs are used to combine two Scheme objects into one compound object.
42Hence the name: A pair stores a pair of objects.
43
44The data type @dfn{pair} is extremely important in Scheme, just like in
45any other Lisp dialect. The reason is that pairs are not only used to
46make two values available as one object, but that pairs are used for
47constructing lists of values. Because lists are so important in Scheme,
48they are described in a section of their own (@pxref{Lists}).
49
50Pairs can literally get entered in source code or at the REPL, in the
51so-called @dfn{dotted list} syntax. This syntax consists of an opening
52parentheses, the first element of the pair, a dot, the second element
53and a closing parentheses. The following example shows how a pair
54consisting of the two numbers 1 and 2, and a pair containing the symbols
55@code{foo} and @code{bar} can be entered. It is very important to write
56the whitespace before and after the dot, because otherwise the Scheme
57parser would not be able to figure out where to split the tokens.
58
59@lisp
60(1 . 2)
61(foo . bar)
62@end lisp
63
64But beware, if you want to try out these examples, you have to
65@dfn{quote} the expressions. More information about quotation is
66available in the section (REFFIXME). The correct way to try these
67examples 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
78A new pair is made by calling the procedure @code{cons} with two
79arguments. Then the argument values are stored into a newly allocated
80pair, and the pair is returned. The name @code{cons} stands for
81"construct". Use the procedure @code{pair?} to test whether a
82given Scheme object is a pair or not.
83
84@rnindex cons
85@deffn {Scheme Procedure} cons x y
86@deffnx {C Function} scm_cons (x, y)
87Return a newly allocated pair whose car is @var{x} and whose
88cdr is @var{y}. The pair is guaranteed to be different (in the
89sense of @code{eq?}) from every previously existing object.
90@end deffn
91
92@rnindex pair?
93@deffn {Scheme Procedure} pair? x
94@deffnx {C Function} scm_pair_p (x)
95Return @code{#t} if @var{x} is a pair; otherwise return
96@code{#f}.
97@end deffn
98
7f4c83e3
MV
99@deftypefn {C Function} int scm_is_pair (SCM x)
100Return 1 when @var{x} is a pair; otherwise return 0.
101@end deftypefn
102
07d83abe
MV
103The two parts of a pair are traditionally called @dfn{car} and
104@dfn{cdr}. They can be retrieved with procedures of the same name
105(@code{car} and @code{cdr}), and can be modified with the procedures
106@code{set-car!} and @code{set-cdr!}. Since a very common operation in
107Scheme programs is to access the car of a pair, or the car of the cdr of
108a pair, etc., the procedures called @code{caar}, @code{cadr} and so on
109are 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
117Return 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
176These procedures are compositions of @code{car} and @code{cdr}, where
177for 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)
187Stores @var{value} in the car field of @var{pair}. The value returned
188by @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)
194Stores @var{value} in the cdr field of @var{pair}. The value returned
195by @code{set-cdr!} is unspecified.
196@end deffn
197
198
199@node Lists
200@subsection Lists
201@tpindex Lists
202
203A very important data type in Scheme---as well as in all other Lisp
204dialects---is the data type @dfn{list}.@footnote{Strictly speaking,
205Scheme 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
207of pairs which looks like a list.}
208
209This is the short definition of what a list is:
210
211@itemize @bullet
212@item
213Either the empty list @code{()},
214
215@item
216or 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
241The syntax for lists is an opening parentheses, then all the elements of
242the list (separated by whitespace) and finally a closing
243parentheses.@footnote{Note that there is no separation character between
244the 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
252The last example needs a bit more explanation. A list with no elements,
253called the @dfn{empty list}, is special in some ways. It is used for
254terminating lists by storing it into the cdr of the last pair that makes
255up a list. An example will clear that up:
256
257@lisp
258(car '(1))
259@result{}
2601
261(cdr '(1))
262@result{}
263()
264@end lisp
265
266This example also shows that lists have to be quoted (REFFIXME) when
267written, because they would otherwise be mistakingly taken as procedure
268applications (@pxref{Simple Invocation}).
269
270
271@node List Predicates
272@subsubsection List Predicates
273
274Often it is useful to test whether a given Scheme object is a list or
275not. List-processing procedures could use this information to test
276whether their input is valid, or they could do different things
277depending on the datatype of their arguments.
278
279@rnindex list?
280@deffn {Scheme Procedure} list? x
281@deffnx {C Function} scm_list_p (x)
282Return @code{#t} iff @var{x} is a proper list, else @code{#f}.
283@end deffn
284
285The predicate @code{null?} is often used in list-processing code to
286tell whether a given list has run out of elements. That is, a loop
287somehow 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)
293Return @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)
297Return 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
304This 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
307the 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
321Return a new list containing elements @var{elem1} to @var{elemN}.
322
323@code{scm_list_n} takes a variable number of arguments, terminated by
324the special @code{SCM_UNDEFINED}. That final @code{SCM_UNDEFINED} is
325not included in the list. None of @var{elem1} to @var{elemN} can
326themselves be @code{SCM_UNDEFINED}, or @code{scm_list_n} will
327terminate 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{}
336Like @code{list}, but the last arg provides the tail of the
337constructed list, returning @code{(cons @var{arg1} (cons
338@var{arg2} (cons @dots{} @var{argn})))}. Requires at least one
339argument. If given one argument, that argument is returned as
340result. This function is called @code{list*} in some other
341Schemes and in Common LISP.
342@end deffn
343
344@deffn {Scheme Procedure} list-copy lst
345@deffnx {C Function} scm_list_copy (lst)
346Return a (newly-created) copy of @var{lst}.
347@end deffn
348
349@deffn {Scheme Procedure} make-list n [init]
350Create a list containing of @var{n} elements, where each element is
351initialized to @var{init}. @var{init} defaults to the empty list
352@code{()} if not given.
353@end deffn
354
355Note that @code{list-copy} only makes a copy of the pairs which make up
356the spine of the lists. The list elements are not copied, which means
357that modifying the elements of the new list also modifies the elements
358of 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
360list. If you also need to copy the list elements (making a deep copy),
361use the procedure @code{copy-tree} (@pxref{Copying}).
362
363@node List Selection
364@subsubsection List Selection
365
366These procedures are used to get some information about a list, or to
367retrieve one or more elements of a list.
368
369@rnindex length
370@deffn {Scheme Procedure} length lst
371@deffnx {C Function} scm_length (lst)
372Return 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 377Return 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)
384Return 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)
391Return the "tail" of @var{lst} beginning with its @var{k}th element.
392The 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
395think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,
396or 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)
401Copy the first @var{k} elements from @var{lst} into a new list, and
402return 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
409lists in order to form a new list. @code{reverse} and @code{reverse!}
410return lists with the same elements as their arguments, but in reverse
411order. The procedure variants with an @code{!} directly modify the
412pairs which form the list, whereas the other procedures create new
413pairs. This is why you should be careful when using the side-effecting
414variants.
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)
421Return 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
430The last argument @var{lstN} may actually be any object; an improper
431list 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
439structure with the final @var{lstN}. @code{append!} modifies the
440given lists to form its return.
441
442For @code{scm_append} and @code{scm_append_x}, @var{lstlst} is a list
443of the list operands @var{lst1} @dots{} @var{lstN}. That @var{lstlst}
444itself 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)
452Return 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
457For @code{reverse!}, the optional @var{newtail} is appended to to the
458result. @var{newtail} isn't reversed, it simply becomes the list
459tail. For @code{scm_reverse_x}, the @var{newtail} parameter is
460mandatory, 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
466The following procedures modify an existing list, either by changing
467elements 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)
471Set 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)
476Set 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)
481Return 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)
489Return 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)
497Return 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}
500against @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)
509These procedures are destructive versions of @code{delq}, @code{delv}
510and @code{delete}: they modify the pointers in the existing @var{lst}
511rather than creating a new list. Caveat evaluator: Like other
512destructive 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)
519Like @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)
526Like @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)
533Like @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
540Return a list containing all elements from @var{lst} which satisfy the
541predicate @var{pred}. The elements in the result list have the same
542order as in @var{lst}. The order in which @var{pred} is applied to
543the 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
551The following procedures search lists for particular elements. They use
552different comparison predicates for comparing list elements with the
553object to be searched. When they fail, they return @code{#f}, otherwise
554they return the sublist whose car is equal to the search object, where
555equality 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)
560Return the first sublist of @var{lst} whose car is @code{eq?}
561to @var{x} where the sublists of @var{lst} are the non-empty
562lists 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
564occur in @var{lst}, then @code{#f} (not the empty list) is
565returned.
566@end deffn
567
568@rnindex memv
569@deffn {Scheme Procedure} memv x lst
570@deffnx {C Function} scm_memv (x, lst)
571Return the first sublist of @var{lst} whose car is @code{eqv?}
572to @var{x} where the sublists of @var{lst} are the non-empty
573lists 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
575occur in @var{lst}, then @code{#f} (not the empty list) is
576returned.
577@end deffn
578
579@rnindex member
580@deffn {Scheme Procedure} member x lst
581@deffnx {C Function} scm_member (x, lst)
582Return the first sublist of @var{lst} whose car is
583@code{equal?} to @var{x} where the sublists of @var{lst} are
584the 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
587empty list) is returned.
588@end deffn
589
590
591@node List Mapping
592@subsubsection List Mapping
593
594List processing is very convenient in Scheme because the process of
595iterating over the elements of a list can be highly abstracted. The
596procedures in this section are the most basic iterating procedures for
597lists. They take a procedure and one or more lists as arguments, and
598apply the procedure to each element of the list. They differ in their
599return 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)
606Apply @var{proc} to each element of the list @var{arg1} (if only two
607arguments are given), or to the corresponding elements of the argument
608lists (if more than two arguments are given). The result(s) of the
609procedure 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
612elements.
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{}
618Like @code{map}, but the procedure is always applied from left to right,
619and the result(s) of the procedure applications are thrown away. The
620return value is not specified.
621@end deffn
622
623
624@node Vectors
625@subsection Vectors
626@tpindex Vectors
627
628Vectors are sequences of Scheme objects. Unlike lists, the length of a
629vector, once the vector is created, cannot be changed. The advantage of
630vectors over lists is that the time required to access one element of a vector
631given its @dfn{position} (synonymous with @dfn{index}), a zero-origin number,
632is constant, whereas lists have an access time linear to the position of the
633accessed element in the list.
634
e6b226b9
MV
635Vectors can contain any kind of Scheme object; it is even possible to
636have different types of objects in the same vector. For vectors
637containing vectors, you may wish to use arrays, instead. Note, too,
52d28fc2
MV
638that vectors are the special case of one dimensional non-uniform arrays
639and 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
653Vectors can literally be entered in source code, just like strings,
654characters or some of the other data types. The read syntax for vectors
655is as follows: A sharp sign (@code{#}), followed by an opening
656parentheses, all elements of the vector in their respective read syntax,
657and finally a closing parentheses. The following are examples of the
658read syntax for vectors; where the first vector only contains numbers
659and the second three different object types: a string, a symbol and a
660number in hexadecimal notation.
661
662@lisp
663#(1 2 3)
664#("Hello" foo #xdeadbeef)
665@end lisp
666
52d28fc2 667Like 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
676Instead of creating a vector implicitly by using the read syntax just
677described, you can create a vector dynamically by calling one of the
678@code{vector} and @code{list->vector} primitives with the list of Scheme
679values that you want to place into a vector. The size of the vector
680thus 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)
687Return a newly allocated vector composed of the
688given 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
695The 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)
700Return 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
708To allocate a vector with an explicitly specified size, use
709@code{make-vector}. With this primitive you can also specify an initial
710value for the vector elements (the same value for all elements, that
711is):
712
713@rnindex make-vector
61eed960
MV
714@deffn {Scheme Procedure} make-vector len [fill]
715@deffnx {C Function} scm_make_vector (len, fill)
716Return a newly allocated vector of @var{len} elements. If a
07d83abe
MV
717second argument is given, then each position is initialized to
718@var{fill}. Otherwise the initial contents of each position is
719unspecified.
720@end deffn
721
61eed960
MV
722@deftypefn {C Function} SCM scm_c_make_vector (size_t k, SCM fill)
723Like @code{scm_make_vector}, but the length is given as a @code{size_t}.
724@end deftypefn
725
07d83abe
MV
726To 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)
732Return @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)
737Return 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
745given vector, respectively its size and the elements that are contained
746in the vector.
747
748@rnindex vector-length
749@deffn {Scheme Procedure} vector-length vector
750@deffnx {C Function} scm_vector_length vector
751Return 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)
755Return 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
761Return 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 774Return the contents of position @var{k} (a @code{size_t}) of
61eed960
MV
775@var{vector}.
776@end deftypefn
777
07d83abe
MV
778A vector created by one of the dynamic vector constructor procedures
779(@pxref{Vector Creation}) can be modified using the following
780procedures.
781
782@emph{NOTE:} According to R5RS, it is an error to use any of these
783procedures on a literally read vector, because such vectors should be
784considered as constants. Currently, however, Guile does not detect this
785error.
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
790Store @var{obj} in position @var{k} of @var{vector}.
791@var{k} must be a valid index of @var{vector}.
792The 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
801Store @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)
807Store @var{fill} in every position of @var{vector}. The value
808returned 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)
813Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
814to @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.
818Therefore, in the case where @var{vec1} and @var{vec2} refer to the
819same 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)
825Copy elements from @var{vec1}, positions @var{start1} to @var{end1},
826to @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.
830Therefore, in the case where @var{vec1} and @var{vec2} refer to the
831same 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
838A 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
840addition to these functions, there are two more ways to access vectors
841from C that might be more efficient in certain situations: you can
842restrict yourself to @dfn{simple vectors} and then use the very fast
843@emph{simple vector macros}; or you can use the very general framework
844for accessing all kinds of arrays (@pxref{Accessing Arrays from C}),
845which is more verbose, but can deal efficiently with all kinds of
846vectors (and arrays).
847
848@deftypefn {C Function} int scm_is_simple_vector (SCM obj)
849Return non-zero if @var{obj} is a simple vector, else return zero. A
850simple vector is a vector that can be used with the @code{SCM_SIMPLE_*}
851macros below.
01e6d0ec 852
52d28fc2
MV
853The 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)
859Evaluates to the length of the simple vector @var{vec}. No type
860checking is done.
01e6d0ec
MV
861@end deftypefn
862
52d28fc2
MV
863@deftypefn {C Macro} SCM SCM_SIMPLE_VECTOR_REF (SCM vec, size_t idx)
864Evaluates 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)
869Sets 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