Mastering Dyalog Apl A Complete Introduction To Dyalog Apl Bernard Legrand Download
Mastering Dyalog Apl A Complete Introduction To Dyalog Apl Bernard Legrand Download
https://ebookbell.com/product/mastering-dyalog-apl-a-complete-
introduction-to-dyalog-apl-bernard-legrand-4978886
https://ebookbell.com/product/mastering-collaboration-in-a-product-
team-70-techniques-to-help-teams-build-better-products-1st-edition-
natasha-hampshire-glaudia-califano-david-spinks-44875474
https://ebookbell.com/product/mastering-collaboration-in-a-product-
team-70-techniques-to-help-teams-build-better-products-1st-edition-
natasha-hampshire-44915468
https://ebookbell.com/product/mastering-gnome-a-beginners-guide-1st-
edition-sufyan-bin-uzayr-46082774
https://ebookbell.com/product/mastering-flutter-a-beginners-guide-1st-
edition-sufyan-bin-uzayr-46082778
Mastering Rust A Beginners Guide 1st Edition Sufyan Bin Uzayr
https://ebookbell.com/product/mastering-rust-a-beginners-guide-1st-
edition-sufyan-bin-uzayr-46151622
https://ebookbell.com/product/mastering-rust-a-beginners-guide-1st-
edition-sufyan-bin-uzayr-46191750
https://ebookbell.com/product/mastering-primary-science-amanda-
mccrory-kenna-worthington-46236134
https://ebookbell.com/product/mastering-ubuntu-server-explore-the-
versatile-powerful-linux-server-distribution-ubuntu-2204-with-this-
comprehensive-guide-4th-edition-4th-jay-lacroix-46242660
https://ebookbell.com/product/mastering-python-scripting-for-system-
administrators-ganesh-sanjiv-naik-46242670
Mastering Dyalog APL
A Complete Introduction to Dyalog APL
Bernard Legrand
TRADEMARKS
Dyalog Limited
http://www.dyalog.com
ISBN : 978-0-9564638-0-7
iii
Contents
Array Processing 5
More Symbols 7
Indexing 11
FAQ 28
iv Contents
2 - Arrays of Items 47
2.1 Create a List or a Matrix 47
2.2 Special Cases with Reshape 48
2.3 Multi-dimensional Arrays 49
5 - Indexing 56
5.1 Traditional Vector Indexing 56
5.2 The Shape of the Result 57
5.3 Array Indexing 58
5.4 Convention 60
5.5 Warnings 61
5.6 The Index Function 62
Contents v
7 - Empty Arrays 70
1 - Definitions 89
3 - Order of Evaluation 94
vi Contents
7 - Reduction 104
7.1 Presentation 104
7.2 Definition 105
7.3 Reduction of Binary Data 106
7.4 Reduction of Nested Arrays 107
7.5 Application 1 107
7.6 Application 2 108
10 - Concatenation 113
10.1 Concatenating Vectors 113
10.2 Concatenating Other Arrays 114
10.3 Concatenating Scalars 117
10.4 Special Notations 117
11 - Replication 118
11.1 Basic Approach: Compression 118
11.2 Replication 120
11.3 Scalar Left Argument 120
11.4 Special Notations 121
14 - Ravel 132
1 - Landmarks 147
1.1 Some Definitions 147
1.2 Configure Your Environment 148
9 - Recursion 217
10 - Synonyms 218
Contents ix
12 - SALT 225
Exercises 227
1 - Execute 273
1.1 Definition 273
1.2 Some Typical Uses 274
1.3 Make Things Simple 276
2 - Laminate 305
2.1 Application to Vectors and Scalars 307
2.2 Applications 308
3 - Expand 310
3.1 Basic Use 310
3.2 Extended Definition 310
3.3 Expand Along First Axis 311
5 - Rotate 314
5.1 Rotate Vectors 314
5.2 Rotate Higher-Rank Arrays 315
Contents xi
3 - Each 342
3.1 Definition and Examples 342
3.2 Three Compressions! 345
8 - Pick 361
8.1 - Definition 361
8.2 - Beware! 362
8.3 - Important 363
8.4 - Selective Assignment 364
8.5 - An Idiom 365
11 - Enlist 369
Exercises 371
1 - Definitions 377
1.1 Operators & Derived Functions 377
1.2 Sequences of Operators 378
1.3 List of Built-in Operators 379
3 - Scan 383
3.1 Definition 383
3.2 Scan with Binary Values 384
3.3 Applications 385
6 - Compose 410
6.1 Form 1 411
6.2 Form 2 412
6.3 Form 3 412
6.4 Form 4 413
7 - Commute 414
9 - Spawn 418
9.1 Main Features 418
9.2 Special Syntax 420
5 - Domino 452
5.1 Some Definitions 452
5.2 Matrix Inverse 453
5.3 Matrix Division 455
5.4 Two or Three Steps in Geometry 455
5.5 Least Squares Fitting 457
Exercises 461
Contents xv
1 - Overview 473
1.1 Commands, System Variables, and System Functions 473
1.2 Common Properties 474
1.3 Organisation 475
9 - Miscellaneous 507
9.1 ⎕OFF & )OFF Quit APL 507
9.2 ⎕SH, ⎕CMD, )SH & )CMD Host System Commands 507
9.3 ⎕PW Page Width 508
1 - Guidelines 603
1.1 Terminology and Options 603
1.2 Create a Simple Dialog Box 607
1.3 Get Information 610
1.4 Changing Properties 611
1.5 Make It Work 612
6 - Menus 644
1 - Introduction 675
1 - Introduction 693
1.1 Why a Source Code Management System? 693
1.2 Using Script Files 697
1.3 Updating a Script From the APL Session 700
3 - Settings 709
1 - NewLeaf 714
1.1 Getting Started 714
1.2 Frames and Text 715
1.3 Fonts 720
1.4 Tables 722
1.5 The Page Designer 726
1.6 More Tools, Better Quality 735
2 - RainPro 738
2.1 Getting started 738
2.2 Multiple Bar Chart 740
2.3 Scattered Points 744
2.4 Min-Max Vertical Lines 750
2.5 Polar Representations 753
2.6 Multiple Charts 754
2.7 There is Much More To Explore! 756
Contents xxi
APPENDICES 773
INDEX M-789
xxii Contents
1
You might like to refer to section A-1 for additional installation hints.
As you will see in the following pages, APL uses special symbols, like ⍉, ∰, and ∸, which
you enter using a special keyboard layout.
You will not need all of the special symbols to read the following pages. The picture below
illustrates a cut-down version of the UK keyboard layout, with only the APL symbols that are
referred to in this introduction. They are shown on a grey background. The US keyboard is
slightly different, but the symbols we need are positioned identically. The full keyboard layout
is shown in the User Guide.
Once the workspace has been loaded, a welcome message is displayed, and you can check that
the workspace contains the variables we shall be using in the following pages:
)vars
Actual Ages AlphLower AlphUpper Area Big Category etc…
You can display the contents of any variable by hovering over its name with the mouse-
pointer, by double-clicking on its name, or just by typing its name and pressing the Enter key,
like this:
Forecast
150 200 100 80 80 80
300 330 360 400 500 520
100 250 350 380 400 450
50 120 220 300 320 350
Variables
As in any programming language, it is possible to create variables. Just choose a name and use
the left arrow to assign it a value. In APL a numeric value can consist of a single number, or
several numbers separated by at least one blank space. The arrow can be obtained using Ctrl+ [
VAT ← 19.6 Read it as: VAT gets 19.6
Years ← 1952 1943 1986 2007
The names are "case sensitive". It means that three variables named respectively VAT, Vat,
and vat, would be distinct, and may contain different values.
To ask for the contents of a variable, just type its name and press Enter, like this:
VAT
19.6
Years
1952 1943 1986 2007
Array Processing
APL is able to operate on two sets of numbers, provided those two sets have the same "shape".
For the moment, understand this as "the same number of items". For example, suppose that
you have a list of prices of 5 products, and the quantity bought of each:
Prices 5.20 11.50 3.60 4.00 8.45
Quantities 2 1 3 6 2
You can create two variables like this:
Price ← 5.2 11.5 3.6 4 8.45
Qty ← 2 1 3 6 2
When multiplied together, the variables are multiplied item by item, and produce a result of
the same length. That result can be assigned to a new variable.
Costs ← Price ∲ Qty
Costs
10.4 11.5 10.8 24 16.9
This array processing capability eliminates most of the "loops" which are common to other
programming languages. This remains true even if the data is not a simple list but a multi-
dimensional array, of almost any size and number of dimensions.
6 Dyalog APL - Tutorial
To make it clear, imagine that a Sales Director makes forecasts for sales of 4 products over the
coming 6 months, and assigns them to the variable Forecast. At the end of the 6 months, he
assigns the real values to the variable Actual. Here they are:
Forecast Actual
150 200 100 80 80 80 141 188 111 87 82 74
300 330 360 400 500 520 321 306 352 403 497 507
100 250 350 380 400 450 118 283 397 424 411 409
50 120 220 300 320 350 43 91 187 306 318 363
We have not yet explained how you can build such arrays of data, but if you have APL
installed, these variables are provided in the Workspace file named "DyalogTutor_EN.dws".
Refer to the "Utilities and Data" section above to see how you can load the workspace and
access the data.
It is clear that the first idea of any Sales Director will be to ask for the differences between
what he expected and what he has really got. This can be done easily by typing:
Actual - Forecast
¯9 ¯12 11 7 2 ¯6 Note that to distinguish the sign attached to
21 ¯24 ¯8 3 ¯3 ¯13 negative values from subtraction, negative
18 33 47 44 11 ¯41 values are shown with a high minus sign.
¯7 ¯29 ¯33 6 ¯2 13
To enter negative values, this high minus sign can be obtained by pressing Ctrl+2.
In most traditional programming languages an operation like the one above requires two
embedded loops. See what is needed in PASCAL:
DO UNTIL I=4
DO UNTIL J=6
DIFF(I,J):=ACTUAL(I,J)-FORECAST(I,J)
END
END.
Even if this may seem obvious to a programmer, it is worth noting that most of the code has
nothing to do with the user requirement. The only important thing (subtract forecasts from
actual values) is hidden behind the detailed workings of the computer program.
To have a calculation done by a machine, one must translate our human wording into
something that the computer can understand. With traditional languages, most of that effort is
made by the man, to produce a program like the PASCAL example above. The great
advantage of APL is that the man has generally much less effort to make, and the machine
does the rest.
We have seen that APL will work on two variables of the same shape; it also works if one of
the variables is a single item, which is called a scalar. If so, the other variable may be of any
shape.
Introduction - Will you play APL with me ? 7
For example, if we want to calculate the amount of 19.6% VAT applied to the variable Price
above, we can type Price × VAT ÷ 100 (or VAT × Price ÷ 100 as well), as shown here:
Price × VAT ÷ 100
1.0192 2.254 0.7056 0.784 1.6562 This result would require some rounding
but this is not important for now
More Symbols
Most programming languages represent only a very small subset of the mathematical
functions using symbols (typically +, -, * and /). The creator of APL, Kenneth E. Iverson,
chose to include many traditional mathematical symbols in his language, and also added some
new symbols to the set that we already know so well.
E.g.: Many functions which in other programming languages are library routines with names
like "Maximum" have their own symbols in APL.
The function "Maximum" (⌈) returns the greater of two numbers, or of two arrays of numbers
compared item by item.
There is also, as one might expect, a symbol for "Minimum" (⌊).
75.6 ⌈ 87.3 Maximum (Ctrl+S)
87.3
11 28 52 14 ⌈ 30 10 50 20 Comparison item by item
30 28 52 20
11 28 52 14 ⌊ 20 Minimum (Ctrl+D)
11 20 20 14
APL supports about 70 symbols. Since some symbols have more than one meaning one could
argue at length about the exact number.
This is nothing to worry about: Some of the symbols are familiar; such as × or > or again ÷
and -, but also ! and a good many others.
8 Dyalog APL - Tutorial
Used dyadically, the same symbol will organise items into a specified shape. For example,
suppose that we want to create the matrix below:
25 60
33 47
11 44
53 28
We must give the computer two pieces of information:
First the shape to give to the matrix: 4 2 (4 rows of 2 columns)
Next the contents of the matrix: 25 60 33 47 11 44 53 28
It is the symbol ∰ (Rho) which makes the connection between the shape and the contents:
Tab ← 4 2 ∰ 25 60 33 47 11 44 53 28
Tab
25 60
33 47
11 44
53 28
A new variable Tab is thereby created, and this is also how the variables Forecast and
Actual above were made.
Introduction - Will you play APL with me ? 9
Conventions
In APL, we give special names to certain shapes of data:
Scalar is used for a single value, a number like 456.18 or a single letter like 'Q'.
Vector is a plain list of values
It may be composed of numbers like Price and Qty,
or of letters like 'Once upon a time' within single quotes
Matrix is an array with two dimensions, like Forecast or Tab
Array is a generic word for any set of values, whatever the number of its dimensions
Table is a common word used for arrays with 2 dimensions (matrices)
Cube is a common word used for arrays with 3 dimensions
Similarly, if we write ⌈/ 21 45 18 27 11
- it is as if we had written 21 ⌈ 45 ⌈ 18 ⌈ 27 ⌈ 11
- so, we obtain the largest term 45
Let us also write two little dyadic functions, the left argument of which is ∭, and the right is ∱:
Plus ← {∭+∱}
Times ← {∭∲∱}
(3 Plus 6) Times (5 Plus 2)
63
As you can see, these functions behave exactly as if we had written (3+6) × (5+2)
We said in the preceding section that a user-defined program could be used by the Reduce
operator; let us try:
Plus/ Val
218 It works!
Indexing
Returning to our vector of numbers Val: 22 37 41 19 54 11 34
In order to extract the 4th item, we just write: Val[4]
In many other programming languages one uses parentheses instead of brackets; this is not
very different.
What is new is that one can extract several items in one instruction.
Val
22 37 41 19 54 11 34
Val[2 4 7 1 4] One may extract the same item twice or more
37 19 34 22 19
12 Dyalog APL - Tutorial
And of course, in the same way, one may modify one or more items of Val using their
indexes. Naturally, one must provide as many values as there are items to modify, or a single
value for all:
Val[3 5 1] ← 0
Val
0 37 0 19 0 11 34
Val[3 5 1] ← 300 77 111
Val
111 37 300 19 77 11 34 You can check that the 3rd item is now 300, the 5th is 77, etc.
It is often necessary to extract the first few items from a list of values, for example the first 5.
Nothing could be easier:
Val[1 2 3 4 5]
111 37 300 19 77
But if one needs to extract the first 500 items from a long list, typing the integers from 1 to
500 would of course be very inconvenient.
This is why APL has been given the symbol ∯ (Iota), which produces the set of the first n
integers (∯ can be obtained using Ctrl+I)
Thus, instead of writing 1 2 3 4 5 6 7 8, it is sufficient to write ∯8.
And to extract the first 500 terms of a large vector, one may write: Big[∯500]
We shall discover later an even simpler method.
Suggested
Category increase
1 8%
2 5%
3 2%
Naturally one can operate on this binary data using all the functions of Boolean algebra, and
moreover, the symbols used are those familiar to mathematicians of all nationalities around
the world:
Function AND is represented by the symbol ∧ (represented by the word AND in
many programming languages)
Function OR is represented by the symbol ∨ (represented by the word OR in
these languages)
Thus, if I am looking for people in category 3 whose salary is less than 4000 euros, I can
write:
(Categories = 3) ^ (Salaries < 4000)
0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1
In fact APL offers all the functions of Boolean algebra, including some perhaps less familiar
functions like NOR and NAND (Not-OR and Not-AND), but they are very useful in finance
and electronic automation.
There is, however, no special symbol for the function Exclusive OR (often called XOR). This
is because it is not needed: The function Not Equal ≠ gives the same result as Exclusive OR
when it is used with Boolean values, as you can see below:
0 0 1 1 ≠ 0 1 0 1
0 1 1 0
Finally, not only can these binary vectors be used as we have described but also for novel
purposes, such as counting and selecting.
Introduction - Will you play APL with me ? 15
Counting
Having found which salaries are less than 2500 euros by means of the following expression:
Salaries < 2500
0 1 0 1 1 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0
It is easy to add all the 1s and 0s to calculate how many people earn less than 2500 euros:
+/ Salaries < 2500
8
Selection
One can also use the binary vector as a "mask" to select the items corresponding to the binary
"1"s from another array:
1 1 0 1 0 0 1 / 23 55 17 46 81 82 83
23 55 46 83
The procedure is identical for character data:
1 0 1 0 0 0 0 1 1 / 'Drumstick'
Duck
This function, called Compress, is particularly useful for extracting the items conforming to a
given criterion from a variable. For example, to display the salaries of people in Category 2,
one writes:
(Categories = 2) / Salaries
2240 2076 2735 3278 1339 3319 Powerful, isn’t it?
Discovery
To practise our skills some more, let us find in our variable Val the positions of numbers
greater than 35. Here are the necessary steps:
Val ← 22 37 41 19 54 11 34
Val>35 is 0 1 1 0 1 0 0
∰Val is 7
∯∰Val is 1 2 3 4 5 6 7 All possible positions
Let us compare two of these results
Val>35 0 1 1 0 1 0 0
∯∰Val 1 2 3 4 5 6 7
16 Dyalog APL - Tutorial
You can see that that if you eliminate (using Compress) the items which correspond to zeros
in order to retain only those corresponding to 1, you easily get the positions required: 2 3 5
Thus the job may be done as follows:
(Val>35) / ∯∰Val
2 3 5
This expression is applicable in many different situations.
Here is a similar use, but applied to character data: To find the positions of "a" within a
phrase; the method is the same.
Phrase ← 'Panama is a canal between Atlantic and Pacific'
(Phrase = 'a') / ∯∰Phrase
2 4 6 11 14 16 30 36 41 You can check it!
So we shall use a new function: Membership which is represented by the symbol ∮, also used
in mathematics.(∮ can be obtained by pressing Ctrl+E)
The expression A ∮ B returns a Boolean result which indicates which items of the variable A
appear in the variable B, wherever they may be. And it works no matter what are the shapes,
the dimensions or the type (numeric or character) of A and B, a pure marvel!
For example:
5 7 2 8 4 9 ∮ 3 4 5 6
1 0 0 0 1 0 Only 5 and 4 are found in 3 4 5 6
'dandelion' ∮ 'garden'
1 1 1 1 1 0 0 0 1 The letters "lio" do not appear in "garden"
1
"Y" is considered to be a vowel in many European languages.
Introduction - Will you play APL with me ? 17
So in pursuit of our enquiry we shall write:
(Phrase ∮ 'aeiouy') / ∯∰Phrase
2 4 6 8 11 14 16 20 23 24 30 33 36 41 43 45
One can also use membership between a vector and a matrix, as shown below, assuming that
the list of towns is a variable created earlier.
We have represented side by side the variable itself and the result of using Membership:
Towns Towns ∮ 'aeiouy'
Canberra 0 1 0 0 1 0 0 1 0 0
Paris 0 1 0 1 0 0 0 0 0 0
Washington 0 1 0 0 1 0 0 0 1 0
Moscow 0 1 0 0 1 0 0 0 0 0
Martigues 0 1 0 0 1 0 1 1 0 0
Mexico 0 1 0 1 0 1 0 0 0 0
We can reverse the expression, but the result has always the same shape as the left argument:
'aeiouy' ∮ Towns
1 1 1 1 1 0 None of the town names contains a "y"
It is true that 29, 63, 40 and 33, occur respectively in positions 8, 3, 2 and 7. It's much better!
But, first surprise: The value 40 occurs 3 times in List, but only the first one is reported in
the result. This is because, by definition, dyadic Iota returns only the first occurrence of a
given item. If the response for each value sought has to match a position; how may one,
looking for 5 numbers, obtain 7 results?
Second surprise: The value 50 is reported as being found in position 11 in a vector comprising
only 10 items! This is how the function IndexOf (dyadic ∯) reports that a value is absent.
At first sight this seems a bit surprising, but in fact it is a property which makes this function
so generally powerful, as we shall soon see.
An Example
A car manufacturer decides that he will offer his customers a discount on the catalogue price
(you can see how this example is imaginary!)
The country has been divided into 100 areas, and the discount rate will depend on the area
according to the following table:
Area Discount
17 9%
50 8%
59 6%
84 5%
89 4%
Others 2%
The problem is to calculate the discount rate that may be claimed for a potential customer who
lives in given area D; for example D ← 84.
Let us begin by creating two variables:
Area ← 17 50 59 84 89
Discount ← 9 8 6 5 4 2
Let us see if 84 is in the list of favoured areas:
D ∮ Area
1 Yes, it's there
Area ∯ D
4 84 is the 4th item in the list
Introduction - Will you play APL with me ? 19
Let us find the current rate of discount for this index position:
Discount[4]
5 This customer can claim a 5% discount; good!
One may simply write: Discount[Area∯D]
If a customer lives in any area such as 75, 45, or 93, the expression Area∯D will in all cases
give the result 6, because those values are absent in Area. Then Discount[6] will always
find the rate 2%, as expected.
The importance of this approach is that it is vector-based. Suppose that publicity attracts
crowds and that therefore D is no longer a single value but a vector, the solution is still valid:
D ← 24 75 89 60 92 50 51 50 84 66 17 89
Discount[Area∯D]
2 2 4 2 2 8 2 8 5 2 9 4
All that without a program, neither "loop" nor "test", and whatever the number of areas.
Readers who know other programming languages will have no difficulty in making the
comparison.
Generalisation
In truth, the expression we just wrote is an example of an algorithm for "changing the frame of
reference". Don’t panic, the name may seem esoteric, but the concept is simple. A list of area
numbers (the initial set) is translated into a list of discount rates (the final set).
Let us now imagine the initial set to be an alphabet composed of lower case and upper case
letters, and the final set to be composed of only upper case letters (with a blank space in the
middle):
AlphLower
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
AlphUpper
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ*
Notice that AlphUpper is one character longer than AlphLower. We have added an asterisk
at the end, and you will see why we did so:
Here is a little French sentence, with one accented letter.
Tale ← 'Le Petit Chaperon-Rouge a bouffé le Loup'
The expression below converts from lower to upper case.
AlphUpper[AlphLower∯Tale]
LE PETIT CHAPERON*ROUGE A BOUFF* LE LOUP
20 Dyalog APL - Tutorial
As one might expect, the characters – and é, which are absent from the initial alphabetic set
have been replaced by the * of the final set, but the conversion is acceptable. This solution can
easily be improved.
Once more, the rational steps to be taken to create a solution are easily translated into a
programming algorithm, and the programmer can thereby get a much more extensive insight
into the problem itself.
Top-bottom Swap
Initial Variable Left-right reverse reverse Rows & Columns
(Mirror)
(Mirror) (Transpose)
× 1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10 12 14 16 18
3 3 6 9 12 15 18 21 24 27
4 4 8 12 16 20 24 28 32 36
etc. etc.
× 8 5 15 9 11 40
5 40 25 75 45 55 200
4 32 20 60 36 44 160
10 80 50 150 90 110 400
3 24 15 45 27 33 120
1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 2 3 4 5 2 3 4 5 6
0 1 0 0 0 0 0 1 1 1 1 1 0 0 0 2 2 3 4 5 3 4 5 6 7
0 0 1 0 0 0 0 0 1 1 1 1 1 0 0 3 3 3 4 5 4 5 6 7 8
0 0 0 1 0 0 0 0 0 1 1 1 1 1 0 4 4 4 4 5 5 6 7 8 9
0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 5 5 5 5 5 6 7 8 9 10
24 Dyalog APL - Tutorial
A Useful Application
Suppose the vector Ages contains the ages of 400 respondents to an opinion poll. We want to
establish how many people there are in each of the following categories:
0 - 25 - 30 - 35 - 45 - 50 - 55 - 65 or above.
Here is an extract of the data:
Ages 32 19 50 33 23 65 46 26 31 58 51 23 51 36 28 42 ... etc
Category 0 25 30 35 45 50 55 65
We are going to use the Outer Product Category ∬.< Ages , and here are the first items of
the result:
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
25 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 1 1
30 1 0 1 1 0 1 1 0 1 1 1 0 1 1 0 1 1
35 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 0
45 0 0 1 0 0 1 1 0 0 1 1 0 1 0 0 0 0
50 0 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 … etc.
etc.
If one adds up this Boolean matrix, one obtains for each row the number of people who are
older than 0 years, older than 25 years, older than 30 years, etc. This is the expression:
cum ← +/ (Category ∬.< Ages)
With the cut-down extract shown above, the value of cum would be: 17 14 12 8 6 4
In other words there are 12 people older than 30. But among them, 8 are older than 35. In
order to know how many people are between 30 and 35, it is necessary to calculate 12-8 to
obtain 4.
If one wants to reproduce this calculation for all categories, it is necessary to perform a series
of subtractions as here:
17 14 12 8 6 4 This is cum
- 14 12 8 6 4 0 This is cum without its first item and followed by zero
-------------- Let us subtract
= 3 2 4 2 2 4 This result was obtained by the calculation cum-(1∸cum,0)
To append a zero to the right, we used a comma, which joins variables together. This is a
function called Catenate.
If one no longer works with a small extract of data, but with the full list of 400 people, this is
what one gets:
Introduction - Will you play APL with me ? 25
cum ← +/ (Category ∬.< Ages)
cum - (1∸cum,0)
56 32 56 104 63 38 37 14
All that without real programming, and it works whatever the number of people or categories.
What luck!
Once again, APL allowed us to find straightforward and original solutions to traditional
problems.
× works on 5 (the value immediate to the left of it) and the result of 3 + 2, the entire
expression to the right of it. Even though it is not strictly correct, many people say that APL
evaluates from right to left. In any case, the result of the expression is 25 in APL!
Had we written (5 × 3) + 2 instead the result would of course have been 17.
It may take a little while to get used to this slightly unfamiliar rule, but once it has been
learned it is really a great advantage because you can direct your energy towards solving your
problem and not have to remember complex rules just to satisfy the computer's need for
guidance.
And Also…
You can interface APL with Internet, write your own web server, use multithreading to
process simultaneous tasks, use all the advantages of true Object Oriented Programming, and
use many attractive features, which are beyond the scope of this quick survey.
28 Dyalog APL - Tutorial
FAQ
Perhaps you have found this language rather engaging, but before you decide to invest time
and energy in developing APL applications, you would like to be sure of your choice. Let's
give you some answers.
Plate
IV.
The Use of the Net.—In the use of the net the old saying is true
that "practice makes perfect." The bag of the net should be
sufficiently long to allow of its being completely closed when
hanging from the ring on either side. It is possible to sweep into the
net an insect which is fluttering through the air, and then by a turn
of the hand to close the bag and to capture the specimen. When the
insect has alighted upon the ground it is best to clap the net over it
and then to raise the net with one hand. Very many species have the
habit of flying upward. This is particularly true of the skippers, a
group of very vigorous and swift-flying butterflies. The writer
prefers, if possible, to clap the net over the specimens and then to
allow them to rise, and, by inserting the wide-mouthed collecting-jar
below, to capture them without touching them at all with the fingers.
So far as possible the fingers should not be allowed to come in
contact with specimens, whether in or out of the net, though some
persons acquire an extremely delicate yet firm touch which enables
them to handle the wings of frail species without removing any of
the scales. Nothing is more unsightly in a collection than specimens
that have been caught and rubbed by the fingers.
Baits.—Moths are frequently taken by the method of collecting
known as "sugaring." But it may also be employed for butterflies. For
this purpose a mixture of beer and cheap brown sugar may be used.
If the beer be stale drippings, so much the better. In fact, it is well, if
the collector intends to remain in one locality for some time, to make
a mixture of beer and sugar some hours or a day in advance of its
application. In semi-tropical countries a mixture of beer and sugar is
hardly as good as a mixture of molasses and water into which a few
tablespoonfuls of Jamaica rum have been put. A mixture thus
prepared seems to attract more effectually than the first
prescription. Having provided a pail with a quart or two of the
mixture, the collector resorts to the point where he proposes to
carry on his work. With an ordinary whitewash brush the mixture is
applied to the trunks of trees, stumps, fence-rails, and other objects.
It is well to apply the mixture to a series of trees and posts located
on the side of a bit of woodland, or along a path through forests, if
comparatively open and not too dense. The writer has rarely had
success in sugaring in the depths of forests. His greatest success has
always been on paths and at the edge of woods. Many beetles and
other insects come to the tempting sweets, and separate jars for
capturing these should be carried in the pocket. The collector never
should attempt to kill beetles in the same jar into which he is putting
butterflies. The hard, horny bodies and spiny legs of beetles will
make sad havoc with the delicate wings of butterflies.
Many other baits besides this may be employed to attract insects.
Some writers recommend a bait prepared by boiling dried apples
and mashing them into a pulp, adding a little rum to the mixture,
and applying this to the bark of trees. In tropical countries bananas,
especially rotten bananas, seem to have a charm for insects. The
cane-trash at sugar-mills is very attractive. If possible, it is well to
obtain a quantity of this trash and scatter it along forest paths. Some
insects have very peculiar appetites and are attracted by things
loathsome. The ordure of carnivorous animals seems to have a
special charm for some of the most magnificently colored and the
rarest of tropical butterflies. A friend of mine in Africa, who collected
for me for a number of years, used to keep civet-cats, the ordure of
which was collected and placed at appropriate points in the forest
paths; and he was richly rewarded by obtaining many insects which
were not obtained in any other way. Putrid fish have a charm for
other species, and dead snakes, when rankly high, will attract still
others. It may be observed that after the trees have been treated for
a succession of days or nights with the sweetening mixture spoken
of above, they become very productive. When collecting in Japan I
made it a rule to return in the morning to the spots that I had
sugared for moths the evening before, and I was always amply
repaid by finding multitudes of butterflies and even a good many
day-flying moths seated upon the mossy bark, feasting upon the
remnants of the banquet I had provided the evening before. There is
no sport—I do not except that of the angler—which is more
fascinating than the sport derived by an enthusiastic entomologist
from the practice of "sugaring." It is well, however, to know always
where your path leads, and not to lay it out in the dusk, as the
writer once did when staying at a well-known summer resort in
Virginia. The path which he had chosen as the scene of operations
was unfortunately laid, all unknown to himself, just in the rear of the
poultry-house of a man who sold chickens to the hotel; and when he
saw the dark lantern mysteriously moving about, he concluded that
some one with designs upon his hens was hidden in the woods, and
opened fire with a seven-shooter, thus coming very near to
terminating abruptly the career of an ardent entomologist.
Beating.—There are many species which are apparently not
attracted by baits such as we have spoken of in the preceding
paragraph. The collector, passing through the grove, searches
diligently with his eye and captures what he can see, but does not
fail also with the end of his net-handle to tap the trunks of trees and
to shake the bushes, and as the insects fly out, to note the point
where they settle, and then make them his prey. It is well in this
work, as in all collecting, to proceed somewhat leisurely, and to keep
perfectly cool. The caricature sometimes found in newspapers of the
ardent lepidopterist running like a "quarter-back" across a ten-acre
lot in quest of some flying insect does not represent the truly skilful
collector, whose movements are more or less stealthy and cautious.
THE BREEDING OF SPECIMENS
By breeding it is possible to obtain specimens in the most perfect
condition. Bred specimens which have not had an opportunity to fly
are always preferred on account of their freshness of color and
perfection of form. A great many species which apparently are
exceedingly rare may often be obtained in considerable numbers by
the process of breeding, the caterpillar being more readily found
than the perfect insect. Although the process of breeding involves a
good deal of labor and care, it affords a most delightful field for
observation, and the returns are frequently of the very greatest
value.
How to Get the Eggs of Butterflies.—The process of breeding may
begin with the egg. The skilful eye of the student will detect the
eggs of butterflies upon the leaves upon which they have been
deposited. The twig may be cut and placed in a vase, in water, and
kept fresh until the minute caterpillar emerges, and then from time
to time it may be transferred to fresh leaves of the same species of
plant, and it will continue to make its moults until at last it is
transformed into a chrysalis, and in due season the butterfly
emerges. Eggs may frequently be obtained in considerable numbers
by confining the female under gauze, with the appropriate food-
plant. A knowledge of the food-plant may often be obtained by
watching the female and observing upon what plants she deposits
her eggs. The exceedingly beautiful researches of Mr. W.H. Edwards
were largely promoted by his skill in inducing females to oviposit
upon their food-plants. He did this generally by confining the female
with the food-plant in a barrel or nail-keg, the bottom of which had
been knocked out, and over the top of which he tied mosquito-
netting. The plant was placed under the keg. The insects thus
confined may be fed with a mixture of honey and water placed upon
the leaves.
In collecting caterpillars it is well to have on hand a number of
small boxes in which to place them, and also a botany-box in which
to bring from the field a supply of their appropriate food.
The process of breeding may begin with the caterpillar. The
collector, having discovered the caterpillar feeding upon the branch
of a certain plant, provides the creature with a constant supply of
the fresh foliage of the same plant, until it finally pupates.
Breeding-Cages.—Various devices for
breeding caterpillars and rearing moths and
butterflies are known. One of the most
important of these devices is the breeding-
cage, which is sometimes called a vivarium.
The simplest form of the vivarium is often
the best. In breeding some species the best
method is simply to pot a plant of the
species upon which the larva is known to
feed, and to place the potted plant in a box Fig. 49.—Cheap form of
over which some mosquito-netting is tied. breeding-cage: G, lid
The writer frequently employs for this covered with mosquito-
purpose cylinders of glass over the top of netting; E, pan of
which perforated cardboard is placed. This earth; B, bottle for
method, however, can be resorted to only food-plant.
with the more minute forms and with plants
that do not attain great height. Another
form of vivarium is represented in the adjoining woodcut (Fig. 50).
The writer has successfully employed, for breeding insects upon a
large scale, ordinary store boxes provided with a lid made by
fastening together four pieces of wood, making a frame large
enough to cover the top of the box, and covering it with gauze. The
food-plant is kept fresh in bottles or jars which are set into the
boxes. Be careful, however, after you have put the branches upon
which the caterpillars are feeding into the jars, to stuff something
into the neck of the jar so as to prevent the caterpillar from
accidentally getting into the water and drowning himself—a mishap
which otherwise might occur. When breeding is undertaken on a still
larger scale, it may be well to set apart for this purpose a room,
preferably in an outbuilding, all the openings leading from which
should be carefully closed so as to prevent the escape of the
caterpillars.
Fig. 57.—Setting-board
with moth expanded
upon it (Riley).
Fig. 58.—Butterfly
pinned on board,
showing method of
holding up body and
pinning down antennæ.
Plate
V.
Fig. 61.—Apparatus for inflating
larvæ: B, foot-bellows; K, rubber
tube; C, flask; D, anhydrous
sulphuric acid; E, overflow-flask; F,
rubber tube from flask; G, standard
with cock to regulate flow of air; H,
glass tube with larva upon it; I,
copper drying-plate; J, spirit-lamp.
"When the skins have been inflated they may be mounted readily
by being placed upon wires wrapped with green silk, or upon
annealed aluminium wire. The wires are bent and twisted together
for a short distance and then made to diverge. The diverging ends
are pressed together, a little shellac is placed upon their tips, and
they are then inserted into the opening at the anal extremity of the
larval skin. Upon the release of pressure they spread apart, and after
the shellac has dried the skin is firmly held by them. They may then
be attached to pins by simply twisting the free end of the wire about
the pin, or they may be placed upon artificial imitations of the leaves
and twigs of their appropriate food-plants."
THE PRESERVATION AND ARRANGEMENT OF COLLECTIONS
The secret of preserving collections of lepidoptera in beautiful
condition is to exclude light, moisture, and insect pests. Light
ultimately bleaches many species, moisture leads to mould and
mildew, and insect pests devour the specimens. The main thing is
therefore to have the receptacles in which the specimens are placed
dark and as nearly as possible hermetically sealed and kept in a dry
place. In order to accomplish this, various devices have been
resorted to.
Boxes.—Boxes for the preservation of
specimens are made with a tongue on the
edges of the bottom fitting into a groove
upon the lid, or they may be made with
inside pieces fastened around the inner
edge of the bottom and projecting so as to
catch the lid. The accompanying outlines
show the method of joining different forms
of boxes (Figs. 65-67). The bottom of the
box should be lined with some substance
Fig. 65.—Detail drawing which will enable the specimens to be
of front of box, made to pinned into it securely. For this purpose
resemble a book: s, s, sheet-cork about a quarter of an inch thick
sides, made of two is to be preferred to all other substances.
pieces of wood glued Ground cork pressed into layers and
together across the covered with white paper is manufactured
for the purpose of lining boxes. Turf
grain; t, tongue; g, compressed into
groove; c, cork; p, sheets about half
paper covering the an inch thick and
cork. covered with paper
is used by many
European collectors. Sheets of aloe-pith or
of the wood of the yucca, half an inch thick,
are used, and the pith of corn-stalks (Indian
corn or maize) may also be employed, laid
into the box and glued neatly to the
bottom. The corn-pith should be cut into
pieces about half an inch square and joined Fig. 66.—Detail drawing
together neatly, covering it with thin white of front of box: t, top;
paper after the surface has been made b, bottom; e, side; f,
quite even and true. Cork is, however, the strip, nailed around
best material, for, though more expensive inside as at n; c, cork;
than the other things named, it has greater p, paper lining.
power to hold the pins, and unless these
are securely fixed and held in place great damage is sure to result. A
loose specimen in a box will work incalculable damage. Boxes should
be made of light, thoroughly seasoned wood, and should be very
tight. They are sometimes made so that specimens may be pinned
both upon the top and the bottom, but this is not to be commended.
The depth of the box should be sufficient to admit of the use of the
longest insect-pin in use, and a depth between top and bottom of
two and a quarter inches is therefore sufficient. Boxes are
sometimes made with backs in imitation of books, and a collection
arranged in such boxes presents an attractive external appearance.
A very good box is made for the United States Department of
Agriculture and for the Carnegie Museum in Pittsburgh (Fig. 68).
This box is thirteen inches long, nine inches wide, and three inches
thick (external measurement). The depth between the bottom and
the lid on the inside is two and one eighth inches. The ends and
sides are dovetailed; the top and bottom are each made of two
pieces of light stuff, about one eighth of an inch thick, glued
together in such a way that the grain of the two pieces crosses at
right angles, and all cracking and warping are thus prevented. The
lids are secured to the bottoms by brass hooks fitting into eyelets.
Such boxes provided with cork do not cost more than fifty-five cents
apiece when bought in quantities. Boxes may be made of stout
pasteboard about one eighth or three sixteenths of an inch thick,
with a rabbet-tongue on the inside. Such boxes are much used in
France and England, and when well and substantially made are most
excellent. They may be obtained for about thirty-five cents apiece
lined with compressed cork.
Cabinets and
Drawers.—Large
collections which
are intended to be
frequently
Fig. 68.—Insect-box for
consulted are best
preservation of
preserved in
collections.
cabinets fitted with
glass-covered
drawers. A great
deal of variety exists in the plans which are
adopted for the display of specimens in
cabinets. Much depends upon the taste and Fig. 67.—Detail drawing
the financial ability of the collector. Large of box, in which the
sums of money may be expended upon tongue, z, is made of
cabinets, but the main thing is to secure the strips of zinc let into a
specimens from dust, mould, and insect groove and fastened as
pests. The point to be observed most at n; g, groove to catch
carefully is so to arrange the drawers that tongue; s, s, top and
they are, like the boxes, practically air-tight. bottom; c, cork.
The writer employs as the standard size for
the drawers in his own collection and in the Carnegie Museum a
drawer which is twenty-two inches long, sixteen inches wide, and
two inches deep (inside measurement). The outside dimensions are:
length, twenty-three inches exclusive of face; breadth, seventeen
inches; height, two and three eighths inches. The covers are glazed
with double-strength glass. They are held upon the bottoms by a
rabbet placed inside of the bottom and nearly reaching the lower
surface of the glass on the cover when closed. The drawers are lined
upon the bottom with cork five sixteenths of an inch thick, and are
papered on the bottom and sides with good linen paper, which does
not easily become discolored. Each drawer is faced with cherry and
has a knob. These drawers are arranged in cabinets built in sections
for convenience in handling. The two lower sections each contain
thirty drawers, the upper section nine. The drawers are arranged in
three perpendicular series and are made interchangeable, so that
any drawer will fit into any place in any one of the cabinets. This is
very necessary, as it admits of the easy rearrangement of
collections. On the sides of each drawer a pocket is cut on the inner
surface, which communicates through an opening in the rabbet with
the interior. The paper lining the inside is perforated over this
opening with a number of small holes. The pocket is kept filled with
naphthaline crystals, the fumes of which pass into the interior and
tend to keep away pests. The accompanying figure gives the details
of construction (Fig. 69). Such drawers can be made at a cost of
about $3.50 apiece, and the cost of a cabinet finished and supplied
with them is about $325, made of cherry, finished in imitation of
mahogany.
ebookbell.com