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