Concepts of Programming Languages

(Sean Pound) #1

266 Chapter 6 Data Types


6.5.5 Array Operations


An array operation is one that operates on an array as a unit. The most com-
mon array operations are assignment, catenation, comparison for equality and
inequality, and slices, which are discussed separately in Section 6.5.5.
The C-based languages do not provide any array operations, except
through the methods of Java, C++, and C#. Perl supports array assignments
but does not support comparisons.
Ada allows array assignments, including those where the right side is
an aggregate value rather than an array name. Ada also provides catenation,
specified by the ampersand (&). Catenation is defined between two single-
dimensioned arrays and between a single-dimensioned array and a scalar.
Nearly all types in Ada have the built-in relational operators for equality and
inequality.
Python’s arrays are called lists, although they have all the characteristics
of dynamic arrays. Because the objects can be of any types, these arrays are
heterogeneous. Python provides array assignment, although it is only a refer-
ence change. Python also has operations for array catenation (+) and element
membership (in). It includes two different comparison operators: one that
determines whether the two variables reference the same object (is) and one
that compares all corresponding objects in the referenced objects, regardless
of how deeply they are nested, for equality (==).
Like Python, the elements of Ruby’s arrays are references to objects. And
like Python, when a == operator is used between two arrays, the result is true
only if the two arrays have the same length and the corresponding elements are
equal. Ruby’s arrays can be catenated with an Array method.
Fortran 95+ includes a number of array operations that are called elemen-
tal because they are operations between pairs of array elements. For example,
the add operator (+) between two arrays results in an array of the sums of the
element pairs of the two arrays. The assignment, arithmetic, relational, and
logical operators are all overloaded for arrays of any size or shape. Fortran 95+
also includes intrinsic, or library, functions for matrix multiplication, matrix
transpose, and vector dot product.
F# includes many array operators in its Array module. Among these are
Array.append, Array.copy, and Array.length.
Arrays and their operations are the heart of APL; it is the most powerful
array-processing language ever devised. Because of its relative obscurity and its
lack of effect on subsequent languages, however, we present here only a glimpse
into its array operations.
In APL, the four basic arithmetic operations are defined for vectors
(single-dimensioned arrays) and matrices, as well as scalar operands. For
example,

A + B

is a valid expression, whether A and B are scalar variables, vectors, or
matrices.
Free download pdf