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