ListPair
structure
signature LIST_PAIR
structure ListPair
:> LIST_PAIR
The ListPair
structure provides operations on pairs of lists. The operations fall into two categories. Those in the first category, whose names do not end in "Eq"
, 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. The operations in the second category, whose names have the suffix "Eq"
, differ from their similarly named operations in the first category only when the list arguments have unequal lengths, in which case they typically raise the UnequalLengths
exception.
exception UnequalLengths
val zip : 'a list * 'b list -> ('a * 'b) list
val zipEq : 'a list * 'b list -> ('a * 'b) list
val unzip : ('a * 'b) list -> 'a list * 'b list
val app : ('a * 'b -> unit) -> 'a list * 'b list -> unit
val appEq : ('a * 'b -> unit) -> 'a list * 'b list -> unit
val map : ('a * 'b -> 'c) -> 'a list * 'b list -> 'c list
val mapEq : ('a * 'b -> 'c) -> 'a list * 'b list -> 'c list
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 foldlEq : ('a * 'b * 'c -> 'c)
-> 'c -> 'a list * 'b list -> 'c
val foldrEq : ('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
val allEq : ('a * 'b -> bool) -> 'a list * 'b list -> bool
exception UnequalLengths
zip (l1, l2)
zipEq (l1, l2)
zip
ignores the excess elements from the tail of the longer one, while zipEq
raises the exception UnequalLengths
.
unzip l
zip
for equal length lists.
app f (l1, l2)
appEq f (l1, l2)
UnequalLengths
. The above expressions are respectively equivalent to:
List.app f (zip (l1, l2)) List.app f (zipEq (l1, l2))ignoring possible side-effects of the function f.
map f (l1, l2)
mapEq f (l1, l2)
UnequalLengths
. The above expressions are respectively equivalent to:
List.map f (zip (l1, l2)) List.map f (zipEq (l1, l2))ignoring possible side-effects of the function f.
foldl f init (l1, l2)
foldr f init (l1, l2)
foldlEq f init (l1, l2)
foldrEq f init (l1, l2)
List.foldl f' init (zip (l1, l2)) List.foldr f' init (zip (l1, l2)) List.foldl f' init (zipEq (l1, l2)) List.foldr f' init (zipEq (l1, l2))where f' is
fn ((a,b),c) => f(a,b,c)
and ignoring possible side-effects of the function f.
all f (l1, l2)
exists f (l1, l2)
List.all f (zip (l1, l2)) List.exists f (zip (l1, l2))
allEq f (l1, l2)
true
if l1 and l2 have equal length and all pairs of elements satisfy the predicate f. That is, the expression is equivalent to:
(List.length l1 = List.length l2) andalso (List.all f (zip (l1, l2)))This function does not appear to have any nice algebraic relation with the other functions, but it is included as providing a useful notion of equality, analogous to the notion of equality of lists over equality types.
Implementation note:
The implementation is simple:
fun allEq p ([], []) = true | allEq p (x::xs, y::ys) = p(x,y) andalso allEq p (xs,ys) | allEq _ _ = false
List
Note that a function requiring equal length arguments should determine this lazily, i.e., it should act as though the lists have equal length and invoke the user-supplied function argument, but raise the exception if it arrives at the end of one list before the end of the other.
Generated April 12, 2004
Last Modified May 28, 2000
Comments to John Reppy.
This document may be distributed freely over the internet as long as the copyright notice and license terms below are prominently displayed within every machine-readable copy.
Copyright © 2004 AT&T and Lucent Technologies. All rights reserved.
Permission is granted for internet users to make one paper copy for their
own personal use. Further hardcopy reproduction is strictly prohibited.
Permission to distribute the HTML document electronically on any medium
other than the internet must be requested from the copyright holders by
contacting the editors.
Printed versions of the SML Basis Manual are available from Cambridge
University Press.
To order, please visit
www.cup.org (North America) or
www.cup.cam.ac.uk (outside North America). |