7 ms·
As I like to put it when I have to come up with a “fun fact” for corporate icebreaker activities, “I have programmed in APL for money”. It’s not really that har
by masto 7y ago
As I like to put it when I have to come up with a “fun fact” for corporate icebreaker activities, “I have programmed in APL for money”. It’s not really that hard.
The notation is certainly a stumbling block if you’re trying to read these articles out of context. How do you even “read” something like “5⍴⍳20” in your head when it’s that unfamiliar? But that’s the same as anything else, what does “for (x = 0; x < 10; i++)” mean to someone who’s never seen it before? So when you can see that as “5 rho iota 20”, at least you can pronounce it. But when you’re actively using the language you know what ⍳20 means as easily as you know what “1 through 20” means. “Reshape 5” is just as familiar. So it takes only a glance at 5⍴⍳20 to visualize a 5x4 matrix of the numbers from 1 to 20.
Once you start thinking that way, the majority of what might seem like show-off tricks or being clever are just the paradigm of the language. The idea of creating a vector of things, applying a true/false test to every element to create another vector of 1s and 0s, and summing those or using them to pick elements out of another vector is a super powerful tool that lets you express a lot of ideas in a compact form that describes the algorithm without getting bogged down in writing explicit loops. What’s the average of these numbers? The sum divided by the count. In APL you just write that down. In many other languages, you write “I wish to declare a variable ‘i’ of type 32-bit-integer. Set ‘i’ to 0. Compare ‘i’ to ...”. What does any of this have to do with an average?
Now of course in an article where the author is actually showing off how clever you can get, and the topic of many items is some obscure mathematical concept, it’s going to be harder to read than the average program.
- retrac 7y ago> The idea of creating a vector of things, applying a true/false test to every element to create another vector of 1s and 0s, and summing those or using them to pick elements out of another vector is a super powerful tool that lets you express a lot of ideas in a compact form that describes the algorithm without getting bogged down in writing explicit loops. Exactly. APL's syntax and terminology is a bit odd. But what you're describing there should be a concept known to anyone familiar with a functional language. It seems you've just described what I would call mapping a function over an array. Indeed, a very powerful construct, often lacking from more procedural languages. Or, sadly, often eschewed by programmers even in the languages that do have it. APL makes me think of Forth. Because of its idiosyncratic terminology (e.g. functions are "words") and unusual syntax (strict RPN), it is considered more exotic than it really is.
- icen 7y ago> But what you're describing there should be a concept known to anyone familiar with a functional language. It seems you've just described what I would call mapping a function over an array. The other powerful notion is that arrays become mapping functions over their index.
- sedachv 7y ago> So it takes only a glance at 5⍴⍳20 to visualize a 5x4 matrix of the numbers from 1 to 20. That would be 5 4⍴⍳20