Some Notes on Direct Definition
Vector 9:2, page 27
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 27
Many readers of Education Vector use Direct Definition routinely to input their APL functions, and will probably wonder why I have bothered to include these notes.
Now, for those who are still with me … Direct Definition is an alternative way of entering APL functions into a workspace. In style it is similar to formal functions in mathematics, and it is popular with people who have been trained in mathematics, or whose brain happens to work that way. Personally I think it is well worth exploring — like APL itself, you don’t know what you’re missing until you’ve tried it.
Some APL interpreters support direct definition as standard, others do not have it at all. For the latter case, I include at the end of this article a "DIY" DD Workspace (based on one by Ken Iverson, as found in ‘A Source Book in APL’ reprinted from the 1980 IP Sharp Users’ Meeting in Toronto, but modified by various people since). DD functions are presumed to produce a result, and the left and right arguments (if they exist) are referred to as ⍺ and ⍵. To enter DD mode, first type DEF (in some versions this may be DEFINE). Then type in the name of the function you want to define, followed by a colon (:). After the colon, type in the APL formula which has to be evaluated to produce the required result, using ⍺s and ⍵s as necessary.
Examples¶
(a) to calculate a square root:
(b) to round a value to a specified number of places:
It is quite easy to translate back from DD to regular function definition. First extract the function name, and look for ⍺s and ⍵s. If there are any ⍺s, you want a dyadic function; if there are ⍵s only, you want a monadic one; and if there are neither, the function is niladic (no arguments). Accordingly, your header will be Z←A FUNCTION B, or Z←FUNCTION B, or just Z←FUNCTION. The formula must then be expressed in APL, using A for ⍺, B for ⍵ and assigning the result to Z. If your system does not support DD, you can build your own workspace as follows:
∇ DEF;⎕IO
[1] ⎕IO←1
[2] ⎕FX F9 ⍞
∇
∇ D←F9 E;F;I;K
[1] F←(,(E='⍵')∘.≠5↑1)/,E,(⌽4,⍴E)⍴' Y9 '
[2] F←(,(F='⍺')∘.≠5↑1)/,F,(⌽4,⍴F)⍴' X9 '
[3] F←1↓⍴D←(0,+/¯6,I)↓(-(3×I)++\I←':'=F)⌽F,(⌽6,⍴F)⍴' '
[4] D←3⌽C9[1+(1+'⍺'∊E),I,0;],⍉D[;1,(I←2↓⍳F),2]
[5] D[1;]←(1↓⍴D)↑(¯2×~('⍵'∊E)∨'⍺'∊E)↓D[1;]
[6] K←K+2×K<1⌽K←I∧K∊(>⌿ 1 0 ⌽'←⎕'∘.=E)/K←+\~I←E∊A9
[7] F←(0,1+⍴E)⌈⍴D←D,(F,⍴E)↑⍉ 0 ¯2 ↓K⌽' ',E,[1.5]';'
[8] D←(F↑D),[1]F[2]↑'⍝',E
∇
A9←'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ⎕∆'
C9← 5 11 ⍴'∆∆∆Z9←∆∆∆∆∆∆Y9Z9←∆∆∆∆∆∆Y9Z9←X9∆∆∆)/3←(0=1↑,∆∆∆∆←0,0⍴Z9←'
Note: in the definition of C9 the symbol ∆ represents a space. These spaces must be counted carefully! (A small prize is offered to anyone who gets this right in 1!)
With these four objects in your workspace, you will have direct definition available for any APL environment. Only one restriction — you can’t name objects DEF, F9, A9 or C9.