Problem A
APL Lives!
Problem ID: apl
APL is an array programming language that uses a notation invented by Ken Iverson in 1957. In this problem
we consider only a small subset of the language which we call
apl
(that is,
small APL
).
Each
apl
expression appears on a line by itself and each expression has a value, which is displayed immediately
after the expression is entered. Operators in
apl
do not have precedence like those in C, C++, or Java, but
instead are applied right to left. However, parentheses may be used to control evaluation order. Similarly,
operands for binary operators are evaluated in right to left order. Here are some examples of
apl
expressions.
var = 1 2 3
Store the vector
1 2 3
in
var
, replacing its previous value. The
value of the expression is
1 2 3.
The left operand of the =
operator must be a variable.
var + 4
Display the value of
var
with 4 added to each of its elements
(result:
5 6 7
); the stored version of
var
is not modified.
 / var
Display the value of
var
as if a

operator had been inserted
between each of its elements on each row (result:
2
). If
var
has
two dimensions, the result is a vector. If
var
has three
dimensions, the result is a twodimensional array.
* /
and
+ /
have analogous behaviors.
iota 5
Generate a vector with the values
1 2 3 4 5
.
2 2 rho 1 2 3 4
Reshape the vector
1 2 3 4
into a 2 by 2 array; 1 and 2 are in
the first row, and 3 and 4 are in the second row.
2 2 rho 1 2 3 4 5 6
Same result as above.
2 3 rho 1 2 3 4
Another reshaping, yielding a first row with
1 2 3
and a second
row with
4 1 2
; if the right argument does not have a sufficient
number of elements, then the elements of the right operand are
reused starting from the beginning, in rowmajor order.
2 drop iota 5
Result:
3 4 5
. Drops the two leading elements from
iota 5
.
1 2 * 3 4
Result:
3 8
. Illustrates elementwise multiplication. Operands
must be conformable – either they have the same shape, or at least
one must be a oneelement vector (see second example).
( ( a = 1 ) drop 1 2 3 ) – 5
Result:
3 2
. Illustrates use of parentheses.
a + ( a = 5 ) + a + ( a = 6 )
Result:
22
. Illustrates evaluation order
In this problem you are to write an interpreter for
apl
. Integers in the input are nonnegative and less than 10
4
.
All computed integer values (including intermediate values) have absolute values less than 10
4
. The number of
entries in any matrix is always less than or equal to 10
4
. Variable names consist of one to three alphabetic lower
case characters, and the names
iota
,
rho
, and
drop
are always interpreted as operators. Exactly one space
separates elements of statements (constants, variables, operators, and parentheses).
Constants in the input are vectors. All intermediate values are one, two, or threedimensional arrays with
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 q
 Topographic map, Test Case, Sample Input Case

Click to edit the document details