Lecture 004

Array Implementation of Sequence

\langle a_0, ..., a_{n-1}\rangle represented as [a_0, ..., a_{n-1}], n, (l, r) Where it stores a 'a list, the length of array, and left, right index of subsequence.

By design, the sequence is immutable, since we want pure functions. The only way to get something new is to construct a new sequence.

nth A i = A[i]

length A = |A|

append (A, B)

tabulate f n = <f(i) : 0 <= i <= n>: construct a new sequence

subseq A (start, length) = <A[i] : start <= i < start + length>: implemented as array slice


filter predicate A:

inject: since each modification requires copy of a new array, we want to modify multiple indice at once.

Side Note About Compairson Function

The function Seq.merge require a compairson function that satisfy the following properties:

A invalid compairson function might be: sml fun cmp (x, y) = if x < y then LESS else GREATER

If the requirement is not satisfied, some compiler will generate infinite loop when using Seq.merge.

Therefore, you should use Int.compare whenever possible.

Table of Content