1Stdlib.List(3)                   OCaml library                  Stdlib.List(3)
2
3
4

NAME

6       Stdlib.List - no description
7

Module

9       Module   Stdlib.List
10

Documentation

12       Module List
13        : (module Stdlib__list)
14
15
16
17
18
19
20
21       type 'a t = 'a list =
22        | []
23        | :: of 'a * 'a list
24
25
26       An alias for the type of lists.
27
28
29
30       val length : 'a list -> int
31
32       Return the length (number of elements) of the given list.
33
34
35
36       val compare_lengths : 'a list -> 'b list -> int
37
38       Compare  the lengths of two lists.  compare_lengths l1 l2 is equivalent
39       to compare (length l1) (length l2) , except that the computation  stops
40       after itering on the shortest list.
41
42
43       Since 4.05.0
44
45
46
47       val compare_length_with : 'a list -> int -> int
48
49       Compare the length of a list to an integer.  compare_length_with l n is
50       equivalent to compare (length l) n , except that the computation  stops
51       after at most n iterations on the list.
52
53
54       Since 4.05.0
55
56
57
58       val cons : 'a -> 'a list -> 'a list
59
60
61       cons x xs is x :: xs
62
63
64
65       Since 4.03.0
66
67
68
69       val hd : 'a list -> 'a
70
71       Return  the  first element of the given list. Raise Failure "hd" if the
72       list is empty.
73
74
75
76       val tl : 'a list -> 'a list
77
78       Return the given list without its first element. Raise Failure "tl"  if
79       the list is empty.
80
81
82
83       val nth : 'a list -> int -> 'a
84
85       Return the n -th element of the given list.  The first element (head of
86       the list) is at position 0.  Raise Failure "nth" if  the  list  is  too
87       short.  Raise Invalid_argument "List.nth" if n is negative.
88
89
90
91       val nth_opt : 'a list -> int -> 'a option
92
93       Return the n -th element of the given list.  The first element (head of
94       the list) is at position 0.  Return None if  the  list  is  too  short.
95       Raise Invalid_argument "List.nth" if n is negative.
96
97
98       Since 4.05
99
100
101
102       val rev : 'a list -> 'a list
103
104       List reversal.
105
106
107
108       val init : int -> (int -> 'a) -> 'a list
109
110
111       List.init  len  f  is  [f  0;  f 1; ...; f (len-1)] , evaluated left to
112       right.
113
114
115       Since 4.06.0
116
117
118       Raises Invalid_argument if len < 0.
119
120
121
122       val append : 'a list -> 'a list -> 'a list
123
124       Concatenate two lists.  Same as the infix operator @ .  Not tail-recur‐
125       sive (length of the first argument).
126
127
128
129       val rev_append : 'a list -> 'a list -> 'a list
130
131
132       List.rev_append  l1 l2 reverses l1 and concatenates it to l2 .  This is
133       equivalent to List.rev l1 @ l2 , but rev_append is  tail-recursive  and
134       more efficient.
135
136
137
138       val concat : 'a list list -> 'a list
139
140       Concatenate a list of lists.  The elements of the argument are all con‐
141       catenated together (in  the  same  order)  to  give  the  result.   Not
142       tail-recursive  (length  of  the  argument  +  length  of  the  longest
143       sub-list).
144
145
146
147       val flatten : 'a list list -> 'a list
148
149       An alias for concat .
150
151
152
153
154   Iterators
155       val iter : ('a -> unit) -> 'a list -> unit
156
157
158       List.iter f [a1; ...; an] applies function f in turn to a1; ...;  an  .
159       It is equivalent to begin f a1; f a2; ...; f an; () end .
160
161
162
163       val iteri : (int -> 'a -> unit) -> 'a list -> unit
164
165       Same  as  List.iter  ,  but the function is applied to the index of the
166       element as first argument (counting from 0), and the element itself  as
167       second argument.
168
169
170       Since 4.00.0
171
172
173
174       val map : ('a -> 'b) -> 'a list -> 'b list
175
176
177       List.map f [a1; ...; an] applies function f to a1, ..., an , and builds
178       the list [f a1; ...; f an] with  the  results  returned  by  f  .   Not
179       tail-recursive.
180
181
182
183       val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
184
185       Same as List.map , but the function is applied to the index of the ele‐
186       ment as first argument (counting from 0), and  the  element  itself  as
187       second argument.  Not tail-recursive.
188
189
190       Since 4.00.0
191
192
193
194       val rev_map : ('a -> 'b) -> 'a list -> 'b list
195
196
197       List.rev_map  f  l  gives the same result as List.rev ( List.map f l) ,
198       but is tail-recursive and more efficient.
199
200
201
202       val filter_map : ('a -> 'b option) -> 'a list -> 'b list
203
204
205       filter_map f l applies f to every element of l , filters out  the  None
206       elements and returns the list of the arguments of the Some elements.
207
208
209       Since 4.08.0
210
211
212
213       val concat_map : ('a -> 'b list) -> 'a list -> 'b list
214
215
216       List.concat_map  f  l gives the same result as List.concat ( List.map f
217       l) . Tail-recursive.
218
219
220       Since 4.10.0
221
222
223
224       val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
225
226
227       List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn .
228
229
230
231       val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
232
233
234       List.fold_right f [a1; ...; an] b is f a1 (f a2 (... (f an b) ...))   .
235       Not tail-recursive.
236
237
238
239
240   Iterators on two lists
241       val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
242
243
244       List.iter2  f [a1; ...; an] [b1; ...; bn] calls in turn f a1 b1; ...; f
245       an bn .  Raise Invalid_argument if the two lists are determined to have
246       different lengths.
247
248
249
250       val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
251
252
253       List.map2  f  [a1;  ...; an] [b1; ...; bn] is [f a1 b1; ...; f an bn] .
254       Raise Invalid_argument if the two lists are determined to have  differ‐
255       ent lengths.  Not tail-recursive.
256
257
258
259       val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
260
261
262       List.rev_map2  f  l1 l2 gives the same result as List.rev ( List.map2 f
263       l1 l2) , but is tail-recursive and more efficient.
264
265
266
267       val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list  ->
268       'a
269
270
271       List.fold_left2  f  a  [b1; ...; bn] [c1; ...; cn] is f (... (f (f a b1
272       c1) b2 c2) ...) bn cn .  Raise Invalid_argument if the  two  lists  are
273       determined to have different lengths.
274
275
276
277       val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c ->
278       'c
279
280
281       List.fold_right2 f [a1; ...; an] [b1; ...; bn] c is f a1 b1  (f  a2  b2
282       (...  (f an bn c) ...))  .  Raise Invalid_argument if the two lists are
283       determined to have different lengths.  Not tail-recursive.
284
285
286
287
288   List scanning
289       val for_all : ('a -> bool) -> 'a list -> bool
290
291
292       for_all p [a1; ...; an] checks if all elements of the list satisfy  the
293       predicate p . That is, it returns (p a1) && (p a2) && ... && (p an) .
294
295
296
297       val exists : ('a -> bool) -> 'a list -> bool
298
299
300       exists  p [a1; ...; an] checks if at least one element of the list sat‐
301       isfies the predicate p . That is, it returns (p a1) || (p a2) || ... ||
302       (p an) .
303
304
305
306       val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
307
308       Same  as  List.for_all  ,  but  for  a  two-argument  predicate.  Raise
309       Invalid_argument if the two lists  are  determined  to  have  different
310       lengths.
311
312
313
314       val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
315
316       Same  as  List.exists  ,  but  for  a  two-argument  predicate.   Raise
317       Invalid_argument if the two lists  are  determined  to  have  different
318       lengths.
319
320
321
322       val mem : 'a -> 'a list -> bool
323
324
325       mem a l is true if and only if a is equal to an element of l .
326
327
328
329       val memq : 'a -> 'a list -> bool
330
331       Same  as  List.mem  ,  but uses physical equality instead of structural
332       equality to compare list elements.
333
334
335
336
337   List searching
338       val find : ('a -> bool) -> 'a list -> 'a
339
340
341       find p l returns the first element of the list  l  that  satisfies  the
342       predicate p .  Raise Not_found if there is no value that satisfies p in
343       the list l .
344
345
346
347       val find_opt : ('a -> bool) -> 'a list -> 'a option
348
349
350       find_opt p l returns the first element of the list l that satisfies the
351       predicate p , or None if there is no value that satisfies p in the list
352       l .
353
354
355       Since 4.05
356
357
358
359       val find_map : ('a -> 'b option) -> 'a list -> 'b option
360
361
362       find_map f l applies f to the elements of l in order, and  returns  the
363       first result of the form Some v , or None if none exist.
364
365
366       Since 4.10.0
367
368
369
370       val filter : ('a -> bool) -> 'a list -> 'a list
371
372
373       filter  p  l  returns  all  the elements of the list l that satisfy the
374       predicate p .  The order of the elements in  the  input  list  is  pre‐
375       served.
376
377
378
379       val find_all : ('a -> bool) -> 'a list -> 'a list
380
381
382       find_all is another name for List.filter .
383
384
385
386       val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
387
388
389       partition  p  l returns a pair of lists (l1, l2) , where l1 is the list
390       of all the elements of l that satisfy the predicate p , and l2  is  the
391       list of all the elements of l that do not satisfy p .  The order of the
392       elements in the input list is preserved.
393
394
395
396
397   Association lists
398       val assoc : 'a -> ('a * 'b) list -> 'b
399
400
401       assoc a l returns the value associated with key a in the list of  pairs
402       l  .  That  is, assoc a [ ...; (a,b); ...] = b if (a,b) is the leftmost
403       binding of a in list l .  Raise Not_found if there is no value  associ‐
404       ated with a in the list l .
405
406
407
408       val assoc_opt : 'a -> ('a * 'b) list -> 'b option
409
410
411       assoc_opt  a  l  returns the value associated with key a in the list of
412       pairs l . That is, assoc_opt a [ ...; (a,b); ...] = b if (a,b)  is  the
413       leftmost  binding  of  a in list l .  Returns None if there is no value
414       associated with a in the list l .
415
416
417       Since 4.05
418
419
420
421       val assq : 'a -> ('a * 'b) list -> 'b
422
423       Same as List.assoc , but uses physical equality instead  of  structural
424       equality to compare keys.
425
426
427
428       val assq_opt : 'a -> ('a * 'b) list -> 'b option
429
430       Same  as  List.assoc_opt , but uses physical equality instead of struc‐
431       tural equality to compare keys.
432
433
434       Since 4.05
435
436
437
438       val mem_assoc : 'a -> ('a * 'b) list -> bool
439
440       Same as List.assoc , but simply return true if a  binding  exists,  and
441       false if no bindings exist for the given key.
442
443
444
445       val mem_assq : 'a -> ('a * 'b) list -> bool
446
447       Same  as  List.mem_assoc , but uses physical equality instead of struc‐
448       tural equality to compare keys.
449
450
451
452       val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
453
454
455       remove_assoc a l returns the list of pairs l  without  the  first  pair
456       with key a , if any.  Not tail-recursive.
457
458
459
460       val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
461
462       Same  as  List.remove_assoc  ,  but  uses  physical equality instead of
463       structural equality to compare keys.  Not tail-recursive.
464
465
466
467
468   Lists of pairs
469       val split : ('a * 'b) list -> 'a list * 'b list
470
471       Transform a list of pairs into a pair of lists:  split  [(a1,b1);  ...;
472       (an,bn)] is ([a1; ...; an], [b1; ...; bn]) .  Not tail-recursive.
473
474
475
476       val combine : 'a list -> 'b list -> ('a * 'b) list
477
478       Transform  a  pair of lists into a list of pairs: combine [a1; ...; an]
479       [b1; ...; bn] is [(a1,b1); ...; (an,bn)] .  Raise  Invalid_argument  if
480       the two lists have different lengths.  Not tail-recursive.
481
482
483
484
485   Sorting
486       val sort : ('a -> 'a -> int) -> 'a list -> 'a list
487
488       Sort  a  list  in  increasing order according to a comparison function.
489       The comparison function must return  0  if  its  arguments  compare  as
490       equal, a positive integer if the first is greater, and a negative inte‐
491       ger if the first is smaller (see Array.sort for a  complete  specifica‐
492       tion).   For  example,  compare is a suitable comparison function.  The
493       resulting list is sorted in increasing order.  List.sort is  guaranteed
494       to  run  in  constant heap space (in addition to the size of the result
495       list) and logarithmic stack space.
496
497       The current implementation uses Merge Sort. It runs  in  constant  heap
498       space and logarithmic stack space.
499
500
501
502       val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
503
504       Same  as List.sort , but the sorting algorithm is guaranteed to be sta‐
505       ble (i.e. elements that compare equal are kept in their original order)
506       .
507
508       The  current  implementation  uses Merge Sort. It runs in constant heap
509       space and logarithmic stack space.
510
511
512
513       val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
514
515       Same as List.sort or List.stable_sort , whichever is faster on  typical
516       input.
517
518
519
520       val sort_uniq : ('a -> 'a -> int) -> 'a list -> 'a list
521
522       Same as List.sort , but also remove duplicates.
523
524
525       Since 4.02.0
526
527
528
529       val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
530
531       Merge  two  lists:  Assuming that l1 and l2 are sorted according to the
532       comparison function cmp , merge cmp l1 l2 will  return  a  sorted  list
533       containing all the elements of l1 and l2 .  If several elements compare
534       equal, the elements of l1 will be before the  elements  of  l2  .   Not
535       tail-recursive (sum of the lengths of the arguments).
536
537
538
539
540   Iterators
541       val to_seq : 'a list -> 'a Seq.t
542
543       Iterate on the list
544
545
546       Since 4.07
547
548
549
550       val of_seq : 'a Seq.t -> 'a list
551
552       Create a list from the iterator
553
554
555       Since 4.07
556
557
558
559
560
561OCamldoc                          2020-02-27                    Stdlib.List(3)
Impressum