1.3. Data Structures

Orc supports three basic data structures, tuples, lists, and records. This section describes tuples and lists; records are described in a subsequent chapter.

1.3.1. Tuples

A tuple expression is a comma-separated sequence of at least two expressions, enclosed by parentheses. Each expression is executed and its first published value is taken; the value of the whole tuple expression is a tuple containing each of these values in order. If any of the expressions is silent, then the whole tuple expression is silent.

Examples

  • (1+2, 7) evaluates to (3,7).

  • ("true" + "false", true || false, true && false) evaluates to ("truefalse", true, false).

  • (2/2, 2/1, 2/0) is silent, since 2/0 is a silent expression.

1.3.2. Lists

A list expression is a comma-separated sequence of expressions enclosed by square brackets. It may be of any length, including zero. Each expression is executed and its first published value is taken; the value of the whole list expression is a list containing each of these values in order. If any of the expressions is silent, then the whole list expression is silent.

Examples

  • [1,2+3] publishes [1,5].
  • [true && true] publishes [true].
  • [] just publishes [], the empty list.
  • [5, 5/0, 5] is silent, since 5/0 is a silent expression.

There is also a concatenation (cons) operation on lists, written F:G, where F and G are expressions. It publishes a new list whose first element is the value of F and whose remaining elements are the list value of G.

Examples

  • (1+3):[2+5,6] publishes [4,7,6].
  • 2:2:5:[] publishes [2,2,5].
  • Suppose t is bound to [3,5]. Then 1:t publishes [1,3,5].
  • 2:3 is silent, because 3 is not a list.

1.3.3. Patterns

The Orc language provides pattern matching capabilities to inspect and extract pieces of a data structure. A pattern may be used wherever a variable could be bound; for example, in a val declaration, or a sequential combinator.

The following val declarations bind z to (3,4), x to 3, and y to 4, using the tuple pattern (x,y):

val z = (3,4)
val (x,y) = z

The wildcard pattern _ lets us ignore irrelevant parts of a data structure. Here is an expression which extracts the first element of a list:

[1,2,3] >first:_> first

Notice that cons (:) is being used as a pattern to separate a list into its head and tail.

A pattern may also be used as a filter. A literal value may be used as a pattern; if the same value is not present in the data structure being matched, the pattern will fail, and the value will be discarded. Here is an expression which publishes the second element of pairs with first element 0, and ignores any other pair:

( (0,3) | (1,4) | (2,5) | (0,6) ) >(0,x)> x