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