# How I was getting started with APL recently. NARS2000. ## 2022-03-31 I decided to try out NARS2000. It's Windows-only but is said by the authors to work well under Wine. Although on my OpenBSD ThinkPad I don't have Wine - I only could have on the PopOS laptop. You land in an environment that has a windows-in-window interface. Starting with the NARS2000 Session Manager. There is a button on the toolbar to create a new function, and not much else of use. Besides there being a keyboard of many APL characters sorted by their functionalities, with the names of their meanings showing up on the tooltip. After creating functions, my first question was: how do i define constants for my program? It only lets me define functions! Only then I remembered about how tutorials mentioned Immediate Execution mode that I treated as just a REPL (which it is) but not how you write programs. So I remembered about `)VARS` command, and I knew I was supposed to just assign. In the workspace. ```word "variable", a left arrow, and numbers one to five separated with spaces variable←1 2 3 4 5 ``` The way a function definition is as a whole represented is ```square frame character with vr and single-quoted word "put" on the first line, on the second line: delta character followed by word "result" followed by left arrow followed by parenthesized three words "where put what" and word "board" outside of parentheses, on the third line word "result" followed by square-bracketed character called "left shoe" with word "where", that followed by arrow left and word "what". lines i called second and third start with a square bracketed numbering as 1 and 2. All that is followed by another delta character followed by a date: 2022 3 31 17 12 49 751 (UTC) ⎕vr 'put' ∇ result ← (where put what) board [1] result←board [2] result[⊂where]←what ∇ 2022 3 31 17 12 49 751 (UTC) ``` But - without the first line! The first line is the System Function VR to print the function's code. VR stands for Visual Representation. The first character is "⎕" (square frame character) that can be typed with Alt+L or from the characters toolbar with mouse. Its tooltip description is "Quad (⎕). Used as first characters in system names.". The next first line starts with a Del (∇) (delta) and is the opening of a function definition. It starts with a name of a variable, followed by the assignment operator, followed by the declaration of function's/operator's operators with its name. In this case that's a dyadic (i.e. two-argument) operator derived monadic (i.e. single-argument) function. There is the left operator argument "where", the right operator argument "what", and the derived function's right argument "board". What this function does is take a value, coordinates and a matrix (a two-dimensional array, not to be confused with a "nested" array of arrays) and returns a matrix with the value "put" under the coordinates. We have line 1 of function body (numbered) that assigns the original matrix "board" to our function result variable "result", followed by the line 2 that assigns the new value argument "what" to the coordinates "where" of "result". In Dyalog APL, line 2 could have been ```word "what" followed by "at" character followed by word "where" followed by a character looking like a capital "T" rotated to the left, followed by word "board" what@where⊢board ``` but before I was suggested to use ⊂where, it seemed I had to do ```"result" with square-bracketed "1 right shoe where semicolon 2 right shoe where" followed by arrow left what. "Among us" looking character marks start of a comment on each line, this line is commented "Or maybe even". Second line is "result" with square-bracketed "where square-bracketed 1 semicolor where square-bracketed 2" arrow left what, followed by "Among us" character delimited comment " - which is even uglier". result[1⊃where;2⊃where]←what ⍝ Or maybe even result[where[1];where[2]]←what ⍝ - which is even uglier ``` In the above you can see what is the character for line comments. But what actually is that "⊂where"? Note that in our case "where" is coordinates, like "(2 3)". "⊂" is called "LeftShoe" and in its monadic (single-argument) meaning it's called "Enclose". ```left shoe with parenthesized 1 2 results in 1 2 one space from the left side. just the parenthesized 1 2 result in 1 2 without the space on the left side. Calling right parenthese "box" command with word "on" returns "Was OFF". Then there is just the parenthesized "1 2" returning "1 2" in a box that's labelled with "2" on top and a tilde on the bottom. Then there is left shoe with parenthesised "1 2" returning the same thing but boxed in another box that's labelled with "2" on the right side and with an epsilon on the bottom. ⊂(1 2) 1 2 (1 2) 1 2 )box on Was OFF (1 2) ┌2───┐ │ 1 2│ └~───┘ ⊂(1 2) ┌──────┐ │┌2───┐│ ││ 1 2││ │└~───┘2 └∊─────┘ ``` We can see that the "enclosed" vector has its outer box labelled with an epsilon. That suggests a set of one element! That would mean the square-brackets index syntax accepts either semicolon-demilited coordinates, or a *set* of coordinates Let's try union operator on it then (a down shoe): ```union (infix operator i.e. accepting left and right argument) of enclosed "1 2" and "2 3" returns a set of two vectors "1 2" and "2 3". Vectors are labelled with a tilde. Union of so-to-say 'bare' vectors 1 2 and 2 3 returns a vector labelled with 3 on top, containing 1 2 3. (⊂(1 2))∪(⊂(2 3)) ┌2────────────┐ │┌2───┐ ┌2───┐│ ││ 1 2│ │ 2 3││ │└~───┘ └~───┘2 └∊────────────┘ (1 2)∪(2 3) ┌3─────┐ │ 1 2 3│ └~─────┘ ``` Boxes labeled with a tilde on the bottom represent vectors, label on top indicates size of the "box". Well, I lied to you with the epilons being sets. => http://wiki.nars2000.org/index.php/Multisets > Part of the reason the so-called set functions in APL are in an odd state is that they are defined on sets, but implemented on non-sets. The epsilon on the bottom of a box only means it is a nested vector, i.e. containing other vectors. And it turns out there are counterparts of the set operations in APL that use the Multiset Operator modifier and they always preserve the *multiplicity* of an element in the collections (vectors, basically). If left shoe is called Enclose, and right shoe is called Disclose, then what will happen if we try to "disclose" the vector of two vectors, which was union of two enclosed vectors? ```right shoe of parenthesized union of parenthesized left shoe parenthesized 1 2 and parenthesized right shoe parenthesized 2 3, returns a box labelled 2 on top, 2 on the left, and tilde on the bottom, with the numbers aligned in a 2 by 2 matrix of four numbers "1 2 2 3" ⊃((⊂(1 2))∪(⊂(2 3))) ┌2───┐ 2 1 2│ │ 2 3│ └~───┘ ``` It made us a matrix where our original vectors are now rows! But let's want a function that would do "zipping" of two vectors for us, i.e. giving us pairs of nth elements from each of the vectors. A shorter way to declare a function, one that makes it anonymous (unnamed), is to place a single expression in curly braces. Optionally maybe we can add more of them, separating with the Diamond (⋄) character, described as "Used as statement separator". In case of anonymous function/operator syntax, we can use Omega (⍵) (small omega) symbol to recall the right argument. We can just assign it to a name and that will be equivalent to a full-fledged function definition. ```right parenthese box off returns "Was ON". then zip left arrow followed by (in curly brackets) circle slope character up, up arrow character, small omega character. then, "zip" parenthesized "1 2 3" parenthesized "4 5 6" returns just "1 2 3" )box off Was ON zip←{⍉↑⍵} zip (1 2 3) (4 5 6) 1 2 3 ``` That's not what we want, but a Stack Overflow answer told us this would work! But the => http://wiki.nars2000.org/index.php?title=Implementation_Comparisons NARS2000 wiki article "Implementation Comparisons" tells us > Dyalog's definition of the monadic function ↑ [up arrow] depends upon the value of ⎕ML [system variable ML] (⎕ML<2 is Mix, ⎕ML≥2 is First), whereas NARS2000 uses the monadic function ⊃ [right shoe] for Mix and ↑ [up arrow] for First ML is said to stand for "Migration Language". We try and ```same thing but with right shoe in place of up arrow, and the invocation returns us three rows two numbers each "1 4", "2 5", "3 6" zip←{⍉⊃⍵} zip (1 2 3) (4 5 6) 1 4 2 5 3 6 ``` This time it returned us a matrix of three rows and two columns, with the columns being our original arrays, therefore the rows being our desired zipped pairs. Note that this time without boxes - because we disabled boxes with a system command earlier. These boxes are toggled with system command "box" accepting arguments "on" and "off". System commands start with a right parenthese. How does it work though? The "circle slope" character monadic meaning is "Reverse All Coords" even though its dyadic meaning is "Transpose", but it can be said that it does a transposition, whatever the actual "Transpose" meaning does with its additional left argument. And when you try using right shoe ("Disclose") on two arrays (naturally for APL, that usually amounts to an array of two arrays as usually you don't need additional parentheses around them for that) it returns you a matrix of which the two vectors are rows. And the "up arrow" function called "First" in this langauge "migration" or variant? It was getting the first of the two arrays and then giving it to the transpose (or "Reverse All Coords" if you wish), which did nothing. But I wanted a ("nested") vector of vectors-pairs and not a matrix of rows-pairs. So I was searchengining. And under one stackoverflow answer two comments stood out for me: => https://stackoverflow.com/questions/17688146/apl-matrix-manipulation-trick#comment25799947_17699818 > Monadic down arrow is known as "split" and does the same thing as ⊂[2] [left shoe with square-bracketed 2], converting a matrix into a vector of vectors. May not be available in all APL implementions. – Paul Mansour, on Jul 17, 2013 at 15:56 > @PaulMansour - That's probably the case since I'm using NARS2000. I'll try it with ⊂[2] [left shoe with square-bracketed 2]. Thanks for the response. – Expedito, on Jul 17, 2013 at 16:03 ```our expression: left shoe with square-bracketed 2 followed by circle slope followed by right shoe followed by parenthesized 1 1 2 3 followed by parenthesized 4 4 5 6. evaluates to a box with epsilon of size 4 and right label digit 2, containing vectors (pairs) "1 4", "1 4", "2 5", "3 6". ⊂[2]⍉⊃(1 1 2 3)(4 4 5 6) ┌4──────────────────────────┐ │┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐│ ││ 1 4│ │ 1 4│ │ 2 5│ │ 3 6││ │└~───┘ └~───┘ └~───┘ └~───┘2 └∊──────────────────────────┘ ``` Now that we came back to nested vectors, I am thinking that that the number on the right may possibly mean the level of nesting. I am too lazy right now to write expression to test that and then caption all the boxes ascii art with extensive title text, though. Next let's mention "⍴" ("Rho") operator. In its monadic form it's called Shape because it returns the shape. In its dyadic form it's called Reshape because it creates a vector from the given elements. ```Rho of parenthesised 6 24 Rho 0 1 2 3 returns vector 6 24. Just the 6 24 Rho 0 1 2 3 returns a matrix of 6 rows and 24 columns that has each row be six (24 is 6 times 4) consecutive repetitions of 0 1 2 3. ⍴(6 24 ⍴ 0 1 2 3) ┌2────┐ │ 6 24│ └~────┘ 6 24 ⍴ 0 1 2 3 ┌24──────────────────────────────────────────────┐ 6 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3│ │ 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3│ │ 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3│ │ 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3│ │ 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3│ │ 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3│ └~───────────────────────────────────────────────┘ ``` Let's look at another thing ```2 slash curly-braced "crossed circle and omega" upper two dots character followed by a 2, a crossed circle, and a 4. Evaluates to an epsilon-box of size 6, containing two plus-boxes of size two with a crossed circle and a 2, two tilde-boxes of size two with two crossed circles, and two plus-boxes of size two with a crossed circle and a 4. 2/{∅ ⍵}¨2 ∅ 4 ┌6────────────────────────────────────────┐ │┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐ ┌2───┐│ ││ ∅ 2│ │ ∅ 2│ │ ∅ ∅│ │ ∅ ∅│ │ ∅ 4│ │ ∅ 4││ │└+───┘ └+───┘ └~───┘ └~───┘ └+───┘ └+───┘2 └∊────────────────────────────────────────┘ ``` Since the crossed circle symbol means float (floating point number) NaN (Not a Number), it is different type from an integer. Therefore, vectors containing both are of some kind of mixed type that has plus on their boxes in place of a tilde. Let's deconstruct the expression. From right to left: 2 NaN 4 is a vector; the upper two dots ("Dieresis") is described as "Each operator" - here it takes a function and a vector; the function is just one that returns an array of a NaN followed by the argument to it; that goes into a Slash ("Replicate" operation) with left argument two. The result is that for each of 2, NaN, and 4, an array of NaN and the element is made, and then of the resulting array each inner vector is repeated 2 times. Ok but commands * )SAVE -- saves the workspace to disk * )EDIT functionname -- opens a function for editing * )VARS -- lists the global variables * )FNS -- lists the functions * )OPS -- lists the operators (they are not functions) * )NMS -- just gives you all the global names of the above, with numbers telling you of which "class" a thing is * )SI -- informs you on what is used by what in an interruption of suspended funciont, the State Indicator stack of the Immediate Execution Mode * )SIC -- cleans the SI Sometimes a function may not want to save (into the workspace, not to disk) because "that would damage the SI". => http://wiki.nars2000.org/index.php?title=System_Command_SI Those are just interruptions that you want to ignore unless you're debugging, you can safely clean them with )SIC command. I'm done, I'm tired, I want this pushed to the site. It is as it is. Hope you enjoyed. There are many amazing thing about NARS2000 that I haven't covered. They may come next. I wonder if you see how every code block in this post has an extensive alt-text.