Direct definition (notation)
Ken Iverson's direct definition is a function style written with a name followed by :
and a result, using ⍺
and ⍵
for arguments. The result may be one expression, which is evaluated to apply the function, or three expressions separated by :
, in which case the middle expression is evaluated to determine which of the other two should be run. Direct definition was used by Iverson and others in publications describing APL in the 1970s and 80s, and was also implemented as a library for SHARP APL that translated a direct definition to a defined function. The argument naming and use of :
for a condition later appeared in Dyalog APL's dfns.
The simpler form of direct definition consists of a name and result expression. In the result, ⍺
indicates the left argument, and ⍵
indicates the right argument. Unlike the later dfns, the name is part of the function definition; direct definition can't define anonymous functions. For example, the following expression would define the function plus
to add its arguments:
plus:⍺+⍵
In a form with three expressions (four parts total), the middle part is a condition, and is evaluated first. The function's result is then found by evaluating the first expression, if the condition resulted in 0, and the last expression otherwise.
fac:⍵×fac ⍵-1:⍵=1:1
The valence of the function is determined by which argument names appear: it's niladic if neither ⍺
nor ⍵
is used in any expression, monadic if only ⍵
is used, and dyadic if ⍺
is used. As direct definition was intended to represent pure functions (without side effects), assignment within an expression always localizes its target.
Direct definition was defined in Iverson's 1974 text Elementary Functions,[1] where it was called "formal function definition", and used in writings by Iverson[2] and others including Gene McDonnell[3][4] and Donald McIntyre.[5] Having been used in the SHARP APL Technical Notes (SATNs) 34 and 35, it was described by Iverson in SATN-36 and subsequently used in SATNs 40 and 41. The syntax was never implemented in SHARP APL itself, but a set of library functions for working with it were defined, including CFD
("canonical from direct") to translate it to SHARP's function definition, or the "canonical form".[6]
References
- ↑ Ken Iverson. Elementary Functions chapter 10. IBM. 1974.
- ↑ K. E. Iverson. Programming Style in APL at IPSA '78.
- ↑ Eugene McDonnell and Jeffrey Shallit. Extending APL to infinity at APL80.
- ↑ Eugene McDonnell. SATN-40: Complex Numbers. 1981-06-20.
- ↑ D. B. McIntyre. "Experience with Direct Definition One-liners in Writing APL Applications" at IPSA '78.
- ↑ Ken Iverson. SATN-36: Direct Definition. 1980-04-20.