6 ms·
Array Languages: R vs. APL (2023)
- olliej 3y agoI personally think APL is wonderful simply because of the original APL specific keyboard [1] I've looked briefly at R and found the syntax and semantics to be less than stellar. Obviously there's going to be some bias in that sentiment due me not generally doing "array programming", but I don't believe the things that irked me were entirely as a result of that. The more annoying stuff for R is entirely second hand. As far as I can tell R (or at least R studio) maintains implicit state between runs which means you can get to a position where the same code works on some runs, and then not on later runs. My friend was having to do a lot of bioinformatics processing (many of the libraries for this are in R) and was constantly fighting to have code she wrote to process the data or produce charts (publications in bioinformatics have an acceptance bias for "looks like it came from R" that is similar to what CS [used to?] have for gnu plot). But you could run the same scripts on the same input and have it fail where previously it worked. This is before you deal with inter-version compatibility problems which also seemed frequent. What was irksome to me looking at a lot of the stuff that were doing is that it was fundamentally mostly basic scripting stuff you could do in other languages trivially (and more cleanly imo) but there were a bunch of functions (builtin or from libraries?) that did the work, but those functions weren't in R, so the claims that R was "necessary" seemed fairly bogus to me. [1] https://en.wikipedia.org/wiki/APL_(programming_language)#/media/File:APL-keybd2.svg https://en.wikipedia.org/wiki/APL_(programming_language)#/me...
- crispyambulance 3y ago> [R/RStudio] maintains implicit state between runs... That can be turned off and is, in fact, widely recommended to not keep one's workspace between runs. > This is before you deal with inter-version compatibility problems which also seemed frequent. Yeah, that can be a problem with libraries (as it is with python dependencies). It really afflicts long-running projects. R has taken a cue from the python world there. renv the best way (IMHO) to maintain a reproduceable environment in R (https://rstudio.github.io/renv/articles/renv.html https://rstudio.github.io/renv/articles/renv.html). R is nicely cogent in syntax and largely "just works" once you accept its idiosyncrasies.
- goosedragons 3y agoYou can save your workspace (state) in R. It's generally bad practice to do so. R is VERY VERY good at handling tabular data. Python can get kind of close with Pandas but IMO, it's still more awkward than base R data frames and way worse than data.table. R also has a lot of built-ins geared for statistics and built by statisticians. If you're do it statistics there's value in not having to find a library or libraries that do that.
- rzmmm 3y agoR has a lot high quality packages which implement e.g. frequently used sophisticated regression analysis algorithms. Python has these too but in my experience they are not that well tested and suffer from bugs.
- weinzierl 3y ago> "So, would APL be “readable” if I was more familiar with it? Let’s find out!" An alternative test for this hypothesis might have been using the language J, which is an array language based on APL and by the designer of APL but only using ASCII characters.
- nonfamous 3y agoR itself could be considered a test of this hypothesis, too. It’s been said that elegant, powerful Lisp would be more widely adopted if it wasn’t for all those gosh-darned parenthesis. Well, at its core R is a Lisp (specifically, Scheme) but with a more traditional syntax (infixed operators, function calls, etc). And it’s fair to say the adoption of R has, indeed, been more widespread than that of Lisp.
- anthk 3y agoJ it's standalone, it doesn't use APL in the background.
- mik1998 3y agoI'm not sure I would come to this conclusion. R has some adoption, but it's also really not used as a generic programming language, which most Lisp dialects are.
- aydyn 3y agoThat has more to do with (poor) performance, not syntax. At it's core, R's source code written in C is still very badly optimized and not performant at all.
- dan-robertson 3y agoI’m not totally convinced that being ‘secretly a lisp’ is what was good about R. I think the easy vectorisation is good, and the consequences of the bizarre function argument evaluation are good. I don’t know of lisps that do the vectorisation stuff so naturally, and while I guess fexprs are a thing, I think they are possibly too general in the syntax they can accept – basically the simplicity of lisp syntax allows macros to have more tree-structured input in a way you wouldn’t want for a language with non-lisp syntax (where the head lives outside the list), and I think the flexibility makes the syntax more confusingly non-uniform.
- bnprks 3y agoOne of the wildest R features I know of comes as a result of lazy argument evaluation combined with the ability to programmatically modify the set of variable bindings. This means that functions can define local variables that are usable by their arguments (i.e. `f(x+1)` can use a value of `x` that is provided from within `f` when evaluating `x+1`). This is used extensively in practice in the dplyr, ggplot, and other tidyverse libraries. I think software engineers often get turned off by the weird idiosyncrasies of R, but there are surprisingly unique (arguably helpful) language features most people don't notice. Possibly because most of the learning material is data-science focused and so it doesn't emphasize the bonkers language features that R has.
- VTimofeenko 3y agoAsking out of lack of experience with R: how does such invocation handle case when `x` is defined with a different value at call site? In pseudocode: f = let x = 1 in # inner vars for f go here arg -> arg + 1 # function logic goes here # example one: no external value f (x+1) # produces 3 (arg := (x+1) = 2; return arg +1) # example two: x is defined in the outer scope let x = 4 in f (x+2) # produces 5 (arg := 4; return arg + 1)? Or 3 if inner x wins as in example one?
- hugh-avherald 3y agoWell the point is that the function can define its own logic to determine the behaviour. Users can also (with some limits) restrict the variable scope.
- bnprks 3y agoIf the function chooses to overwrite the value of a variable binding, it doesn't matter how it is defined at the call site (so inner x wins in your example). In the tidyverse libraries, they often populate a lazy list variable (think python dictionary) that allows disambiguating in the case of name conflicts between the call site and programmatic bindings. But that's fully a library convention and not solved by the language.
- deleted 3y ago[deleted]
- adregan 3y agoI had been wanting to sign up for exercism, and I love APL, so this was a good nudge. However, I'm browsing the language tracks, and I don't see APL. I see this post is from July 2023—has the APL track been removed since then? Or am I just looking in the wrong place?
- jasonpeacock 3y agoFrom the post: > APL isn’t on the list of [Exercism] languages but I’ve seen it in codegolf (codegolf.stackexchange.com) solutions often enough that it seemed worth a look.
- adregan 3y agoThanks. I think I parsed that as “isn’t on the list of languages that I had wanted to learn.”
- anthk 3y agoJ it's interesting too, without the non-ASCII mess: https://www.jsoftware.com/indexno.html https://www.jsoftware.com/indexno.html https://code.jsoftware.com/wiki/System/Installation https://code.jsoftware.com/wiki/System/Installation <- install https://code.jsoftware.com/wiki/Guides/Getting_Started https://code.jsoftware.com/wiki/Guides/Getting_Started <- help
- nmz 3y agoYou can ignore this, given that I haven't used either APL/J seriously, but if I were to truly dive in, I'd lean towards APL exactly because of its non-ascii/symbolic leanings. the only similitude I know of is operator overloading, and whenever that is used, I have to relearn what each operator does in a certain context. it is only if you use it regularly like regex which while changing the meaning of the operators, since its an entire DSL, is too different for me to think + means sum. If another entirely different symbol was introduced, then I'm not assigning any functionality to it, which is why I think it should be easier.
- bruturis 3y ago>> find the GCD (greatest common divisor) of the smallest and largest numbers in an array Just for a short comparison, In J the analogous code is </ +. >/ Where / is for reduce, +. is for the GCD, the LCM is *. The basic idea of J notation is using some small change to mean the contrary, for example {. for first and {: for last, {. for take and }. for drop (one symbol can be used as a unary or binary operator with different meaning. So if floor is <. you can guess what will be the symbol for roof. For another example /:~ is for sorting in ascending order and I imagine that you can guess what is the symbol for sorting in descending order. In a sense, J notation include some semantic meaning, a LLM could use that notation to try to change an algorithm. So perhaps someone could think about how to expand this idea for LLM to generate new algorithms. The matrix m, the sum of the rows, and the maximum of the sum of the rows in J (separated by ;) m ; (+/ m) ; >./ +/ m ┌─────┬───────┬──┐ │0 1 2│9 12 15│15│ │3 4 5│ │ │ │6 7 8│ │ │ └─────┴───────┴──┘
- KarlKode 3y agoI think you mistyped J code. I don't know any J but what I understood from your comment that it should be something like </ +. >/ *.
- bruturis 3y agoYou are right, the correct code is .</ +. >./ To understand this you need to know that >. and <. are the min and max functions, and that in J three functions separated by spaces, f g h, constitutes a new function mathematically defined by (f g h)(x) = g(f(x), h(x)). An example is (+/ % #) which applied to a list gives the mean of the list. Here +/ gives the total, # gives the number of elements and % is the quotient.
- kqr 3y ago> So if floor is <. you can guess what will be the symbol for roof. Based on the examples, no, I cannot. It could be either of <: and >.
- bruturis 3y ago
- AndyKluger 3y agoNot an array language (AFAIU), but here are some of the mentioned problems solved in (glorious) Factor: : find-gcd ( nums -- gcd ) [ infimum ] [ supremum ] bi gcd nip ; : max-wealth ( accounts -- n ) [ sum ] map-supremum ; : which-max-wealth ( accounts -- i ) [ sum ] supremum-by* drop ; primes-upto
- cl3misch 3y ago> what if we just generate all products from the set of numbers 2:n and exclude those as "not prime" from all the numbers up to n? It's fun to translate terse APL to somewhat terse numpy. The result still can be very compact and you can parse it easily if you're used to looking at numpy: s = arange(2, 50); p = outer(s, s).ravel(); sorted(set(s) - set(p))
- jonocarroll 3y agoWhat's interesting there is that numpy is inspired (more than a little) by APL and aims to bring that 'array' thinking to python. I agree that thinking in this 'array' way helps to better construct a solution in any language, so I'm leaning towards 'designing' with APL glyphs, even if that's not the language I'm implementing the thing in.
- nerdponx 3y agoIf it takes any inspiration from APL, it would be mostly indirect, via Matlab.
- jonocarroll 3y agoI've seen the connection made here https://dev.to/bakerjd99/numpy-another-iverson-ghost-9mc https://dev.to/bakerjd99/numpy-another-iverson-ghost-9mc though the link to the source quote is broken. In all fairness, Matlab is itself inspired by APL.
- bruturis 3y agoAnalogous code in J, /:~ s -. p [ p =: s*/s [ s=: 2+i.48 An exercise for numpy, test that GCD(x,y) * LCM(x,y) = x*y using 1000 random numbers in the range 0..99 for x e y. test =: (* = *. * +.) & ? *./ test~ 1000 # 100
- cl3misch 3y agoThanks, that was fun. def d(x): N = arange(1, x + 1); return N[x % N == 0] def m(x, n): return x * arange(1, n + 1) def gcd(x, y): return max(set(d(x)) & set(d(y))) def lcm(x, y): return min(set(m(x, y)) & set(m(y, x))) def test(x, y): return gcd(x, y) * lcm(x, y) == x * y all([test(x, y) for (x, y) in randint(1, 100, (1000, 2))]) # True I am not a good golfer. Now I want to look at the codegolf stackexchange for this...
- jonocarroll 3y ago(author here, still getting over the first time I've seen one of my own posts on this site) The many recommendations for J here are a great nudge for me to give it a proper go. I've taken quite a liking to the traditional APL glyphs ( see a photo of the stickers on my laptop keys in this post https://jcarroll.com.au/2023/12/10/advent-of-array-elegance/ https://jcarroll.com.au/2023/12/10/advent-of-array-elegance/ ) so I'm not looking for a way to avoid them. Another detraction I've seen around is about the ambivalence of APL glyphs (taking either 1 or 2 arguments and doing something different in each case). I don't particularly mind it because I think it becomes more natural to "understand" how a function is being used the more familiar you become with it, but without the limitation on the number of glyphs, I can see the benefit of separating those.
- dan-robertson 3y agoCan’t the second argmax example be written with a right tack? Is it nicer then? (⊢⍳⌈/)+/x
- jonocarroll 3y agoYep, that makes for a nicer tacit solution maxrow←(⊢⍳⌈/)+/ but I find ⊃(⍒+/) to be an even cleaner tacit solution.
- AndyKluger 3y agoIt's not glyph-ish those APL style languages you like, but have you given Factor a good go?
- jonocarroll 3y agoI have not. I've done at least one problem (some many more) in each of 32 languages on Exercism so far, though. Looking at your example, there's some familiar features from the lisp and ML families.
- segmondy 3y agoThe thing about APL/J/K is "Notation as a tool of thought". Sure, most folks would frown and claim that Kanji or Arabic script looks like noise and must be difficult. Yet some people read it just as easy as we read roman scripts, the idea behind APL/J is to learn it enough to read it as easy as you can read python or javascript, with the added benefit that it's compact so would be faster to read and reason about than an equivalent python code. Of course python users that don't know APL are rolling their eyes, yet when you make such statement about python vs Java they get it.
- kstrauser 3y agoI saw a recent paper saying that Standard Chinese and English are approximately as fast to read by their respective native readers. One Chinese character holds much more information than one Latin letter, but an English reader can process many letters simultaneously whereas the Chinese reader takes longer to ingest each character. In other words, if you were to write the same text in Chinese and English, the Chinese version would take much less room, but native readers of each would take about as long to read them. In CompSci terms, Chinese gets more done per instruction, but English has a much higher IPC. I think we'd find the same between APL and Python. You could express ideas much more compactly in APL, but someone skilled in APL would take about as long to interpret a bit of code as a Pythonista would take to understand the Python equivalent.
- Qem 3y ago> but someone skilled in APL would take about as long to interpret a bit of code as a Pythonista would take to understand the Python equivalent. If we take into account break of flow due scrolling up and down trough text, APL probably has an advantage after a few hundred Python-equivalent lines. One screen worth of APL holds many screens worth of Python.
- thaumasiotes 3y ago> Sure, most folks would frown and claim that Kanji or Arabic script looks like noise and must be difficult. Yet some people read it just as easy as we read roman scripts This is folding several claims into each other, and they're not all true. I would tend to associate "looks like noise" with writing where the units are not separated from each other. Arabic has this feature and this makes it appear more forbidding to the untrained. Devanagari has it too: भारत गणराज्य Ge'ez script doesn't: የኢትዮጵያ ፌዴራላዊ ዴሞክራሲያዊ ሪፐብሊክ English does, if you choose to write in cursive. By this standard that I just made up, kanji are clearly pretty far toward the "organized" end of the spectrum. An untrained person looking at them is going to be able to tell you what the main principles of the script are. And while I suspect that people complaining that a script "looks like noise" are mostly just saying that they can't read it, I also think that if people were forced to rank scripts based on how confusing they look, Arabic would be rated a lot more confusing than kanji. (For a parallel to the above examples: 大日本帝國; الْخِلَافَة الْعَبَّاسِيَّة.) This belies reality; in fact, reading kanji is so much more difficult that your claim that some people can do it as easily as we read Roman scripts is not defensible. (Whereas it's fine for Arabic.) The problem isn't in the appearance of the elements, it is that there are too many of them. This means that (a) learning to read kanji is a multi-year process; and (b) even those who are considered "fully educated" nevertheless need help in doing things like reading museum plaques or technical documents that use words which modern people are not expected to encounter in day-to-day life. If all you have is "full" training in the writing system, these words cannot be read at all, so there is a system of phonetic annotation for just this purpose. (For Japanese kanji, ruby; for Taiwan, zhuyin; for mainland China, pinyin.) Kanji in specific has an additional problem that Chinese characters do not have, which is that the Japanese interpreted it as being primarily a logographic script rather than a phonetic script. To predict how a kanji is supposed to be pronounced, you need to see a particular use in a particular sentence and know the Japanese language. This is not true of Chinese characters - but it's not so much of a problem for your claim that "some people find this just as easy as we find reading Roman characters"; native speakers of Japanese don't have a problem with knowing Japanese. It does mean that it's hard to predict how personal names are supposed to be pronounced.
- pocketsand 3y agoI've always wondered if the "hard productive way" as understood by conventional wisdom (using VIM, using a language like APL) is actually more productive. For this topic, I have in mind two exercises: First, get people to nominate "experts" in a language, and assign them a straightforward task, and see who is actually faster using their native environments with code completion, language servers, etc. by systematically running these trials. Second, get people untutored in each language of similar experience, randomly assign them to a language, give them a not short but not long period of time, then do a similar exercise as above. The first has problems with assuring there is skill balance between the groups, but would I think tell you about how productive one can be in each language. The second would have good causal inference properties, but probably not tell you about the downstream effects of learning something hard with great rewards (e.g., vim or emacs). I'd bet there are marginal gains to APL (or vim) but probably nothing that matters in long-term productivity. However, I do think people underrate how fun it can be to "get good" at things like vim or APL, and I think that has really positive knock-on effects.
- deleted 3y ago[deleted]