by Charlie Bovaird
I recently came across A Programming Language that can be downloaded to your personal computer with a license for noncommercial users. An Internet search for “DYALOG APL download” will get you started.
I offer the following problem and a solution to illustrate APL’s power and simplicity.
PROBLEM: Write a program to simulate the following process.
Problem specification
Fifty students are required to turn lights on and off according to a set of rules.
The problem is to write a program to solve the problem with a few lines of code.
Problem definition:
(1) initially 50 Electric lights are off along a dark corridor
(2) 50 students (light string pullers) will go down the corridor one at a time
(3) student 1 pulls every string
(4) student 2 pulls every second string
(5) student 3 pulls every third string
(6) And so forth until the 50th student pulls the cord on light 50.
(7) QUESTION: What lights are still lit when the 50th student completes his/her task.
The following program was written in DYALOG APL.
Note: this solution recognizes that for each light an odd number of pulls will leave the light on while an even number of pulls will leave the light off. Lights will be identified by their sequence number along the corridor. Students will be identified by their turn number.
[0] lights ⍝ name of program (function)
[1] S←⍳50 ⍝identify students by numbers 1 to 50
[2] L←⍳50 ⍝ identify lights by numbers 1 to 50
[3] a←S∘.×L ⍝creates 50×50 matrix “a”. Each row identifies lights pulled by that student
[4] b←,a ⍝un-ravels the matrix “a” to a vector of lights pulled.
[5] c←(b>50)/b ⍝eliminates virtual light pulls whose identifier is greater than 50
[6] d←{≢⍵}⌸c ⍝counts the light pulls after the 50th student completes his/her task.
[7] e←2|d ⍝modulo 2 gets binary vector, 1’s indicating odd counts of light pulls
[8] g←e/L ⍝deletes lights with even counts leaving the list of lights that are on
[9] ⎕←g ⍝ displays lights left on
Explanation of APL code
This example was written to assist interpretation by a reader unfamiliar with APL coding.
Each line of code is numbered (in this display format). The variables: a, b, c, d, ,f, g, S, L.
Each variable is specified by the results of the operation on its right.
Data forms are : binary, integer, number, character.
Data formats (shape) : bit, byte, vector, matrix.
In this program we are using integer vectors, an integer matrix, and a binary vector.
APL program lines are parsed from right to left.
primitive functions description
⍝ comments are on the right of this operator
∇ open/close function editor
⍳ creates a list of index numbers
← specify
≤ logical less than or equal
∘.× matrix multiply operation
{≢⍵}⌸ counting operation
| modulo, in this case modulo 2
/ reduce
line [3] Here are the results of the first 4 students:. note: line[5] will remove numbers >50.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20………….50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40……….100
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60………..150
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80…………200
line [6] creates the following vector “d” containing counts of the lights for
each of the 50 light positions in the corridor.
1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6 4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8 2 8 2 6 6 4 2 10 3 6
line [7] generates a length 50 binary vector “e” of ones and zeros where the one’s indicate the lights left on. Note the ones indicate the position of the “odd” counts in the “d” vector.
1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0
line [8] reduces the original set of 50 lights leaving “g”, the lights left on –
which displays: 1 4 9 16 25 36 49
For DACSDOC readers interested in the use of APL in problem solving please send a feedback message to aplstart@www.dacs.org with “aplstart” in title line.