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