Tuple

From the SETL Wiki

Jump to: navigation, search

Contents

[edit] Introduction

The tuple type in SETL represents a sequence of arbitrary values. Any value can be an element of a tuple, and tuples can contain elements of mixed types (unless a repr declaration prohibits it.) Tuples are similar to arrays and vectors in other languages. Tuple indices start from 1 in SETL (like in PASCAL), not 0 (like in most other languages including Java and C.) Tuples may contain "holes", that is to say not every index in range necessarily has a corresponding value.

[edit] Creating new tuples

[edit] The empty tuple

The simplest tuple is of course the empty tuple, denoted by []. Tuples can be created by enumerating the elements in the order that they are to appear (or expressions that evaluate to elements) e.g. [2, 3+1, om, 5+1] or equivalently [2, 4, om, 6}. Note that the 3rd element is absent (denoted by om.)

[edit] Operations on tuples

[edit] Appending additional elements

The with operator appends a value to the end of a tuple e.g.

  • [1, 2] with 3 = [1, 2, 3]

[edit] Accessing, replacing and removing individual elements

If a is a tuple then the form a(i) refers to the ath element of i, e.g.

a := [1, 2];
print(a);
print(a(1));
print(a(3));

outputs

 [1 2]
 1
 *

This form can also appear on the left of an assignment to update an element, e.g.

a := [1, 2];
print(a);
a(1) := 66;
print(a);
a(3) := 77;
print(a);
a(2) := om;
print(a);
a(5) := 88;
print(a);

outputs

 [1 2]
 [66 2]
 [66 2 77]
 [66 * 77]
 [66 * 77 * 88]

Note that unsetting an element (by assigning om to it) does not move other elements to fill the gap, but see fromb, frome and slices, below.

[edit] Removing elements from the beginning or end

  • The fromb operator removes an element from the beginning of a tuple. x fromb t assigns t(1) to x, then removes it and shifts each remaining element of t down one position.
  • Similarly the frome operator removes an element from the end of a tuple. x frome t assigns the last element of t to x, then removes it, without changing the indices of the remaining elements.

[edit] Replacing and resizing ranges (slices)

The form a(i..j) refers to another tuple consisting of the ith through jth elements of a, e.g.

a := [5, 6, 7, 8];
print(a(2 .. 3));

outputs

 [6 7]

This form can also appear on the left of an assignment to update or resize part of the tuple, e.g.

a := [5, 6, om, 8];
print(a);
a(1 .. 2) := [55, 66];
print(a);
a(2 .. 3) := [3, 2, 1];
print(a);
a(1 .. 3) := [];
print(a);
a(2 .. 1) := [om, 77];
print(a);

outputs

 [5 6 * 8]
 [55 66 * 8]
 [55 3 2 1 8]
 [1 8]
 [1 * 77 8]

[edit] Deriving new tuples from existing tuples

New tuples can be derived by comprehension, i.e. evaluating an expression, substituting each element of an existing tuple in turn, and returning a new tuple containing the results at the same positions, e.g. [x * x: x in [1, 2, 3]] yields [1, 4, 9].

[edit] Tuple arithmetic

  • Tuples can be concatenated using the + operator, e.g. [1, 2] + [3, 4] = [1, 2, 3, 4]
  • Tuples can be replicated using the * operator, e.g. [1, 2] * 3 = [1, 2, 1, 2, 1, 2]. The operands can be switched, as in 3 * [1, 2], with the same result.

[edit] Other operations using tuples

The # operator returns the length of a tuple, i.e. the highest index associated with an element. Because of the possibility of holes being present, this is not necessarily the same as the number of elements.

As well as being used in comprehensions (see above) iterators can be used to loop over the elements of a tuple in sequence, e.g.

SETL

(for x in [1, 2, 3])
  print(x);
end;
 

SETL2

for x in [1, 2, 3] loop
  print(x);
end;

outputs:

1
2
3

[edit] Tuple predicates

  • The in and notin operators test whether or not a value is an element of a tuple. Since tuples are not indexed by value this is inefficient as it involves scanning the sequence from beginning to end.
  • The is_tuple predicate returns true for all tuples and false for all non-tuple values.
Personal tools