Node:Tuples, Next:Keyword tuples, Previous:Parameter lists, Up:Functions
Given a sequence X, the form X@
yields
a tuple of the elements of X.
A tuple is a list of elements, just like a sequence.
The main difference is that a tuple of one elements
is equal to that element: [X]@ = X
.
Tuples are not sequences, though you can convert
between them with loss of information.
Tuples are used for the parameter lists of functions, as well the results of multi-valued functions. They are volatile, not real objects, and cannot be assigned to variables or be parts of data structures. 1
If a parameter is a tuple, it is treated as if there were many parameters, one for each element of the tuple. Thus the following are equivalent:
F 10 [1 2 3]@ 20 F 10 1 2 3 20In both cases,
F
is applied with 5 arguments.
In this respect, the list constructor form [...]
is considered to be a function. Thus:
[10 [1 2 3]@ 20]
evaluates to [10 1 2 3 20]
, a sequence of 5 elements.
Note that [...]
@
cancel each other:
If X is a sequence, then: [X@] == X
.
If T is a tuple expression (there are no tuple objects,
strictly speaking), then [T]@ == T
.
A formal parameter can also be a tuple. This is how you define functions that take variable number of arguments. The tuple formal is bound to the arguments that are "left over" after the other arguments have been bound.
Q1> :(f :x :T :y)= printf "x=%S; T=%S; y=%S" x T y Q2> f 2 3 4 5 6 7 x=2; T=[3 4 5 6]; y=7Only one formal can be a tuple. Note that a tuple formal gives sequence value to the parameter name (T, in the example). This is how it works: The formal
:T@
is unified with the sub-tuple
[3 4 5 6]@
from the actual argument list.
After cancelling the @
, we get the unification:
:T = [2 3 4 5]
.
Note that @
is also use for reduction: See Reduce.
This usage is related: You can think of the reduction
converting the argument sequence to a tuple.