1lists(3) Erlang Module Definition lists(3)
2
3
4
6 lists - List processing functions.
7
9 This module contains functions for list processing.
10
11 Unless otherwise stated, all functions assume that position numbering
12 starts at 1. That is, the first element of a list is at position 1.
13
14 Two terms T1 and T2 compare equal if T1 == T2 evaluates to true. They
15 match if T1 =:= T2 evaluates to true.
16
17 Whenever an ordering function F is expected as argument, it is assumed
18 that the following properties hold of F for all x, y, and z:
19
20 * If x F y and y F x, then x = y (F is antisymmetric).
21
22 * If x F y and y F z, then x F z (F is transitive).
23
24 * x F y or y F x (F is total).
25
26 An example of a typical ordering function is less than or equal to:
27 =</2.
28
30 all(Pred, List) -> boolean()
31
32 Types:
33
34 Pred = fun((Elem :: T) -> boolean())
35 List = [T]
36 T = term()
37
38 Returns true if Pred(Elem) returns true for all elements Elem in
39 List, otherwise false. The Pred function must return a boolean.
40
41 any(Pred, List) -> boolean()
42
43 Types:
44
45 Pred = fun((Elem :: T) -> boolean())
46 List = [T]
47 T = term()
48
49 Returns true if Pred(Elem) returns true for at least one element
50 Elem in List. The Pred function must return a boolean.
51
52 append(ListOfLists) -> List1
53
54 Types:
55
56 ListOfLists = [List]
57 List = List1 = [T]
58 T = term()
59
60 Returns a list in which all the sublists of ListOfLists have
61 been appended.
62
63 Example:
64
65 > lists:append([[1, 2, 3], [a, b], [4, 5, 6]]).
66 [1,2,3,a,b,4,5,6]
67
68 append(List1, List2) -> List3
69
70 Types:
71
72 List1 = List2 = List3 = [T]
73 T = term()
74
75 Returns a new list List3, which is made from the elements of
76 List1 followed by the elements of List2.
77
78 Example:
79
80 > lists:append("abc", "def").
81 "abcdef"
82
83 lists:append(A, B) is equivalent to A ++ B.
84
85 concat(Things) -> string()
86
87 Types:
88
89 Things = [Thing]
90 Thing = atom() | integer() | float() | string()
91
92 Concatenates the text representation of the elements of Things.
93 The elements of Things can be atoms, integers, floats, or
94 strings.
95
96 Example:
97
98 > lists:concat([doc, '/', file, '.', 3]).
99 "doc/file.3"
100
101 delete(Elem, List1) -> List2
102
103 Types:
104
105 Elem = T
106 List1 = List2 = [T]
107 T = term()
108
109 Returns a copy of List1 where the first element matching Elem is
110 deleted, if there is such an element.
111
112 droplast(List) -> InitList
113
114 Types:
115
116 List = [T, ...]
117 InitList = [T]
118 T = term()
119
120 Drops the last element of a List. The list is to be non-empty,
121 otherwise the function crashes with a function_clause.
122
123 dropwhile(Pred, List1) -> List2
124
125 Types:
126
127 Pred = fun((Elem :: T) -> boolean())
128 List1 = List2 = [T]
129 T = term()
130
131 Drops elements Elem from List1 while Pred(Elem) returns true and
132 returns the remaining list. The Pred function must return a
133 boolean.
134
135 duplicate(N, Elem) -> List
136
137 Types:
138
139 N = integer() >= 0
140 Elem = T
141 List = [T]
142 T = term()
143
144 Returns a list containing N copies of term Elem.
145
146 Example:
147
148 > lists:duplicate(5, xx).
149 [xx,xx,xx,xx,xx]
150
151 filter(Pred, List1) -> List2
152
153 Types:
154
155 Pred = fun((Elem :: T) -> boolean())
156 List1 = List2 = [T]
157 T = term()
158
159 List2 is a list of all elements Elem in List1 for which
160 Pred(Elem) returns true. The Pred function must return a bool‐
161 ean.
162
163 filtermap(Fun, List1) -> List2
164
165 Types:
166
167 Fun = fun((Elem) -> boolean() | {true, Value})
168 List1 = [Elem]
169 List2 = [Elem | Value]
170 Elem = Value = term()
171
172 Calls Fun(Elem) on successive elements Elem of List1. Fun/1 must
173 return either a Boolean or a tuple {true, Value}. The function
174 returns the list of elements for which Fun returns a new value,
175 where a value of true is synonymous with {true, Elem}.
176
177 That is, filtermap behaves as if it had been defined as follows:
178
179 filtermap(Fun, List1) ->
180 lists:foldr(fun(Elem, Acc) ->
181 case Fun(Elem) of
182 false -> Acc;
183 true -> [Elem|Acc];
184 {true,Value} -> [Value|Acc]
185 end
186 end, [], List1).
187
188 Example:
189
190 > lists:filtermap(fun(X) -> case X rem 2 of 0 -> {true, X div 2}; _ -> false end end, [1,2,3,4,5]).
191 [1,2]
192
193 flatlength(DeepList) -> integer() >= 0
194
195 Types:
196
197 DeepList = [term() | DeepList]
198
199 Equivalent to length(flatten(DeepList)), but more efficient.
200
201 flatmap(Fun, List1) -> List2
202
203 Types:
204
205 Fun = fun((A) -> [B])
206 List1 = [A]
207 List2 = [B]
208 A = B = term()
209
210 Takes a function from As to lists of Bs, and a list of As
211 (List1) and produces a list of Bs by applying the function to
212 every element in List1 and appending the resulting lists.
213
214 That is, flatmap behaves as if it had been defined as follows:
215
216 flatmap(Fun, List1) ->
217 append(map(Fun, List1)).
218
219 Example:
220
221 > lists:flatmap(fun(X)->[X,X] end, [a,b,c]).
222 [a,a,b,b,c,c]
223
224 flatten(DeepList) -> List
225
226 Types:
227
228 DeepList = [term() | DeepList]
229 List = [term()]
230
231 Returns a flattened version of DeepList.
232
233 flatten(DeepList, Tail) -> List
234
235 Types:
236
237 DeepList = [term() | DeepList]
238 Tail = List = [term()]
239
240 Returns a flattened version of DeepList with tail Tail appended.
241
242 foldl(Fun, Acc0, List) -> Acc1
243
244 Types:
245
246 Fun = fun((Elem :: T, AccIn) -> AccOut)
247 Acc0 = Acc1 = AccIn = AccOut = term()
248 List = [T]
249 T = term()
250
251 Calls Fun(Elem, AccIn) on successive elements A of List, start‐
252 ing with AccIn == Acc0. Fun/2 must return a new accumulator,
253 which is passed to the next call. The function returns the final
254 value of the accumulator. Acc0 is returned if the list is empty.
255
256 Example:
257
258 > lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1,2,3,4,5]).
259 15
260 > lists:foldl(fun(X, Prod) -> X * Prod end, 1, [1,2,3,4,5]).
261 120
262
263 foldr(Fun, Acc0, List) -> Acc1
264
265 Types:
266
267 Fun = fun((Elem :: T, AccIn) -> AccOut)
268 Acc0 = Acc1 = AccIn = AccOut = term()
269 List = [T]
270 T = term()
271
272 Like foldl/3, but the list is traversed from right to left.
273
274 Example:
275
276 > P = fun(A, AccIn) -> io:format("~p ", [A]), AccIn end.
277 #Fun<erl_eval.12.2225172>
278 > lists:foldl(P, void, [1,2,3]).
279 1 2 3 void
280 > lists:foldr(P, void, [1,2,3]).
281 3 2 1 void
282
283 foldl/3 is tail recursive and is usually preferred to foldr/3.
284
285 join(Sep, List1) -> List2
286
287 Types:
288
289 Sep = T
290 List1 = List2 = [T]
291 T = term()
292
293 Inserts Sep between each element in List1. Has no effect on the
294 empty list and on a singleton list. For example:
295
296 > lists:join(x, [a,b,c]).
297 [a,x,b,x,c]
298 > lists:join(x, [a]).
299 [a]
300 > lists:join(x, []).
301 []
302
303 foreach(Fun, List) -> ok
304
305 Types:
306
307 Fun = fun((Elem :: T) -> term())
308 List = [T]
309 T = term()
310
311 Calls Fun(Elem) for each element Elem in List. This function is
312 used for its side effects and the evaluation order is defined to
313 be the same as the order of the elements in the list.
314
315 keydelete(Key, N, TupleList1) -> TupleList2
316
317 Types:
318
319 Key = term()
320 N = integer() >= 1
321 1..tuple_size(Tuple)
322 TupleList1 = TupleList2 = [Tuple]
323 Tuple = tuple()
324
325 Returns a copy of TupleList1 where the first occurrence of a tu‐
326 ple whose Nth element compares equal to Key is deleted, if there
327 is such a tuple.
328
329 keyfind(Key, N, TupleList) -> Tuple | false
330
331 Types:
332
333 Key = term()
334 N = integer() >= 1
335 1..tuple_size(Tuple)
336 TupleList = [Tuple]
337 Tuple = tuple()
338
339 Searches the list of tuples TupleList for a tuple whose Nth ele‐
340 ment compares equal to Key. Returns Tuple if such a tuple is
341 found, otherwise false.
342
343 keymap(Fun, N, TupleList1) -> TupleList2
344
345 Types:
346
347 Fun = fun((Term1 :: term()) -> Term2 :: term())
348 N = integer() >= 1
349 1..tuple_size(Tuple)
350 TupleList1 = TupleList2 = [Tuple]
351 Tuple = tuple()
352
353 Returns a list of tuples where, for each tuple in TupleList1,
354 the Nth element Term1 of the tuple has been replaced with the
355 result of calling Fun(Term1).
356
357 Examples:
358
359 > Fun = fun(Atom) -> atom_to_list(Atom) end.
360 #Fun<erl_eval.6.10732646>
361 2> lists:keymap(Fun, 2, [{name,jane,22},{name,lizzie,20},{name,lydia,15}]).
362 [{name,"jane",22},{name,"lizzie",20},{name,"lydia",15}]
363
364 keymember(Key, N, TupleList) -> boolean()
365
366 Types:
367
368 Key = term()
369 N = integer() >= 1
370 1..tuple_size(Tuple)
371 TupleList = [Tuple]
372 Tuple = tuple()
373
374 Returns true if there is a tuple in TupleList whose Nth element
375 compares equal to Key, otherwise false.
376
377 keymerge(N, TupleList1, TupleList2) -> TupleList3
378
379 Types:
380
381 N = integer() >= 1
382 1..tuple_size(Tuple)
383 TupleList1 = [T1]
384 TupleList2 = [T2]
385 TupleList3 = [T1 | T2]
386 T1 = T2 = Tuple
387 Tuple = tuple()
388
389 Returns the sorted list formed by merging TupleList1 and Tu‐
390 pleList2. The merge is performed on the Nth element of each tu‐
391 ple. Both TupleList1 and TupleList2 must be key-sorted before
392 evaluating this function. When two tuples compare equal, the tu‐
393 ple from TupleList1 is picked before the tuple from TupleList2.
394
395 keyreplace(Key, N, TupleList1, NewTuple) -> TupleList2
396
397 Types:
398
399 Key = term()
400 N = integer() >= 1
401 1..tuple_size(Tuple)
402 TupleList1 = TupleList2 = [Tuple]
403 NewTuple = Tuple
404 Tuple = tuple()
405
406 Returns a copy of TupleList1 where the first occurrence of a T
407 tuple whose Nth element compares equal to Key is replaced with
408 NewTuple, if there is such a tuple T.
409
410 keysearch(Key, N, TupleList) -> {value, Tuple} | false
411
412 Types:
413
414 Key = term()
415 N = integer() >= 1
416 1..tuple_size(Tuple)
417 TupleList = [Tuple]
418 Tuple = tuple()
419
420 Searches the list of tuples TupleList for a tuple whose Nth ele‐
421 ment compares equal to Key. Returns {value, Tuple} if such a tu‐
422 ple is found, otherwise false.
423
424 Note:
425 This function is retained for backward compatibility. Function
426 keyfind/3 is usually more convenient.
427
428
429 keysort(N, TupleList1) -> TupleList2
430
431 Types:
432
433 N = integer() >= 1
434 1..tuple_size(Tuple)
435 TupleList1 = TupleList2 = [Tuple]
436 Tuple = tuple()
437
438 Returns a list containing the sorted elements of list Tu‐
439 pleList1. Sorting is performed on the Nth element of the tuples.
440 The sort is stable.
441
442 keystore(Key, N, TupleList1, NewTuple) -> TupleList2
443
444 Types:
445
446 Key = term()
447 N = integer() >= 1
448 1..tuple_size(Tuple)
449 TupleList1 = [Tuple]
450 TupleList2 = [Tuple, ...]
451 NewTuple = Tuple
452 Tuple = tuple()
453
454 Returns a copy of TupleList1 where the first occurrence of a tu‐
455 ple T whose Nth element compares equal to Key is replaced with
456 NewTuple, if there is such a tuple T. If there is no such tuple
457 T, a copy of TupleList1 where [NewTuple] has been appended to
458 the end is returned.
459
460 keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false
461
462 Types:
463
464 Key = term()
465 N = integer() >= 1
466 1..tuple_size(Tuple)
467 TupleList1 = TupleList2 = [tuple()]
468 Tuple = tuple()
469
470 Searches the list of tuples TupleList1 for a tuple whose Nth el‐
471 ement compares equal to Key. Returns {value, Tuple, TupleList2}
472 if such a tuple is found, otherwise false. TupleList2 is a copy
473 of TupleList1 where the first occurrence of Tuple has been re‐
474 moved.
475
476 last(List) -> Last
477
478 Types:
479
480 List = [T, ...]
481 Last = T
482 T = term()
483
484 Returns the last element in List.
485
486 map(Fun, List1) -> List2
487
488 Types:
489
490 Fun = fun((A) -> B)
491 List1 = [A]
492 List2 = [B]
493 A = B = term()
494
495 Takes a function from As to Bs, and a list of As and produces a
496 list of Bs by applying the function to every element in the
497 list. This function is used to obtain the return values. The
498 evaluation order depends on the implementation.
499
500 mapfoldl(Fun, Acc0, List1) -> {List2, Acc1}
501
502 Types:
503
504 Fun = fun((A, AccIn) -> {B, AccOut})
505 Acc0 = Acc1 = AccIn = AccOut = term()
506 List1 = [A]
507 List2 = [B]
508 A = B = term()
509
510 Combines the operations of map/2 and foldl/3 into one pass.
511
512 Example:
513
514 Summing the elements in a list and double them at the same time:
515
516 > lists:mapfoldl(fun(X, Sum) -> {2*X, X+Sum} end,
517 0, [1,2,3,4,5]).
518 {[2,4,6,8,10],15}
519
520 mapfoldr(Fun, Acc0, List1) -> {List2, Acc1}
521
522 Types:
523
524 Fun = fun((A, AccIn) -> {B, AccOut})
525 Acc0 = Acc1 = AccIn = AccOut = term()
526 List1 = [A]
527 List2 = [B]
528 A = B = term()
529
530 Combines the operations of map/2 and foldr/3 into one pass.
531
532 max(List) -> Max
533
534 Types:
535
536 List = [T, ...]
537 Max = T
538 T = term()
539
540 Returns the first element of List that compares greater than or
541 equal to all other elements of List.
542
543 member(Elem, List) -> boolean()
544
545 Types:
546
547 Elem = T
548 List = [T]
549 T = term()
550
551 Returns true if Elem matches some element of List, otherwise
552 false.
553
554 merge(ListOfLists) -> List1
555
556 Types:
557
558 ListOfLists = [List]
559 List = List1 = [T]
560 T = term()
561
562 Returns the sorted list formed by merging all the sublists of
563 ListOfLists. All sublists must be sorted before evaluating this
564 function. When two elements compare equal, the element from the
565 sublist with the lowest position in ListOfLists is picked before
566 the other element.
567
568 merge(List1, List2) -> List3
569
570 Types:
571
572 List1 = [X]
573 List2 = [Y]
574 List3 = [X | Y]
575 X = Y = term()
576
577 Returns the sorted list formed by merging List1 and List2. Both
578 List1 and List2 must be sorted before evaluating this function.
579 When two elements compare equal, the element from List1 is
580 picked before the element from List2.
581
582 merge(Fun, List1, List2) -> List3
583
584 Types:
585
586 Fun = fun((A, B) -> boolean())
587 List1 = [A]
588 List2 = [B]
589 List3 = [A | B]
590 A = B = term()
591
592 Returns the sorted list formed by merging List1 and List2. Both
593 List1 and List2 must be sorted according to the ordering func‐
594 tion Fun before evaluating this function. Fun(A, B) is to return
595 true if A compares less than or equal to B in the ordering, oth‐
596 erwise false. When two elements compare equal, the element from
597 List1 is picked before the element from List2.
598
599 merge3(List1, List2, List3) -> List4
600
601 Types:
602
603 List1 = [X]
604 List2 = [Y]
605 List3 = [Z]
606 List4 = [X | Y | Z]
607 X = Y = Z = term()
608
609 Returns the sorted list formed by merging List1, List2, and
610 List3. All of List1, List2, and List3 must be sorted before
611 evaluating this function. When two elements compare equal, the
612 element from List1, if there is such an element, is picked be‐
613 fore the other element, otherwise the element from List2 is
614 picked before the element from List3.
615
616 min(List) -> Min
617
618 Types:
619
620 List = [T, ...]
621 Min = T
622 T = term()
623
624 Returns the first element of List that compares less than or
625 equal to all other elements of List.
626
627 nth(N, List) -> Elem
628
629 Types:
630
631 N = integer() >= 1
632 1..length(List)
633 List = [T, ...]
634 Elem = T
635 T = term()
636
637 Returns the Nth element of List.
638
639 Example:
640
641 > lists:nth(3, [a, b, c, d, e]).
642 c
643
644 nthtail(N, List) -> Tail
645
646 Types:
647
648 N = integer() >= 0
649 0..length(List)
650 List = [T, ...]
651 Tail = [T]
652 T = term()
653
654 Returns the Nth tail of List, that is, the sublist of List
655 starting at N+1 and continuing up to the end of the list.
656
657 Example
658
659 > lists:nthtail(3, [a, b, c, d, e]).
660 [d,e]
661 > tl(tl(tl([a, b, c, d, e]))).
662 [d,e]
663 > lists:nthtail(0, [a, b, c, d, e]).
664 [a,b,c,d,e]
665 > lists:nthtail(5, [a, b, c, d, e]).
666 []
667
668 partition(Pred, List) -> {Satisfying, NotSatisfying}
669
670 Types:
671
672 Pred = fun((Elem :: T) -> boolean())
673 List = Satisfying = NotSatisfying = [T]
674 T = term()
675
676 Partitions List into two lists, where the first list contains
677 all elements for which Pred(Elem) returns true, and the second
678 list contains all elements for which Pred(Elem) returns false.
679
680 Examples:
681
682 > lists:partition(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]).
683 {[1,3,5,7],[2,4,6]}
684 > lists:partition(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
685 {[a,b,c,d,e],[1,2,3,4]}
686
687 For a different way to partition a list, see splitwith/2.
688
689 prefix(List1, List2) -> boolean()
690
691 Types:
692
693 List1 = List2 = [T]
694 T = term()
695
696 Returns true if List1 is a prefix of List2, otherwise false.
697
698 reverse(List1) -> List2
699
700 Types:
701
702 List1 = List2 = [T]
703 T = term()
704
705 Returns a list with the elements in List1 in reverse order.
706
707 reverse(List1, Tail) -> List2
708
709 Types:
710
711 List1 = [T]
712 Tail = term()
713 List2 = [T]
714 T = term()
715
716 Returns a list with the elements in List1 in reverse order, with
717 tail Tail appended.
718
719 Example:
720
721 > lists:reverse([1, 2, 3, 4], [a, b, c]).
722 [4,3,2,1,a,b,c]
723
724 search(Pred, List) -> {value, Value} | false
725
726 Types:
727
728 Pred = fun((T) -> boolean())
729 List = [T]
730 Value = T
731
732 If there is a Value in List such that Pred(Value) returns true,
733 returns {value, Value} for the first such Value, otherwise re‐
734 turns false. The Pred function must return a boolean.
735
736 seq(From, To) -> Seq
737
738 seq(From, To, Incr) -> Seq
739
740 Types:
741
742 From = To = Incr = integer()
743 Seq = [integer()]
744
745 Returns a sequence of integers that starts with From and con‐
746 tains the successive results of adding Incr to the previous ele‐
747 ment, until To is reached or passed (in the latter case, To is
748 not an element of the sequence). Incr defaults to 1.
749
750 Failures:
751
752 * If To < From - Incr and Incr > 0.
753
754 * If To > From - Incr and Incr < 0.
755
756 * If Incr =:= 0 and From =/= To.
757
758 The following equalities hold for all sequences:
759
760 length(lists:seq(From, To)) =:= To - From + 1
761 length(lists:seq(From, To, Incr)) =:= (To - From + Incr) div Incr
762
763 Examples:
764
765 > lists:seq(1, 10).
766 [1,2,3,4,5,6,7,8,9,10]
767 > lists:seq(1, 20, 3).
768 [1,4,7,10,13,16,19]
769 > lists:seq(1, 0, 1).
770 []
771 > lists:seq(10, 6, 4).
772 []
773 > lists:seq(1, 1, 0).
774 [1]
775
776 sort(List1) -> List2
777
778 Types:
779
780 List1 = List2 = [T]
781 T = term()
782
783 Returns a list containing the sorted elements of List1.
784
785 sort(Fun, List1) -> List2
786
787 Types:
788
789 Fun = fun((A :: T, B :: T) -> boolean())
790 List1 = List2 = [T]
791 T = term()
792
793 Returns a list containing the sorted elements of List1, accord‐
794 ing to the ordering function Fun. Fun(A, B) is to return true if
795 A compares less than or equal to B in the ordering, otherwise
796 false.
797
798 split(N, List1) -> {List2, List3}
799
800 Types:
801
802 N = integer() >= 0
803 0..length(List1)
804 List1 = List2 = List3 = [T]
805 T = term()
806
807 Splits List1 into List2 and List3. List2 contains the first N
808 elements and List3 the remaining elements (the Nth tail).
809
810 splitwith(Pred, List) -> {List1, List2}
811
812 Types:
813
814 Pred = fun((T) -> boolean())
815 List = List1 = List2 = [T]
816 T = term()
817
818 Partitions List into two lists according to Pred. splitwith/2
819 behaves as if it is defined as follows:
820
821 splitwith(Pred, List) ->
822 {takewhile(Pred, List), dropwhile(Pred, List)}.
823
824 Examples:
825
826 > lists:splitwith(fun(A) -> A rem 2 == 1 end, [1,2,3,4,5,6,7]).
827 {[1],[2,3,4,5,6,7]}
828 > lists:splitwith(fun(A) -> is_atom(A) end, [a,b,1,c,d,2,3,4,e]).
829 {[a,b],[1,c,d,2,3,4,e]}
830
831 The Pred function must return a boolean. For a different way to
832 partition a list, see partition/2.
833
834 sublist(List1, Len) -> List2
835
836 Types:
837
838 List1 = List2 = [T]
839 Len = integer() >= 0
840 T = term()
841
842 Returns the sublist of List1 starting at position 1 and with
843 (maximum) Len elements. It is not an error for Len to exceed the
844 length of the list, in that case the whole list is returned.
845
846 sublist(List1, Start, Len) -> List2
847
848 Types:
849
850 List1 = List2 = [T]
851 Start = integer() >= 1
852 1..(length(List1)+1)
853 Len = integer() >= 0
854 T = term()
855
856 Returns the sublist of List1 starting at Start and with (maxi‐
857 mum) Len elements. It is not an error for Start+Len to exceed
858 the length of the list.
859
860 Examples:
861
862 > lists:sublist([1,2,3,4], 2, 2).
863 [2,3]
864 > lists:sublist([1,2,3,4], 2, 5).
865 [2,3,4]
866 > lists:sublist([1,2,3,4], 5, 2).
867 []
868
869 subtract(List1, List2) -> List3
870
871 Types:
872
873 List1 = List2 = List3 = [T]
874 T = term()
875
876 Returns a new list List3 that is a copy of List1, subjected to
877 the following procedure: for each element in List2, its first
878 occurrence in List1 is deleted.
879
880 Example:
881
882 > lists:subtract("123212", "212").
883 "312".
884
885 lists:subtract(A, B) is equivalent to A -- B.
886
887 suffix(List1, List2) -> boolean()
888
889 Types:
890
891 List1 = List2 = [T]
892 T = term()
893
894 Returns true if List1 is a suffix of List2, otherwise false.
895
896 sum(List) -> number()
897
898 Types:
899
900 List = [number()]
901
902 Returns the sum of the elements in List.
903
904 takewhile(Pred, List1) -> List2
905
906 Types:
907
908 Pred = fun((Elem :: T) -> boolean())
909 List1 = List2 = [T]
910 T = term()
911
912 Takes elements Elem from List1 while Pred(Elem) returns true,
913 that is, the function returns the longest prefix of the list for
914 which all elements satisfy the predicate. The Pred function must
915 return a boolean.
916
917 ukeymerge(N, TupleList1, TupleList2) -> TupleList3
918
919 Types:
920
921 N = integer() >= 1
922 1..tuple_size(Tuple)
923 TupleList1 = [T1]
924 TupleList2 = [T2]
925 TupleList3 = [T1 | T2]
926 T1 = T2 = Tuple
927 Tuple = tuple()
928
929 Returns the sorted list formed by merging TupleList1 and Tu‐
930 pleList2. The merge is performed on the Nth element of each tu‐
931 ple. Both TupleList1 and TupleList2 must be key-sorted without
932 duplicates before evaluating this function. When two tuples com‐
933 pare equal, the tuple from TupleList1 is picked and the one from
934 TupleList2 is deleted.
935
936 ukeysort(N, TupleList1) -> TupleList2
937
938 Types:
939
940 N = integer() >= 1
941 1..tuple_size(Tuple)
942 TupleList1 = TupleList2 = [Tuple]
943 Tuple = tuple()
944
945 Returns a list containing the sorted elements of list TupleList1
946 where all except the first tuple of the tuples comparing equal
947 have been deleted. Sorting is performed on the Nth element of
948 the tuples.
949
950 umerge(ListOfLists) -> List1
951
952 Types:
953
954 ListOfLists = [List]
955 List = List1 = [T]
956 T = term()
957
958 Returns the sorted list formed by merging all the sublists of
959 ListOfLists. All sublists must be sorted and contain no dupli‐
960 cates before evaluating this function. When two elements compare
961 equal, the element from the sublist with the lowest position in
962 ListOfLists is picked and the other is deleted.
963
964 umerge(List1, List2) -> List3
965
966 Types:
967
968 List1 = [X]
969 List2 = [Y]
970 List3 = [X | Y]
971 X = Y = term()
972
973 Returns the sorted list formed by merging List1 and List2. Both
974 List1 and List2 must be sorted and contain no duplicates before
975 evaluating this function. When two elements compare equal, the
976 element from List1 is picked and the one from List2 is deleted.
977
978 umerge(Fun, List1, List2) -> List3
979
980 Types:
981
982 Fun = fun((A, B) -> boolean())
983 List1 = [A]
984 List2 = [B]
985 List3 = [A | B]
986 A = B = term()
987
988 Returns the sorted list formed by merging List1 and List2. Both
989 List1 and List2 must be sorted according to the ordering func‐
990 tion Fun and contain no duplicates before evaluating this func‐
991 tion. Fun(A, B) is to return true if A compares less than or
992 equal to B in the ordering, otherwise false. When two elements
993 compare equal, the element from List1 is picked and the one from
994 List2 is deleted.
995
996 umerge3(List1, List2, List3) -> List4
997
998 Types:
999
1000 List1 = [X]
1001 List2 = [Y]
1002 List3 = [Z]
1003 List4 = [X | Y | Z]
1004 X = Y = Z = term()
1005
1006 Returns the sorted list formed by merging List1, List2, and
1007 List3. All of List1, List2, and List3 must be sorted and contain
1008 no duplicates before evaluating this function. When two elements
1009 compare equal, the element from List1 is picked if there is such
1010 an element, otherwise the element from List2 is picked, and the
1011 other is deleted.
1012
1013 unzip(List1) -> {List2, List3}
1014
1015 Types:
1016
1017 List1 = [{A, B}]
1018 List2 = [A]
1019 List3 = [B]
1020 A = B = term()
1021
1022 "Unzips" a list of two-tuples into two lists, where the first
1023 list contains the first element of each tuple, and the second
1024 list contains the second element of each tuple.
1025
1026 unzip3(List1) -> {List2, List3, List4}
1027
1028 Types:
1029
1030 List1 = [{A, B, C}]
1031 List2 = [A]
1032 List3 = [B]
1033 List4 = [C]
1034 A = B = C = term()
1035
1036 "Unzips" a list of three-tuples into three lists, where the
1037 first list contains the first element of each tuple, the second
1038 list contains the second element of each tuple, and the third
1039 list contains the third element of each tuple.
1040
1041 usort(List1) -> List2
1042
1043 Types:
1044
1045 List1 = List2 = [T]
1046 T = term()
1047
1048 Returns a list containing the sorted elements of List1 where all
1049 except the first element of the elements comparing equal have
1050 been deleted.
1051
1052 usort(Fun, List1) -> List2
1053
1054 Types:
1055
1056 Fun = fun((T, T) -> boolean())
1057 List1 = List2 = [T]
1058 T = term()
1059
1060 Returns a list containing the sorted elements of List1 where all
1061 except the first element of the elements comparing equal accord‐
1062 ing to the ordering function Fun have been deleted. Fun(A, B) is
1063 to return true if A compares less than or equal to B in the or‐
1064 dering, otherwise false.
1065
1066 zip(List1, List2) -> List3
1067
1068 Types:
1069
1070 List1 = [A]
1071 List2 = [B]
1072 List3 = [{A, B}]
1073 A = B = term()
1074
1075 "Zips" two lists of equal length into one list of two-tuples,
1076 where the first element of each tuple is taken from the first
1077 list and the second element is taken from the corresponding ele‐
1078 ment in the second list.
1079
1080 zip3(List1, List2, List3) -> List4
1081
1082 Types:
1083
1084 List1 = [A]
1085 List2 = [B]
1086 List3 = [C]
1087 List4 = [{A, B, C}]
1088 A = B = C = term()
1089
1090 "Zips" three lists of equal length into one list of three-tu‐
1091 ples, where the first element of each tuple is taken from the
1092 first list, the second element is taken from the corresponding
1093 element in the second list, and the third element is taken from
1094 the corresponding element in the third list.
1095
1096 zipwith(Combine, List1, List2) -> List3
1097
1098 Types:
1099
1100 Combine = fun((X, Y) -> T)
1101 List1 = [X]
1102 List2 = [Y]
1103 List3 = [T]
1104 X = Y = T = term()
1105
1106 Combines the elements of two lists of equal length into one
1107 list. For each pair X, Y of list elements from the two lists,
1108 the element in the result list is Combine(X, Y).
1109
1110 zipwith(fun(X, Y) -> {X,Y} end, List1, List2) is equivalent to
1111 zip(List1, List2).
1112
1113 Example:
1114
1115 > lists:zipwith(fun(X, Y) -> X+Y end, [1,2,3], [4,5,6]).
1116 [5,7,9]
1117
1118 zipwith3(Combine, List1, List2, List3) -> List4
1119
1120 Types:
1121
1122 Combine = fun((X, Y, Z) -> T)
1123 List1 = [X]
1124 List2 = [Y]
1125 List3 = [Z]
1126 List4 = [T]
1127 X = Y = Z = T = term()
1128
1129 Combines the elements of three lists of equal length into one
1130 list. For each triple X, Y, Z of list elements from the three
1131 lists, the element in the result list is Combine(X, Y, Z).
1132
1133 zipwith3(fun(X, Y, Z) -> {X,Y,Z} end, List1, List2, List3) is
1134 equivalent to zip3(List1, List2, List3).
1135
1136 Examples:
1137
1138 > lists:zipwith3(fun(X, Y, Z) -> X+Y+Z end, [1,2,3], [4,5,6], [7,8,9]).
1139 [12,15,18]
1140 > lists:zipwith3(fun(X, Y, Z) -> [X,Y,Z] end, [a,b,c], [x,y,z], [1,2,3]).
1141 [[a,x,1],[b,y,2],[c,z,3]]
1142
1143
1144
1145Ericsson AB stdlib 3.16.1 lists(3)