Based on xnor's Python answer and
user62131's Jelly answer.
General I/O
Your submission should be a program (tradfn), a function (tradfn, dfn, or tacit function), or an operator (tradop or dop). It should prompt for character and/or evaluated input from STDIN and/or take one or two arguments. It should return the result and/or print to STDOUT or STDERR. If nothing else is specified, programs print to STDOUT and functions return results.
Input
There are two ways to access STDIN: ⍞
gets a single line of text as-is. ⎕
gets a line of text and evaluates it. This is useful for numeric input, without having to use ⍎
to execute the inputted text.
Output
Expressions which are not assignments will implicitly print to STDOUT with a trailing newline. In dfns and dops, such lines will also terminate the function/operator. Output to STDOUT can also be made explicit (e.g. for a partial result in the middle of an expression or to enable a dfn/dop to continue) with ⎕←
. Output to STDERR is done with ⍞←
and does not have a trailing newline.
Programs
Tradfn programs are simply one or more lines of code, where input (if needed) is gotten with ⎕
and/or ⍞
. On TIO, Tradfn program go in the Code field and the Header field must have a ∇
followed by a name for the program. The Footer field must have a ∇
. In the Input field, the first line must have the program's name to call it, and lines immediately after that will be the lines of input, if any.
Functions and Operators
Functions are either infix or prefix.
Tradfns functions and Tradop Operators include a header line which specifies their syntax. They may then refer to the names used in the header. They may do additional I/O in the manner of tradfn programs (see above). On TIO, the entire tradfn/tradop goes in the Code field, the header must begin with a ∇
and the last line must be a ∇
, but these three characters (including a newline before the last ∇
) should not be counted into the submission length.
Dfn functions and Dop operators are one or more lines of code where the first line begins with {
and the last ends with }
, and they refer to their right and optional left arguments as ⍵
and ⍺
. Dops additionally refer to their left and optional right operands as ⍺⍺
and ⍵⍵
. The first expression which is not an assignment will terminate the function, returning the result of that expression. They may otherwise do additional I/O in the manner of tradfn programs (see above). On TIO, the entire dfn/dop (including braces) goes in the Code field, and the first line must be preceded by a name and ←
, e.g. MyFn←
. The name and ←
should not be counted into the submission length. In the Input field, the first line must have the function/operators's name to call it, followed and optionally preceded by arguments and, for operators, operands, and lines immediately after that will be the lines of additional input, if any.
Tacit functions are single lines which refer to their arguments as right and optional left arguments as ⊢
and ⊣
, and to the result of applying a sub-function f
with same argument(s) as just f
. While ⎕
and ⍞
may be used for additional input, they will only take input one, namely when the function is defined, and that input becomes constant for all applications of the tacit function. On TIO, the entire tacit function goes in the Code field, and must be preceded by a name and ←
, e.g. MyFn←
. The name and ←
should not be counted into the submission length. In the Input field, the last line must have the function's name to call it, followed and optionally preceded by arguments, and lines before that will be the lines of additional input, if any.
Examples
These example submissions compute a dot product of two vectors:
Program (tradfn) that prompts for evaluated input and prints to STDOUT:
⎕+.×⎕
Program (tradfn) that prints to STDERR:
⍞←⎕+.×⎕
Function (tradfn) that prints to STDOUT:
x F y
x+.×y
Function (tradfn) that prints to STDERR:
x F y
⍞←x+.×y
Function (tradfn) that returns a result:
r←x F y
r←x+.×y
Operator (tradop) that prints to STDOUT:
(x F) y
x+.×y
Operator (tradop) that prints to STDERR:
(x F) y
⍞←x+.×y
Operator (tradop) that returns a result:
r←(x F) y
r←x+.×y
Function (dfn):
{⍺+.×⍵}
Operator (dop):
{⍺⍺+.×⍵}
You may not expect input pre-written to a variable:
⍝ Invalid, expects input in x and y
x+.×y
Nor may you output just by saving the result to a variable.
⍝ Invalid, saves result to r
r←⎕+.×⎕
Input formats
We're liberal about input formats. For example, if a challenge says to take a table of numbers, you may expect an APL vector of vectors like (1 2)(3 4)
or an actual matrix, like 2 2⍴1 2 3 4
, not necessarily JSON like [[1,2][3,4]]
. So, an APL program can do
t←↑⎕
or
t←⎕
rather than
t←↑⎕JSON ⍞
Versions
Dyalog APL is backwards compatible. However, you may set the states of ⎕DIV
(Division Method), ⎕FR
(Floating-point Representation), ⎕IO
(Index Origin), ⎕ML
(Migration Level), and ⎕WX
(Window Expose) to achieve the desired behaviour. You do not have to count the characters needed to set these. This is justified by the existence of systems where the needed setting is default.
Libraries
You may import libraries. The ⎕CY
(Copy) statement counts as part of the code length, e.g. ⎕CY'dfns'
.
A function submission may include helper code outside the function, for example
⎕CY'dfns'⋄N←NormRand
f← ...
Truthy/Falsey
Some challenges ask for a output to be truthy or falsey, which is determined by what conditionals like :If
and dfn/dop guards (:
) can accept. Simple arrays containing a single 1 or 0, with any number of dimensions (0–15) are acceptable for truthy and falsey respectively. All other values are non-Boolean.
Scoring
Dyalog APL has its own 256-character code page. If your code consists entirely of characters in this code page (most do), you can score the it as 1 byte per character.
If you need to use characters outside APL's code page for some reason, this is legal, but you must score your program using a UTF-8 byte count, in which non-ASCII characters count as 2 or more bytes. TIO's Dyalog Unicode will report both character count and UTF-8 byte count on the top left of the Code field, in the format "X chars, Y bytes (UTF-8)".
Three primitives (built-ins) added in each of versions 14.0 (⌸
, ⍤
, and ⍠
) and 16.0 (⌺
, ⊆
, and ⍸
) are not included in the code page, and may therefore not be used in 1 byte per character submissions without counting each as its shortest multi-character alternative:
⌸
⇒⎕U2338
⍤
⇒⎕U2364
⍠
⇒⎕OPT
⊆
⇒⎕U2286
⌺
⇒⎕U233A
⍸
⇒⎕U2378
Also, remember that these longer names need to be followed by a space if the next character is alphanumeric. Note that the dyadic form of ⊂
can be reached by setting ⎕ML←3
and using ⊂
instead, however, you must account for all other changes due to ⎕ML
changing.
You can now use all the built-ins by claiming use of SBCS.