At one time I briefly spent a bunch of time learning kdb/q. I remember one particular day when I wrote a non-trivial program and it worked first time. I was so shocked I thought I must have suffered some kind of brain aneurism or something.
if you don't mind me asking, do you also remember the day when you wrote some non-trivial program in any language and it "worked" the first time, whatever that means (i presume "correctly")? what was the language? are you sure you've made a full recovery from that shock as well?
on a serious note, APL (and, by proxy, its descendants) invented REPL (dubbed "dialogue approach") long before the people who coined "REPL" even came to be. When that happened, C lanugage wasn't around either. Fortran was, granted, and sure enough it "worked" every time. you didn't even have to try, just punch it up on a punchcard, stick it in, wait a while. done. flawless.
on a closing note: writing correct programs takes skill and happens in iterations. the faster you can iterate, the faster you can justify your money's worth. the less you type, the more you think. less code less bug.
What I mean by "worked" in this context is compiled/parsed[1] without error and had correct behaviour when executed.
I have been programming professionally for about 30 years at this point, so it has happened to me literally hundreds of times at this point in at least 10 other languages- in fact in some languages (especially Haskell and Rust) I would say it's the norm for me rather than the exception to have the code work correctly if it passes compilation (which is sort of the point of strict type systems obviously).
It literally only ever happened the one time in kdb which is why I remember it so vividly and not in those other languages.
I have no idea why you thought your closing note might help, but sure.
>on a serious note, APL (and, by proxy, its descendants) invented REPL (dubbed "dialogue approach") long before the people who coined "REPL" even came to be.
While I have enjoyed learning array languages, and think they are still underrated, Wikipedia seems to disagree with this statement above.
According to wikiepdia, REPL seems to have been coined after Iverson created his notation, but before the first APL was ever written.
"The 1974 Maclisp reference manual by David A. Moon attests "Read-eval-print loop" on page 89, but does not use the acronym REPL.[7]
Since at least the 1980s, the abbreviations REP Loop and REPL are attested in the context of Scheme.[8][9]"
The first APL interpreter was implemented on System/360 in 1965. Iverson got a Turing in 1979.
> If you're arguing that "Read-Eval-Print cycle" doesn't count as REPL, then it pretty strongly undercuts your argument that "dialog approach".
no, thankfully, it doesn't, and here's why. as i elucidated above, PDP-1 was indeed a revolutionary "el cheapo" computer, which DEC managed to ship over 50 units of. it gave birth to hacker culture, just because it had to be hacked with all kinds of peg legs in order to be useful. now, to the point:
1. PDP-1 is mostly remembered for Spacewar!, a groundbreaking space combat game invented by a dude who also coincidentally invented large parts of LISP while on IBM payroll.
2. APL, after its debut in 1965-1966, had its first official application to teach formal methods in systems design at NASA Goddard Center.
My point should be obvious, but just in case:
in our line of work (between sessions on Hacker News) we are sometimes faced with the concept of "production" (usually on Friday afternoons). this idea really matters. it makes all the difference between fooling around and the real deal.
therefore, as it must follow, and as i mentioned much earlier, APL was and remains the very first real REPL system, although they didn't really use that terminology. all i meant is that in 21st century people take Chrome's devconsole, ipython, node, zsh for granted. with completions, hints, all that.
(in APL'esque family, by the way, there's hardly anything to hint or auto-complete. mathematics doesn't work that way. by the way, not coincidentally, one of the most successful modern descendants of APL is called Wolfram Mathematica)
ok, you have a point there. all 53 PDP-1s were "cheap" computers compared to IBM stuff (only $1M, adjusted) and used paper drum instead of punch cards. that was a nightmare to deal with, so people were buying IBM M typewriters to punch stuff in. Although Deutsch and Berkeley's LISP was in large part a copycat of IBM's LISP, people were indeed typing in LISP expressions in a REPL'ish manner. But if you ever saw a line of LISP, you can imagine what kind of "REPL" that was. "Miss one paren" comes to mind :)
APL\360 used a much more advanced selectric with a dedicated typeball, was designed for a machine of a totally different class, and could not be compared to literally kilometers of paper containing mostly mistyped parentheses. it was TRULY terse, expressive and interactive.
but yeah, PDP-1 hackers technically got there first - they had no choice :) check this out:
6-7 Input and Output, System Operation is where the READ-EVAL-PRINT is indeed mentioned. but if you read the entire paragraph, and the next one ("if the system drops dead" lol) you'll agree that PDP-1 "REPL" was hell on earth, at the very end of the manual there are some really juicy REPL expressions :)
LISP was a torture compared to short and powerful APL notation.
Based on the one thing I remember in APL I'm guessing the first two characters are "sum over some data structure" and the data structure is what the next two mean. What does it mean entirely?
There's an initial learning curve as there is with any new programming paradigm (which everyone who works exclusively in C-like languages forgets that they paid a long time ago) but there are only something like 100 primitives, and then once you understand how they interact, the world opens up and the volume of a sphere being
Vs=: 4r3p1"_ * ] ^ 3:
just reads normally. It's been over ten years since I did anything in J and I can almost explain that now...
we call them "trains". since this one has a descriptive name, `avg a`, is not cryptic at all. just a bit fewer of absolutely meaningless parens and duplication.
but it doesn't end there:
1. imagine you wanted a moving average instead. i need to change one character in this train to get `mavg`. what would you need to do?
2. imagine you want to compute moving averages for each of 1000 arrays using both instruction-level parallelism and all available cores? while you'd be writing your unobfuscated code for that, i'd be done way before you're back from your lunch break. in two keystrokes.
once you'd be done with your solution, there would be no need to discuss productivity, or so i hope, but to discuss performance would be interesting.
the thing about tacit programming is that it wouldn't use 'a' in the above. if you wrote average in j without it being tacit, it would probably be more readable to you. the question of how to thread data around without naming it is an interesting one to me.
> without it being tacit, it would probably be more readable
definitely. tacit can be taken a bit too far sometimes, but when a certain discipline is observed (just like in any language, really) it is no less readable than... let me see. oh, lets take some typical pandas/polars heroics - no, those things don't give me brain aneurisms and not too shocking either. they just make wanna vomit no less than the poor people who were forced to write it and contemplate what they've done.
> tacit is "bad"
not at all. it takes one time to see how avg looks in explicit notation to understand that the idea of trains is totally justified.
> the question of how to thread data around without naming it is an interesting one to me.
good question. see above - yes indeed, things can be taken to extremes. when an apl/k program is a oneliner 80 chars long, that's just not cool. there is no reason not to break it down a bit into moving parts with names (and ample space on the right margin for annotations). in no way APL and friends are somehow exempt from commenting their code. but that's not really endemic to array languages, you'd agree.
APL and K are still pretty daunting, but I've recently been dabbling in Lil[1], which is something like a cross between K and Lua. I can fall back on regular procedural code when I need to, but I appreciate being able to do things like:
127 * sin (range sample_rate)*2*pi*freq_hz/sample_rate
This produces one second audio-clip of a "freq_hz" sine-wave, at the given sample-rate. The "range sample_rate" produces a list of integers from 0 to sample_rate, and all the other multiplications and divisions vectorise to apply to every item in the list. Even the "sin" operator transparently works on a list.
It also took me a little while to get used to the operator precedence (always right-to-left, no matter what), but it does indeed make expressions (and the compiler) simpler. The other thing that impresses me is being able to say:
maximum:if x > y x else y end
...without grouping symbols around the condition or the statements. Well, I guess "end" is kind of a grouping symbol, but the language feels very clean and concise and fluent.
for that matter, i always wonder how people mistake python for numpy :) they have surprisingly little in common.
but enough talking about languages that suck. let's talk about python!
i'm not some braniac on a nerd patrol, i'm a simple guy and i write simple programs, so i need simple things. let's say i want an identity matrix of order x*x.
nothing simpler. i just chose one of 6 versions of python found on my system, create a venv, activate it, pip install numpy (and a terabyte of its dependencies), and that's it - i got my matrix straight away. i absolutely love it:
assuming you're referring to numpy as to have anything to do with python spec, i totally agree with you. only it doesn't. so don't pytorch and pandas (and good so, poor python doesn't need any extra help to be completely f).
> you get an nxn identity matrix by...
no, man, that's how you get it. really advanced technique, kudos!
i get it by:
id:{...} /there are many ways to implement identity in k, and it's fun!
id 3
+1.00 +0.00 +0.00
+0.00 +1.00 +0.00
+0.00 +0.00 +1.00
but if you can keep a secret, more recently we've gotten so lazy and disingenuous in k land, and because we need them bloody matrices so often now, we just do it like so:
(but of course before we do that we first install python4, numpy, pytorch, pandas and polars - not because we need them, just to feel like seasoned professionals who know what they're doing)
this is of course obvious first idea, but the recipe from above is actually from the official k4 cookbook. t=t is less innocent than it seems, i'm afraid.
Pretty much, yeah! The difference is that in Python the function that calculates a single value looks like:
foo(x)
...while the function that calculates a batch of values looks like:
[foo(x) for x in somelist]
Meanwhile in Lil (and I'd guess APL and K), the one function works in both situations.
You can get some nice speed-ups in Python by pushing iteration into a list comprehension, because it's more specialised in the byte-code than a for loop. It's a lot easier in Lil, since it often Just Works.
A few more examples in K and Lil where pervasive implicit iteration is useful, and why their conforming behavior is not equivalent to a simple .map() or a flat comprehension: http://beyondloom.com/blog/conforming.html
I assume that in most array languages, you also create "words" or however you want to call functions, to reuse code. I wonder about a purely aesthetic issue: how does it look to interleave those symbols with user-defined words that by nature will be much, much longer, i.e. "create-log-entry" or "calculate-estimated-revenue".
I never did any real programming in APL, but I studied it over about 2 months. When you get used to the symbols, reading spelled-out words feels like reading in slow motion, or being stuck in molasses.
Most (not all) APL code I've seen uses very short names, often one letter names, for function names. And APL programmers are famous for cataloging "idiom" which are short phrases for common subroutines. In other words, it's best practice to repeat 3- or 4- symbol phrases instead of defining a subroutine.
Of course, there's nothing about an array language that requires using symbols; but for some reason most do.
>Of course, there's nothing about an array language that requires using symbols; but for some reason most do.
The idioms become words and you read them like words, you don't step through each letter of a word when you read it, you recognize the shape. The same thing happens in APL and its ilk, any commonly used sequence is instantly understood as its function without having to parse each individual symbol and what it does.
> i assume that in most array languages, you also create "words" or however you want to call functions, to reuse code.
sure, that's a very useful feature, like elsewhere.
> I wonder about a purely aesthetic issue: how does it look to interleave those symbols with user-defined words that by nature will be much, much longer, i.e. "create-log-entry" or "calculate-estimated-revenue".
strictly speaking, dashes and underscores in k can't even be a part of identifier - they are core language primitives. it is very uncommon to see java-like identifiers like CalculateEstimatedRevenue, why would you want that?
to your question:
here's a bit of an oddity: all user-defined functions and core language operators can be called using functional notation:
v:1 2 3 / some vector
v+v / usual infix notation, two operands: left and right
2 4 6
+[v;v] / same as infix, but called as it were a function.
2 4 6
add:{x+y} / a user-defined function: a lambda with a name and two operands.
add[v;v]
2 4 6
but there is an important distinction between the two. you can't use your `add` function infix, you must call it as a function, and there are good reasons for that:
2 add 2 / that's not gonna work
that said, mixing language primitives with function calls looks and reads just fine:
How does that scale up to program that's thousands of lines? What if you have a hundred different vectors? You're not going to be calling them v1, v2, ...
Or, do you just not do that sort of stuff in these languages? I'm not very familiar with them, but I have ended up with some pretty long programs using Pandas in Python.
/sepal:lengths and stroke widths
spl:[l:11 14 12;w:1.3 1.5 1.2] /this is your "struct", if you will
spl.w
1.3 1.5 1.2 /proof
*/spl
14.3 21. 14.4 /for mul, we don't even have to bother with field names
*/spl`l`w /but if you insist, lets make it explicit
14.3 21. 14.4
to produce a "factory" for well-formed spl objects is a no-brainer as well.
why we don't use v_ prefix:
1. everything what can be a vector should be a vector.
2. we can't use underscore anyway - it is an operator.
very important things should have short names.
locals you're immediately operating upon should have short names.
short names should be used in a consistent way.
less important things can have longer names.
variables in a broader scope can have longer names.
if you have a hundred different vectors, don't just dump them in a pile; put them in dictionaries, tables, namespaces, or scopes.
* namespaces do exist, and are just as useful as they are in c++ and especially my beloved *sun.misc.unsafe*. i recommend.
* instead of passing 20 arguments to a function (which is impossible - the limit is lower), we pass a dictionary if we have to. k **pretends** that everything is passed by value, but in reality it is much smarter than that.
* notion of *scopes* is a bit of a non-sequitur here, but it is fundamentally important that there is no *lexical scoping* in k. the only two scopes which are available from the scope of a lambda are exactly *local* and *global*. and for as long as your function doesn't mess around with global scope or i/o (which is essentially the same thing), it remains pure, which is super cool. this design is not just for simplicity - it is for a good reason, and more than one.
* the above doesn't mean that it is impossible to create a *closure* in k and pass it around as a value.
* functions take up to three implicit arguments - named x,y and z (they can be renamed explicitly, but why not just document their semantics instead, in-situ?). all you need to do to declare xyz is reference them in the function definition. in competent k code, you'll rarely see a function with more than xyz.
* in k community, we don't use upper case unless the apartment is on fire. god forbid.
* shorter names and more documentation, and there will be joy.
Perhaps a more precise question is whether you can write programs as performant as those written in C or Fortran and the answer is it depends on the program (and more likely the programmer). The languages tend to do memory management for you which means giving up some control. Most use “immutable” data structures which force more contraints.
But for the loss of control you get stuff like fancy SIMD implementation for nothing.
All and all there’s a cost/benefit calculation but that ratio can get quite low.
this is a gross undeappreciation of a truly remarkable effort by my dear friend and associate. also, at least one of the languages presented is due to him. you can call him @ktye. he's too modest.
it is not a Kunstkamera or some computer cryptozoology extravaganza. many things are shown here, from different eras, but some are not - they are evolving. and progress takes sacrifice.
cheers
k.
ps. we usually spell atw as atw :) he also goes by a. don't chicken out, send him an email. he's a very friendly guy. just like me.
Yeah - IDK why it never makes it to these lists. R too. Matlab being 2D matrix first/default gets it right for me there. IK matrices trivially translate to arrays, still: find 2D to be extra expressive on human level, for zero price paid. I get it it's all the same to the cpu. 2D rows-columns rectangle of data being the simplest data structure both necessary and sufficient covering a 1) matrix 2) spreadsheet 3) SQL table 4) directed graph of nodes and edges. (in the past I've read someplace that lists are for pie eaters, but wouldn't know myself
Octave covers all the Matlab functionality I need, not sure if it runs in a browser. I mean if you have the source code for something there must be some way to get it to run in a browser these days, right?
of course it runs in a browser - pretty much everything you can build using clang will run on the web. including linux kernel and llvm itself.
to hell with peanuts: i'm pretty sure someone must even built cpython interpreter to wasm target, why not. there is no limit of what can be achieved by a group of motivated people with zero sense of direction.
k, however, makes quite a bit of sense on the web - also on your phone. and time to prompt is going to be MUCH faster than python and octave. and i mean their native builds :)
Programming in an array lang "should" generally feel like using a calculator.
You are working in a REPL, starting with small expressions to verify they are roughly doing what you want and then composing them to build up until you can plug it all together and now have a formula you can plug into the calculator to plug and chug all the rest of your data.
So in that sense yeah it does kind of replicate the magic of the first time you got a complex equation or BASIC program to run on your TI back in your school days.
r/apljk on reddit is also active.
reply