https://xpqz.github.io/learnapl/intro.html logo Learning APL [ ] * Introduction * It's arrays all the way down * Indexing * Glyphiary * Direct functions and operators * Iteration * The Key operator: [?] * The At operator: @ * The Rank/Atop operator: [?] * The Stencil operator: [?] * The Over operator: [?] * Dyadic transpose: A[?]B * Encode decode: [?][?] * Products * Trainspotting * Finding things * Partitions * Error handling * The APL Way * Namespaces [?]NS * Dealing with real data * HttpCommand * The dfns workspace * Testing * A Dyalog workflow. Or two. * What now? * Too long; didn't read Powered by Jupyter Book .ipynb .pdf repository open issue Contents * Who is this for? * What is APL? * Why should I learn APL? * ...but it's unreadable! * Don't I need a lot of mathematics? * A note on our APL subset + Not covered * Is terser better? * Other resources * Ok, I'm convinced, how do I get started? * Our first tentative steps * Valence IntroductionP A language that doesn't affect the way you think about programming is not worth knowing. -Alan Perlis Who is this for?P I wrote this to be the book I would have wanted to read when I started to learn APL. An introduction to APL for an experienced practitioner from a different programming language or two. We all learn in different ways, and I prefer the fundamental concepts laid bare first, and then learn by example. I came to APL after discovering a file of solutions to the Advent of Code 2015 challenge in K, an APL derivative. That's around 100 lines of actual code, and whilst I didn't understand any of it, I kept looking at it, trying to figure out which of the 50 problems (well, 49) this was a solution to. Each of my Python solutions typically ran to 50-100 lines+ for the bulk of the problems. Turns out it was the whole lot. That blew my mind. What is APL?P APL is an array language, and one of the oldest programming languages still in use today, next to FORTRAN, Lisp and COBOL. APL uses its own curious-looking symbols, like [?][?][?][?]**[?][?], rather than reserved words written out in English like most other languages, like C or Python. As a language, APL sits at a very high level of abstraction, making it well suited to ultra-concise formulations of algorithms. APL is a language that time is only now beginning to catch up with. Modern processors sport dedicated vector-oriented instructions and APL presents a high degree of mechanical sympathy ideally suited to SIMD instructions and by often being completely branchless in nature. APL, and its more punk rock little sister, K, really fly. APL can offer unprecedented programmer efficiency, as well as all-out execution speed. But isn't APL dead? APL is alive and well. Why should I learn APL?P You will have your own motivations for wanting to learn APL. If you're searching for the hottest thing on the market right now, to land a job at a FAANG or pad your resume with the most marketable skills, there are many other programming languages that will offer a better return: Go, Java, Rust... APL doesn't make a show in the TIOBE index in the top 100 "most popular" programming languages. Apparently, COBOL, Logo and AWK are all more "popular" than APL, at the time of writing. That's not to say there aren't extremely well-paid jobs around for engineers with skills in the "Iverson-family" languages (APL, J, K, Q etc), especially in finance. There are. However, learning APL will reward you in other ways. Perhaps the old trope about "expanding your mind" is tired, but if your background is "C-like" - C, C++, Java, Python etc - you will be exposed to a new way of solving problems. In fact, learning new programming languages that come from a different paradigm is a force multiplier for your brain. Talking about Lisp, but equally applicable to APL, Eric S. Raymond says in his essay How to become a hacker: Lisp is worth learning for a different reason -- the profound enlightenment experience you will have when you finally get it. That experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot. On the topic of Lisp, one of my favorite xkcd strips talks about Lisp's parentheses as 'elegant weapons for a more civilized age', paraphrasing Star Wars. It could also have been said about APL. lisp APL was conceived as a notation for thought. It actually took years before it even had an actual implementation as a programming language. APL is a very natural way to express algorithms. Ken Iverson, the creator of APL, was awarded the ACM Turing Award in 1979 for his work on APL, and his accompanying paper, Notation as a Tool of Thought is somewhat of a computer science milestone. Dyalog, a leading commercial APL solutions provider, make a reference to this in their corporate tag-line: "The Tool of Thought for Software Solutions". If you persist, you may discover an added bonus: code you write in other languages suddenly becomes more concise and efficient, too. ...but it's unreadable!P Every time APL gets a mention on Hacker News (it happens regularly), someone chirps up in the comments that "APL is unreadable", so let's deal with that upfront. crt-{m|[?]+.x[?]([?]x[?]|[?][?]{0=[?]:1 0 [?] ([?][?][?]|[?])+.x0 1,[?]1,-[?][?]/[?]})"[?][?]/[?]m-x/[?]} [?] From APL Cart There it is - perfectly readable: squiggle, squiggle, greek letter, weird symbol.... The key is that readability is in the eye of the beholder. To an APL programmer, the above implementation of the Chinese Remainder Theorem is perfectly readable. Claiming that something is unreadable because you can't read it is intellectually dishonest. I don't speak, or indeed read, Japanese, but that doesn't make Japanese in any way unreadable. As master haiku smith, Basho (Song Wei Jin Zuo ), exclaimed: Xia Cao ya Bing domoga Meng noJi The summer grasses. // All that remains // Of warriors' dreams. What is a fairer observation is that APL doesn't look anything like the other programming languages you know, and yes, learning APL sure presents a different kind of challenge. If you're a seasoned (say) Python programmer, and you decide to pick up Ruby, you can reasonably expect to follow code written by others from day 1, and learn enough syntax within a day or two to write code yourself. Sure, it takes a bit longer to find your way around the standard library and learn how to write idiomatic Ruby, but still. If you've learned a few languages like that, picking up another represents known and quantifiable effort. Let's be honest: there will be more initial friction when learning APL - for starters, your keyboard doesn't even have the squiggly symbols! Having said that, APL is a tiny language - there is negligible amounts of syntax to pick up, and believe it or not, actually quite easy to learn at a superficial level, once you get past the practical barriers. What takes longer is to grasp the APL way - how to wield it idiomatically, if you like. Learning how to solve problems the data-parallel way, no loops, no branching, is the key to writing APL that rocks. Once you get used to it, APL is more readable than more verbose languages [ citation needed ]. It strips away all the fluff, leaving only the algorithmic intent. John Earnest, creator of oK, wrote a humorous blog post on the topic, and Aaron Hsu's presentation on APL patterns and anti-patterns make some of the same points in a more serious vein. The last example from John Earnest's blog post poses the hypothetical question - which is more readable, the JavaScript let max = list[0]; for (let i=0; i