alice
library
manual.

Alice Project

The ListPair structure


________ Synopsis ____________________________________________________

    signature LIST_PAIR
    structure ListPair : LIST_PAIR
  

An extended version of the Standard ML Basis' ListPair structure.

See also: List, VectorPair


________ Import ______________________________________________________

Imported implicitly.


________ Interface ___________________________________________________

    signature LIST_PAIR =
    sig
        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 appr :          ('a * 'b -> unit) -> 'a list * 'b list -> unit
	val apprEq :        ('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 mapPartial :    ('a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
	val mapPartialEq :  ('a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
	val foldl :         ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c
	val foldlEq :       ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c
	val foldr :         ('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 allEq :         ('a * 'b -> bool) -> 'a list * 'b list -> bool
	val exists :        ('a * 'b -> bool) -> 'a list * 'b list -> bool
	val existsEq :      ('a * 'b -> bool) -> 'a list * 'b list -> bool
	val find :          ('a * 'b -> bool) -> 'a list * 'b list -> ('a * 'b) option
	val filter :        ('a * 'b -> bool) -> 'a list * 'b list -> ('a * 'b) list
	val filterEq :      ('a * 'b -> bool) -> 'a list * 'b list -> ('a * 'b) list

	val appi :          (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit
	val appiEq :        (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit
	val appri :         (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit
	val appriEq :       (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit
	val mapi :          (int * 'a * 'b -> 'c) -> 'a list * 'b list -> 'c list
	val mapiEq :        (int * 'a * 'b -> 'c) -> 'a list * 'b list -> 'c list
	val mapiPartial :   (int * 'a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
	val mapiPartialEq : (int * 'a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
	val foldli :        (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list ->'c
	val foldliEq :      (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list ->'c
	val foldri :        (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c
	val foldriEq :      (int * 'a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c
	val alli :          (int * 'a * 'b -> bool) -> 'a list * 'b list -> bool
	val alliEq :        (int * 'a * 'b -> bool) -> 'a list * 'b list -> bool
	val existsi :       (int * 'a * 'b -> bool) -> 'a list * 'b list -> bool
	val existsiEq :     (int * 'a * 'b -> bool) -> 'a list * 'b list -> bool
	val findi :         (int * 'a * 'b -> bool) -> 'a list * 'b list -> (int * 'a * 'b) option
	val filteri :       (int * 'a * 'b -> bool) -> 'a list * 'b list -> (int * 'a * 'b) list
	val filteriEq :     (int * 'a * 'b -> bool) -> 'a list * 'b list -> (int * 'a * 'b) list
    end
  

________ Description _________________________________________________

Items not described here are as in the Standard ML Basis' ListPair structure.

appr f (l1, l2)

Like app, but applies f in right to left order.

mapPartial 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 where f was defined. 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.mapPartial f (zip (l1, l2)).
find f (l1, l2)

Applies f to each pair (x1,x2) of elements of the lists, from left to right, until f(x1,x2) evaluates to true; returns SOME(x1,x2) if such a pair exists and NONE otherwise. 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.find f (zip (l1, l2))
appi f (l1, l2)
appri f (l1, l2)
mapi f (l1, l2)
mapiPartial f (l1, l2)
foldli f b (l1, l2)
foldri f b (l1, l2)
alli f (l1, l2)
existsi f (l1, l2)
findi f (l1, l2)
filteri f (l1, l2)

Indexed versions of the functions app, appr, map, mapPartial, foldl, foldr, all, exists, find and filter. The index of each pair of elements (starting from 0) is passed to f as an additional argument. For appri and foldri, processing starts at the highest index. The functions findi and filteri return indices along with the corresponding pair of elements. The following equivalences hold:

        app f (l1, l2)        = appi (f o g1) (l1, l2)
        appr f (l1, l2)       = appri (f o g1) (l1, l2)
        map f (l1, l2)        = mapi (f o g1) (l1, l2)
        mapPartial f (l1, l2) = mapiPartial (f o g1) (l1, l2)
        foldl f b (l1, l2)    = foldli (f o g2) b (l1, l2)
        foldr f b (l1, l2)    = foldri (f o g2) b (l1, l2)
        all f (l1, l2)        = alli (f o g1) (l1, l2)
        exists f (l1, l2)     = existsi (f o g1) (l1, l2)
        find f (l1, l2)       = Option.map g1 (findi (f o g1) (l1, l2))
        filter f (l1, l2)     = map g1 (filteri (f o g1) (l1, l2))

where

        g1 = fn (i,a,b) => f(a,b)
        g2 = fn (i,a,b,c) => f(a,b,c)
apprEq f (l1, l2)
mapPartialEq f (l1, l2)

Variants of the functions appr and mapPartial that raise UnequalLengths if the argument lists are not of equal lengths.

appiEq f (l1, l2)
appriEq f (l1, l2)
mapiEq f (l1, l2)
mapiPartialEq f (l1, l2)
foldliEq f b (l1, l2)
foldriEq f b (l1, l2)
filteriEq f (l1, l2)

Variants of the functions appi, appri, mapi, mapiPartial, foldli, foldri and filteri that raise UnequalLengths if the argument lists are not of equal lengths.

alliEq f (l1, l2)
existsiEq f (l1, l2)

Indexed version of the functions allEq and existsEq. The index of each pair of elemente (starting from 0) is passed to f as an additional argument. The following equivalence hold:

        allEq f (l1, l2)    = alliEq (f o g) (l1, l2)
        existsEq f (l1, l2) = existsiEq (f o g) (l1, l2)

where

        g = fn (i,a,b) => f(a,b)
    


last modified 2007/Mar/30 17:10