180 likes | 364 Views
Sequences. What is sequence?. A sequence is an ordered collection of objects. We use sequences to model collections in which order or multiplicity is important. If we do not wish to impose an order, then we may choose to use bags instead.
E N D
What is sequence? • A sequence is an ordered collection of objects. • We use sequences to model collections in which order or multiplicity is important. • If we do not wish to impose an order, then we may choose to use bags instead. • A sequence of values of type X is a partial function from natural numbers N to X whose domain is just 1..n, where n is the length of the sequence. • Formally we can define the set of all sequences of values of type X as follows: seq X == { f : N ↛X | (n : N • dom f = 1..n)}
Since seq X is a set of functions, the typeofseq X must be ℙ (ℙ(ℤ X)) • The type of any sequence of values of type X is ℙ(ℤ X) • The following notation denote a sequence: {1↦u, 2 ↦s, 3 ↦i, 4 ↦n, 5 ↦g, 6 ↦z} ‹u,s,i,n,g,z› ‹› empty sequence
If we let s denotes the above sequence, then the following statements are well-formed and true: dom s = 1..6 ran s = {u,s,i,n,g,z} #s = 4 s 4 = n
Sequence notation • last s = s #s • front s = {#s} s
Filter • If s is a sequence, then s ↾ A is the largest subsequence of s containing only those objects that are elements of A. <a, b, c, d, e, d, c, b, a> ↾ {a, b} = <a, b, b, a>
Bag • A bag is an unordered collection of objects in which multiplicities are important: [[ ]] [[a, b, c, a, b, c]]