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