List structure
signature LIST
structure List :> LIST
The List structure provides a collection of utility functions for manipulating polymorphic lists, traditionally an important datatype in functional programming.
Following the concrete syntax provided by the list :: operator, the head of a list appears leftmost. Thus, a traversal of a list from left to right starts with the head, then recurses on the tail. In addition, as a sequence type, a list has an indexing of its elements, with the head having index 0, the second element having index 1, etc.
datatype 'a list = nil | :: of 'a * 'a list
exception Empty
val null : 'a list -> bool
val length : 'a list -> int
val @ : 'a list * 'a list -> 'a list
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val last : 'a list -> 'a
val getItem : 'a list -> ('a * 'a list) option
val nth : 'a list * int -> 'a
val take : 'a list * int -> 'a list
val drop : 'a list * int -> 'a list
val rev : 'a list -> 'a list
val concat : 'a list list -> 'a list
val revAppend : 'a list * 'a list -> 'a list
val app : ('a -> unit) -> 'a list -> unit
val map : ('a -> 'b) -> 'a list -> 'b list
val mapPartial : ('a -> 'b option) -> 'a list -> 'b list
val find : ('a -> bool) -> 'a list -> 'a option
val filter : ('a -> bool) -> 'a list -> 'a list
val partition : ('a -> bool)
-> 'a list -> 'a list * 'a list
val foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
val foldr : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
val exists : ('a -> bool) -> 'a list -> bool
val all : ('a -> bool) -> 'a list -> bool
val tabulate : int * (int -> 'a) -> 'a list
val collate : ('a * 'a -> order)
-> 'a list * 'a list -> order
exception Empty
null l
true if the list l is empty.
length l
l1 @ l2
hd l
Empty if l is nil.
tl l
Empty if l is nil.
last l
Empty if l is nil.
getItem l
NONE if the list is empty, and SOME(hd l,tl l) otherwise. This function is particularly useful for creating value readers from lists of characters. For example, Int.scan StringCvt.DEC getItem has the type
(int,char list) StringCvt.readerand can be used to scan decimal integers from lists of characters.
nth (l, i)
Subscript if i < 0 or i >= length l. We have nth(l,0) = hd l, ignoring exceptions.
take (l, i)
Subscript if i < 0 or i > length l. We have take(l, length l) = l.
drop (l, i)
Subscript if i < 0 or i > length l. It holds that take(l, i) @ drop(l, i) = l when 0 <= i <= length l. We also have drop(l, length l) = [].
rev l
concat l
concat[l1,l2,...ln] = l1 @ l2 @ ... @ ln
revAppend (l1, l2)
(rev l1) @ l2.
app f l
map f l
mapPartial f l
SOME stripped, where f was defined. f is not defined for an element of l if f applied to the element returns NONE. The above expression is equivalent to:
((map valOf) o (filter isSome) o (map f)) l
find f l
f x evaluates to true. It returns SOME(x) if such an x exists; otherwise it returns NONE.
filter f l
f x evaluated to true, in the same order as they occurred in the argument list.
partition f l
(pos, neg) where pos is the list of those x for which f x evaluated to true, and neg is the list of those for which f x evaluated to false. The elements of pos and neg retain the same relative order they possessed in l.
foldl f init [x1, x2, ..., xn]
f(xn,...,f(x2, f(x1, init))...)or init if the list is empty.
foldr f init [x1, x2, ..., xn]
f(x1, f(x2, ..., f(xn, init)...))or init if the list is empty.
exists f l
f x evaluates to true; it returns true if such an x exists and false otherwise.
all f l
f x evaluates to false; it returns false if such an x exists and true otherwise. It is equivalent to not(exists (not o f) l)).
tabulate (n, f)
[f(0), f(1), ..., f(n-1)], created from left to right. It raises Size if n < 0.
collate f (l1, l2)
General,ListPair
The list type is considered primitive and is defined in the top-level environment. It is rebound here for consistency.
Rationale:
Lists are usually supported with a large collection of library functions. Here, we provide a somewhat smaller collection of operations that reflect common usage. We feel the collection is moderately complete, in that most programs will not need to define additional list operations. We have tried to adopt names that reflect a consensus from various existing libraries and texts. We have avoided functions relying on equality types.
Different SML implementations may still desire to provide list utility library modules, though if the design of
Listis right, they should be small.
Generated April 12, 2004
Last Modified May 24, 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). |