Agenda
School of Technology
•
•
•
•
•
•
Sequences
Andrew Simpson
Revised by David Lightfoot
Need for order
Sets versus sequences
The empty sequence
Operations on sequences
Injective sequences
Recursively defined sequences
1
School of Technology
Sequence versus set
Notation
• A set is a collection where an element is either
present or absent, and there is no concept of order.
•
Example, the set of persons attending a class
• s ∈ seq X
2
• s = 〈a, b, b, a〉
• 〈a, b, b, a〉 ≠ 〈a, a, b, b 〉
• 〈a, b, b, a〉 ≠ 〈a, b〉
• A sequence has an order, and an element can be
present more than once.
〈 〉 are ‘sequence brackets’ or ‘chevrons’
• Example, the sequence of names on attendance list
School of Technology
3
Definition
School of Technology
4
A sequence is a function is a relation is a set
〈a, b, b, a〉
=
{ 1 å a, 2 å b, 3 å b, 4 å a }
• A sequence is a function that relates an index to a
value:
seq X = N ß X, where dom s = 1.. #s
=
{ (1, a), (2, b ), (3, b), (4, a) }
• Note: indexing starts at 1
School of Technology
5
School of Technology
6
1
Potatoes
Range of a sequence
• Note that the range of a sequence s, ran s, is the set
of elements that appear in s:
s
1
2
• Example:
classList ∈ seq Student
attenders ∈ PStudent
a
3
4
b
ran classList = attenders
School of Technology
7
School of Technology
Empty sequence
Length of a sequence
• The empty sequence is written:
〈〉
• The length of a sequence is always the same as the
size of the function that represents it:
8
#s
Examples
#〈〉=0
# 〈a, b, b, a 〉 = 4
School of Technology
9
School of Technology
10
Concatenation
Head and tail
• ‘Concatenation’: joining together, catena Latin chain
s^ t
• The concatenation of sequence s and sequence t is
the sequence formed by taking the sequence s and
joining the sequence t to the back of it
• The head of a (non-empty) sequence is its first
element
head s = s 1
(treating the sequence as a function and applying it to
the index 1)
• Example
〈 Oxford, Didcot 〉^ 〈 Reading, Slough, Paddington 〉
• The tail of a (non-empty) sequence is what remains if
the head is removed. It is itself a sequence.
=
〈 Oxford, Didcot, Reading, Slough, Paddington 〉
School of Technology
11
School of Technology
12
2
Examples
•
•
•
•
•
Reverse
head 〈a, b, b, a 〉 = a
tail 〈a, b, b, a 〉 = 〈b, b, a 〉
head 〈 〉 undefined
tail 〈a 〉 = 〈 〉
tail 〈 〉
undefined
• The reverse of a sequence s is the sequence that
has s’s elements in reverse order:
reverse 〈 〉 = 〈 〉
reverse 〈 x 〉 = 〈 x 〉
reverse 〈 x 〉 ^ s = (reverse s) ^ 〈 x 〉
reverse (s^ t) = reverse t ^ reverse s
Note that reverse is defined recursively
School of Technology
13
School of Technology
14
Injective sequence
Inverse of an injective sequence
• An injective sequence is one that could be
considered as an injective function; each index maps
to a different element
• An injective sequence does not permit duplicates
• Notation
s ∈ iseq X
• Since an injective sequence is an injective function,
its inverse is an injective function;
one that relates an X value to its index position in the
sequence:
s ∈ iseq X
s~ = X © N, where ran s = 1.. #s
‘s is an injective sequence of X’s’
iseq X = N © X, where dom s = 1.. #s
School of Technology
15
squash
School of Technology
16
Sequence filtering
• Since a sequence is a function and a function is a
relation, we may apply relational restriction operators
to a sequence, for example, to ‘filter out’ certain
elements.
• This will usually leave a ‘gappy’ sequence, where
some index values do not have corresponding
elements. The special function squash delivers a
sequence formed by ‘squashing up’ a gappy
sequence:
s ∈ seq X; B ∈ PY
squash (3..4 r s) squash (s u B)
School of Technology
17
• We may choose to filter the elements of a sequence
by a given set.
• Given
s ∈ seq X and A ∈ PX
then s ¡ A (‘s filtered by A’)
is the sequence formed of the elements of s that are
in A
• It is the same as the result of squashing the range
restriction of s to the set A
s ¡ A = squash (s t A)
attendanceList ¡ internationalStudents
School of Technology
18
3
Summary
• Need for order: sequences
• A sequence is a function
• The empty sequence
• Operations on sequences: #, ^ , head, tail
• Injective sequences
• Recursively defined sequences: reverse
• squash, filtering
School of Technology
19
4
© Copyright 2026 Paperzz