Dyalog APL

Implementation of programming language APL

Dyalog APL is one of the most popular APL implementations. It was created in 1983 and is still under active development. It implements ISO 8485 standard of the language plus a lot of improvements, including a complex GUI.

The implementation is proprietary, but there exist a free version for students and an online interpreter TryAPL for trying the language out. The interpreter is available for several major platforms.

One of the interpreter features are D-functions, or dynamic functions — a way to define functions (named or anonymous) at runtime. The function definition is enclosed in curly brackets, left and right arguments of the function are available under aliases and , respectively. Such functions allow local definitions and recursive calls (for anonymous functions calls the function from itself).

TryAPL online interpreter
TryAPL online interpreter

Examples:

Hello, World!:

Example for versions Dyalog APL 13.1
'Hello, World!'

Factorial:

Example for versions Dyalog APL 13.1

The first line sets the index of the first element in the lists (in this case 0). The second line sets precision when printing numbers (must be greater than the length of the largest factorial).

The third line, if read in right-to-left direction, performs the following operations:

  • ⍳17 generates a list of 17 indices, starting with 0, i.e. 0 … 16.

  • 17 / ⊂'!=' generates a list of 17 strings !=. is enclose operator which allows to manipulate strings as scalars instead of character arrays. / is replication operator.

  • !⍳17 applies built-in monadic function ! (factorial) to each element of the list.

  • , is a dyadic operation which concatenates left and right arguments. After two concatenations the expression evaluates to 0 1 2 3 4 ... 15 16 != != ... != 1 1 2 6 24 120 720 5040 40320 362880 ..., i.e. the list of numbers followed by the list of strings and the list of factorials.

  • 3 17⍴ reshapes the list into a 3x17 matrix:

    ┌→─┬──┬──┬──┬──┬───┬───┬────┬─────┬──────┬───────┬────────┬─────────┬──────────┬───────────┬─────────────┬──────────────┐  
    ↓0 │1 │2 │3 │4 │5  │6  │7   │8    │9     │10     │11      │12       │13        │14         │15           │16            │  
    ├~─┼~─┼~─┼~─┼~─┼~──┼~──┼~───┼~────┼~─────┼~──────┼~───────┼~────────┼~─────────┼~──────────┼~────────────┼~─────────────┤  
    │!=│!=│!=│!=│!=│!= │!= │!=  │!=   │!=    │!=     │!=      │!=       │!=        │!=         │!=           │!=            │  
    ├─→┼─→┼─→┼─→┼─→┼──→┼──→┼───→┼────→┼─────→┼──────→┼───────→┼────────→┼─────────→┼──────────→┼────────────→┼─────────────→┤  
    │1 │1 │2 │6 │24│120│720│5040│40320│362880│3628800│39916800│479001600│6227020800│87178291200│1307674368000│20922789888000│  
    └~─┴~─┴~─┴~─┴~─┴~──┴~──┴~───┴~────┴~─────┴~──────┴~───────┴~────────┴~─────────┴~──────────┴~────────────┴~─────────────┘
    
  • finally, transposes this matrix so that each row has three elements — a number, a separator string and a factorial.

The program output looks like this:

     ┌→─┬──┬──────────────┐          
     ↓0 │!=│1             │          
     ├~─┼─→┼~─────────────┤          
     │1 │!=│1             │          
     ├~─┼─→┼~─────────────┤          
     │2 │!=│2             │          
     ├~─┼─→┼~─────────────┤          
     [...25 lines of output ...]  
     ├~─┼─→┼~─────────────┤          
     │16│!=│20922789888000│          
     └~─┴─→┴~─────────────┘

The matrix is displayed as a table with cell borders drawn, because it contains boxed strings.

IO0
PP18
3 17 (17) , (17 / '!=') , !17

Fibonacci numbers:

Example for versions Dyalog APL 13.1

This example uses Binet’s formula implemented via an anonymous D-function. is expression separator, so the function consists of two expressions evaluated in left-to-right order. The first one calculates golden ration and binds it to name phi. The second one calculates the value of the function (Fibonacci number) based on its right argument (the index of the number). rounds the number up.

Since the function is unary and is defined using scalar functions only, it can be applied to an array — in this case to an array of indices between 1 and 16, inclusive. This will result in an array of Fibonacci numbers:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

{phi(1+5*0.5)÷2  ((phi*) - (1-phi)*)÷5*0.5} 1+16

Factorial:

Example for versions Dyalog APL 13.1

This example calculates factorial of a number N by applying multiplication reduction to a list of numbers from 1 to N, inclusive (expression ×/1+⍳). The anonymous D-function applies this expression to its right argument and concatenates the result with the argument itself and a string separator. Note that in this case the string is not boxed. The function, in turn, is applied to a list of numbers from 0 to 16, written in a column, i.e. as a two-dimensional 17x1 array. Program output looks like this:

 ┌→───────────────────┐      
 ↓0 != 1              │      
 ├+──────────────────→┤      
 │1 != 1              │      
 ├+──────────────────→┤      
 │2 != 2              │      
 ├+──────────────────→┤      
 [...25 lines of output ...]  
 ├+──────────────────→┤      
 │16 != 20922789888000│      
 └+──────────────────→┘
IO0
PP18
{, '!=', ×/1+⍳⍵}¨17 1⍴⍳17

Fibonacci numbers:

Example for versions Dyalog APL 13.1

This example uses an anonymous D-function which calls itself recursively. The first expression of the function processes the case of the first two Fibonacci numbers, returning 1. The rest of them are processed by the second expression, which calls this very D-function (function ) for smaller indices of the numbers and sums them. Overall the first line of code associates the calculated array of numbers with name fib and outputs nothing.

The second line converts the array to required format: each element gets concatenated with a comma separator, all elements of the resulting array get concatenated with each other (,/), and the result is padded with .... The overall output of this line looks exactly as required:

1 ,  1 ,  2 ,  3 ,  5 ,  8 ,  13 ,  21 ,  34 ,  55 ,  89 ,  144 ,  233 ,  377 ,  610 ,  987 , ...
fib{⍵≤2:1  (∇⍵-1)+∇⍵-2}¨1+16
((,/{, ', '}¨fib)),'...'

Quadratic equation:

Example for versions Dyalog APL 13.1

This code defines a named D-function which accepts equation coefficients as a single argument (a three-element array) and returns equation solution. First the argument is split into separate coefficients (N⊃⍵ picks Nth element of the array) and they are assigned names. After this first coefficient and discriminant are checked — if one of them is zero, the function returns a special value. Finally, non-zero discriminant is processed with the same code regardless of its sign due to the fact that APL has built-in complex numbers datatype. A function call

solve 1 0 2

will return:

0J1.4142135623730951 0J¯1.4142135623730951

(J is a separator between real and imaginary parts of the number, and ¯ — is a “high” minus, used for input/output of negative numbers).

Note that this code is not typical for APL due to conditional branching use as well as limiting possible arguments to a one-dimensional array of three elements.

solve←{A0⊃⍵  B1⊃⍵  C2⊃⍵  A=0:'Not a quadratic equation.'  D(B*2)-4×A×C  D=0:-0.5×B÷A  ((-B-D*0.5), -B+D*0.5)×0.5÷A}