Orc supports three basic data structures, tuples, lists, and records. This section describes tuples and lists; records are described in a subsequent chapter.
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.
(1+2, 7) evaluates to
("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.
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.
[true && true]publishes
, the empty list.
[5, 5/0, 5]is silent, since
5/0is a silent expression.
There is also a concatenation (cons) operation on lists,
: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.
tis bound to
2:3is silent, because
3is not a list.
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.
val declarations bind
4, using the tuple
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