📜 ⬆️ ⬇️

We create simple GUI application on APL

Hello! Appeared recently on Habré article on the language of the APL, in my opinion, not so much described, how much complicated this fairly simple and logical language. I decided to try to prove that APL is easy! To do this, I will show you how to write a small GUI application on the APL using the Dyalog interpreter, since the APL language itself consists of a couple of dozen primitives (the same “boxes”) and that's it. In parallel, I will try to explain the value of the primitives used and the features of development in Dyalog APL (many pictures!).
So "Guess the Number" on APL!

Functions


Thousands of them! As in any interpreted language, executable code is contained in functions that, in turn, can call other functions, and so on. Accordingly, the application will be a kind of "main" function causing all the others.
Dyalog APL has several ways to create a new function. The easiest way is to enter the function name in the interpreter command line ED and press ENTER. An editor window will appear, in which, in fact, the development of the code takes place.

one

The first step is to write the function header. In Dyalog APL, functions can be without arguments (nyladic), with one argument (monadic), with two arguments (dyadic), or with variable (one or two) number of arguments (ambivalence). In addition, functions may or may not return a result. In our case, the “main” function will be with one argument and no return result.
')
2

The fName argument will be a character vector containing the window title. In Dyalog APL, there is no explicit indication of types; therefore, it is necessary to use various checks or exception handling mechanisms. In this example, I will not use them, I will leave everything on the conscience of users.
Also, local variables are defined in the function header. In our case, this is the variable number containing the value of the random number generated by the program, and the variable Win , which will be the namespace of the form containing all its elements.
After the function header follows the executable code. First you need to describe the parameters of the form and its elements.
Graphic elements are created by the system function [] WC (all system functions begin with “)” or with “[]” (this is a rectangle, but I will use an approximation)). The right argument is a namespace or a namespace element, the left is the type of the element being created and its parameters. We need to create the form itself, the input box, the button and the text.

3

Please note that the button has the type of event - 'Event' 'Select' , and the callback function - checkNum , which will be executed when pressed.
Next, you need to generate a random number and put it into a variable. In APL, the monadic function “Roll” is used for this, denoted by the primitive “?”. Its argument is the limit of the range of generated values. In our case, this number is 10.

four

Here, actually, practically all code necessary for form creation. It remains only to define the Callback function for the button and add a form event trigger line. The latter is done simply - calling the system function [] DQ with the namespace of the form connects all the elements of the form with the functions and executes them when an event occurs.
Thus, the full text of the WhatNum function will look like this:

five

To save the function in the workspace, just press Esc, or select File-> Exit (and Fix) in the menu.

6

To edit the Callback function, you can use the already known method -) ED function name (in our case - checkNum ). The editor screen will appear. The sequence of actions is the same: heading, body.
Our function should read the input value, compare it with the “hidden” and display a hint. If the user guessed the number, after 2 seconds. a new round begins.
Write the title:

7

where inp is a local variable containing user-entered text. Next, you need to write a check for equality of the entered number. In Dyalog APL, you can use constructions for this: If: Else: EndIf. So check:

eight

Do not forget that the calculations go from right to left, but the expression in brackets is performed first. If the entered number matches, the “Right!” Is displayed in the text label and after 2 seconds the program “guesss” a new number. The delay is implemented by the system function [] DL, the argument of which is the value of the delay in seconds.
The second branch of the condition will look like this:

9

That's all. Save the changes with the Esc key or through the menu we get back to the interpreter window.
To see the result, you need to run the “main” function of the application, namely, WhatNum, passing the argument the name of the form in single quotes.

ten

Entering the number in the input field and clicking on the "Answer" button, we will see the result of the action Callback function.

eleven

What's next?


Well, now we have a working application code. Now you need to save it!
In Dyalog APL, the easiest way to store is in the workspace. To save the workspace, enter SAVE) and the workspace file (* .dws) will be saved.
Now a little trick. In order not to run the function by hand each time, you can use the system variable [] LX. This variable stores the expressions that will be executed when the workspace is loaded. Expressions are written in single quotes (but quotes inside are escaped by the second quote), and to combine several expressions into one line, a special primitive is used (a diamond in the figure below):

12

I added an expression of exit from the interpreter so that the behavior of the workspace looks like a real program, but this is of course a developer’s matter. And do not forget to save changes in the workspace!
Now you can run our code by double-clicking on the workspace file. However, now, in order to make changes to the executable code, it will be necessary to interrupt the execution of the program by pressing the "CTRL + BREAK" keys. In the debugger window that appears, select “Exit” in the menu or simply press “Esc”.

13

Now you can use the tools described above for editing functions.

That's all guys


In this article, I only slightly opened the curtain over the “amazing and mysterious” world of the APL language. I hope, now he does not seem so esoteric and inaccessible to the mere mortal.
And yes, I will gladly try to answer your questions.

Source: https://habr.com/ru/post/109232/


All Articles