by beagle3 · 13y ago · 0 comments
A language should push you towards a more robust/faster/simpler solution by making it the simple, elegant thing to do. In K, a run length encoder would be something like:
{(x@&~m),'(+/m)@&~m:~':x}
Which looks like line noise, but basically encodes the following operations:x is the input
m gets 1 where a position in x is the same as the previous position, 0 where it isn't. (apply ': "each-pair" to ~ "match")
Then take the sum of match (+/m) at where (@&) there is no match (~m).
Finally, add that to x where there is no match (x@&~m, literally, "x at where no m"), and zip it (,' meaning concat each). And the curly brances make it into a function of x.
Now, I understand most people would think that this is unreasonable. However, in K it is easier to produce a vectorized solution than an itertive one - and as an added bonus, the bugs tend to be either of the "wrong spec" or the "off by one" error, eliminating whole classes of potential bugs.
And .. the result is usually quick, thanks to vectorizing (and can go on a GPU easily).
A language that promotes item-at-a-time processing is missing a lot.
edit: ignore the question, yes it does:
There's a learning curve; it's steep, but it is well worth it - much like Math notation (give me "x^n" any day over "repeat multiplying x by itself n times") or Music notation, once you're used to it, any notation that is less concise seems arbitrarily and needlessly verbose.
(That's actually a reason not to learn K / APL: When you actually grok it, it's hard to take modern software engineering seriously. A reason to grok them is that -- for some problems -- you're going to become much more productive).
What I can say however is that there's a difference between conciseness in writing and productivity in reading.
For instance,
(!R)@&{&/x!/:2_!x}'!R
It's very concise but very hard to grasp quickly. The equivalent, in even 10 lines of code, might be way easier /and/ faster to read and understand.That being said, I really like the functional aspect of it where most of common operations on list were shortened. (I.e. map, reduce, etc.)
Personally, I think the Arc language strikes a really fine balance of verbose versus concise. It's just unfortunate that there's not a more active community and that it lacks the battery to be productive with it.
Once you've done that, the theory is that it is actually quicker and easier to grasp than reading "verbose" lines of code.
That said, I've never taken the time to invest in an APL style language, so I can't speak to whether it is actually true or not.