The Standard ML Basis Library


The ListPair structure

The ListPair structure provides operations on pairs of lists. These operations do not require that the lists have the same length; when the lists are of uneven lengths, the excess elements from the tail of the longer list are ignored.


Synopsis

signature LIST_PAIR
structure ListPair : LIST_PAIR

Interface

val zip : ('a list * 'b list) -> ('a * 'b) list
val unzip : ('a * 'b) list -> ('a list * 'b list)
val map : ('a * 'b -> 'c) -> ('a list * 'b list) -> 'c list
val app : ('a * 'b -> unit) -> ('a list * 'b list) -> unit
val foldl : (('a * 'b * 'c) -> 'c) -> 'c -> ('a list * 'b list) -> 'c
val foldr : (('a * 'b * 'c) -> 'c) -> 'c -> ('a list * 'b list) -> 'c
val all : ('a * 'b -> bool) -> ('a list * 'b list) -> bool
val exists : ('a * 'b -> bool) -> ('a list * 'b list) -> bool

Description

zip (l1, l2)
combines the two lists l1 and l2 into a list of pairs, with the first element of each list comprising the first element of the result, the second elements comprising the second element of the result, and so on. If the lists are of unequal lengths, the excess elements from the tail of the longer one are ignored.

unzip l
returns a pair of lists formed by splitting the elements of l. This is the inverse of zip for equal length lists.

map f (l1, l2)
maps the function f over the list of pairs of elements from the lists l1 and l2, returning the list of results. If the lists are of unequal lengths, the excess elements from the tail of the longer one are ignored. The above expression is equivalent to: List.map f (zip (l1, l2)).

app f (l1, l2)
applies the function f to the list of pairs of elements from from the lists l1 and l2. If the lists are of unequal lengths, the excess elements from the tail of the longer one are ignored. The above expression is equivalent to: List.app f (zip (l1, l2)).

foldl f c (l1, l2)
foldr f c (l1, l2)
return the result of folding the function f over the pair of lists l1 and l2. They are respectively equivalent to: List.foldl f' c (zip (l1, l2)) and List.foldr f' c (zip (l1, l2)) where f' is fn ((a,b),c) => f(a,b,c).

all pred (l1, l2)
exists pred (l1, l2)
These functions provide short-circuit testing of a predicate over a pair of lists. They are respectively equivalent to: List.all pred (zip (l1, l2)) and List.exists pred (zip (l1, l2)).


Discussion

Rationale:

Another approach to these functions is to raise an exception when the argument lists are of uneven length. We chose not to do this, because comparing the lists for length conflicts with the short-circuit evaluation of all and exists.

See Also

List