Skip to content
John Reppy edited this page Oct 27, 2015 · 3 revisions
structure ListPair : LIST_PAIR

This page is part of proposal 2015-003.


Synopsis

signature LIST_PAIR
structure ListPair :> LIST_PAIR

This proposal adds additional functions to the ListPair module.

Interface

val appi          : (int * 'a * 'b -> unit) -> 'a list * 'b list -> unit
val appiEq        : (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 mapPartial    : ('a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
val mapPartialEq  : ('a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
val mapPartiali   : (int * 'a * 'b -> 'c option) -> 'a list * 'b list -> 'c list
val mapPartialiEq : (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 find          : ('a * 'b -> bool) -> 'a list * 'b list -> ('a * 'b) option
val findi         : (int * 'a * 'b -> bool) -> 'a list * 'b list
                      -> (int * 'a * 'b) option

val unzipMap      : ('a -> 'b * 'c) -> 'a list -> 'b list * 'c list
val unzipMapi	  : (int * 'a -> 'b * 'c) -> 'a list -> 'b list * 'c list

Description

  • appi f (l1, l2)

Like `app`, this function applies the function `f` to the list of pairs of elements generated from left to right from the lists `l1` and `l2`, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length, then the excess elements from the longer list are ignored.
  • appiEq f (l1, l2)

Like `appEq`, this function applies the function `f` to the list of pairs of elements generated from left to right from the lists `l1` and `l2`, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length and the evaluation of `f` terminates normally, then the `UnequalLengths` exception is raised.
  • mapi f (l1, l2)

Like `map`, this function maps the function `f` over the list of pairs of elements generated from left to right from the lists `l1` and `l2`, returning the list of results, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length, then the excess elements from the longer list are ignored.
  • mapiEq f (l1, l2)

Like `map`, this function maps the function `f` over the list of pairs of elements generated from left to right from the lists `l1` and `l2`, returning the list of results, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length and the evaluation of `f` terminates normally, then the `UnequalLengths` exception is raised.
  • mapPartial f (l1, l2)

maps the function `f` over the the list of pairs of elements generated from left to right from the lists `l1` and `l2`, and returns a list of results for those arguments on which `f` return some result. If the lists have unequal length, then the excess elements from the longer list are ignored. It is equivalent to the expression ```sml ((List.map valOf) o (List.filter isSome) o (map f)) (l1, l2) ```
  • mapPartialEq f (l1, l2)

The same as `mapPartial`, except that it raises the `UnequalLengths` exception if the lists have different lengths (assuming that `f` terminates normally).
  • mapPartiali f (l1, l2)

Like `mapPartial`, this function maps the function `f` over the the list of pairs of elements generated from left to right from the lists `l1` and `l2`, and returns a list of results for those arguments on which `f` return some result, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length, then the excess elements from the longer list are ignored.
  • mapPartialiEq f (l1, l2)

The same as `mapPartiali`, except that it raises the `UnequalLengths` exception if the lists have different lengths (assuming that `f` terminates normally).
  • foldli f init (l1, l2)

Like `foldl`, this function reduces the pair of lists, `l1` and `l2`, by applying the reduction operator `f` from left to right, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length, then the excess elements from the longer list are ignored.
  • foldliEq f init (l1, l2)

The same as `foldli`, except that it raises the `UnequalLengths` exception if the lists ave different lengths (assuming that `f` erminates normally).
  • foldri f init (l1, l2)

Like `foldr`, this function reduces the pair of lists, `l1` and `l2`, by applying the reduction operator `f` from right to left, but it also supplies `f` with the index of the corresponding elements. If the lists have unequal length, then the excess elements from the longer list are ignored.
  • foldriEq f init (l1, l2)

The same as `foldri`, except that it raises the `UnequalLengths` exception if the lists ave different lengths (assuming that `f` terminates normally).
  • find pred (xs, ys)

searches the list of pairs of elements generated from left to right from the lists `xs` and `ys` until it finds a pair `(x, y)` that satisfies the predicate `pred`. It returns the `SOME(x, y)` for the first such pair found; otherwise it returns `NONE` if no pair satisfying the predicate is found. It ignores excess elements if one of the lists is longer than the other. The above expression is equivalent to ```sml List.find pred (zip (xs, ys)) ```
  • findi pred (xs, ys)

Like `find`, this function searches from right to left for a pair of elements that satisfy the predicate `pred`, but it also supplies the index of the pair to `pred` and, if it finds a pair `(x, y)` at index `i`, it returns `SOME(i, x, y)`. It returns `NONE` if no pair satisfying the predicate is found. It ignores excess elements if one of the lists is longer than the other.
  • unzipMap f l

maps the function `f` over the list of elements of `l` from left to right, returning a pair of lists of results. It is equivalent to the expression ```sml unzip (List.map f l) ``` * `unzipMapi f l`
Live `mapUnzipi`, this function maps the function `f` over the of elements of `l` from left to right, returning a pair of lists of results, but it also supplies `f` with the index of the corresponding element. It is equivalent to the expression ```sml unzip (List.mapi f l) ```

Rationale

The ListPair structure does not provide a version of the mapPartial function, which was an oversight in the original design. Furthermore, the ListPair module should mirror those additional List operations for which it makes sense.

Clone this wiki locally