9 ms·
Thinking in an array language (2022)
- rekttrader 3y agoThank you for posting this, I just this week tried to explain array programming.
- account-5 3y agoIf you never heard of array programming and are interested in an introduction I recommend: The Array Cast https://www.arraycast.com/episodes/ https://www.arraycast.com/episodes/ RSS address: https://www.arraycast.com/episodes?format=rss https://www.arraycast.com/episodes?format=rss
- lolinder 3y agoI listened to the first ~5 episodes of The Array Cast wanting to be persuaded, but I just couldn't buy in. They downplayed the terseness and non-ASCII symbols of the array languages as something you get used to and something that's worth it to get the benefits of array languages, but the benefits they listed were mostly ones that I'm already very familiar with from working with higher-order functions in most mainstream languages today. It felt like the hosts had missed that map/filter/reduce have gone mainstream and are now available ~everywhere without having to learn a whole logographic writing system first.
- nickpeterson 3y agoArray languages come up a lot on HN so I’ve looked into them a few times. I think it’s a hard thing to gauge without trying to use them in anger for a while. One of the core arguments for them that resonates with me is that once you understand the operators, many patterns become so terse and repeat so often, you can refactor large portions of code very rapidly. I use a lot of F#, and can see the beginnings of this where translating from C# to F# often shrinks the code size to the point where I see patterns that weren’t obvious before. I believe APL, K, J, Nial, BQN, etc all have more extreme versions of that happening. It also makes differences stand out so logic bugs are more visually obvious.
- ParetoOptimal 3y agoI can corroborate: That same feeling of as simple and terse code as possible happens in Haskell too. However the most popular professional style of Haskell code is far too verbose for me to be able to "see the essence" in many cases without a round of refactoring. Its far worse and far more grating for Java code that makes you do so much for so little.
- steveBK123 3y agoSometimes the benefit of shorter codebases is that you can actually see the code. Understanding that your logic bug is obviously somewhere within a handful of lines of K/Q/etc that fit neatly on a single screen vs scrolling through 200 lines of logic in Python/Java/whatever is focussing. Sure each line is more compact and may take more time to digest, but that is a reasonable trade-off in most cases. For me, 10 lines you need to read carefully beats 100 lines of having your eyes glaze over and repeatedly skip over the bug because you are so dulled to all the boilerplate.
- rak1507 3y agoAs an array language fan I also feel like a lot of the reasons people give for why array languages are powerful are outdated, and sometimes (not all the time) that's because someone tried both APL and C in the 1970s and their opinions haven't changed since. However I still find them useful (and fun!) because of the terseness, not in spite of it. Terseness means array languages are very mathematical in nature + can be really ergonomic to write, especially with the repl. If someone said "learn an array language, they're easy to write on paper" everyone would think they were insane... but there is something to it, in certain cases. The idea of "fearless refactoring" is popular in the FP world, but it can also be true in the array language world too.
- bear8642 3y ago> "learn an array language, they're easy to write on paper" everyone would think they were insane... but there is something to it, in certain cases. Indeed, and sometimes not needing a computer to quickly splash out an idea you're thinking/talking about is extremely useful! Over Christmas, I showed my brother {+⌿⍤2(∘.≤)⍤1⍨⍵} as a small expression to explore attribute to pick playing Top-Trumps admist a discussion. Not something you'd be able to do in many other languages!
- Affric 3y agoCould you explain the expression to us?
- bear8642 3y agoSure - idea is you've got a 6×30 matrix of the 6 card attributes for each of the 30 cards, and want to know for each attribute how many other cards they'd beat. {+⌿⍤2(∘.≤)⍤1⍨⍵} { ⍵} ⍝ single argument (monadic) function with argument ⍵ { ⍨⍵} ⍝ "selfie" - ⍵ f ⍵ where f is (∘.≤)⍤1 { ⍤1⍨⍵} ⍝ on each row (1 dimensional element) { (∘. )⍤1⍨⍵} ⍝ cartesian (outer) product of each row with itself { (∘.≤)⍤1⍨⍵} ⍝ ≤ comparison of those elements - produces 6 30×30 boolean matrices { ⍤2(∘.≤)⍤1⍨⍵} ⍝ on each of those matricies (2 dimensional element) {+⌿⍤2(∘.≤)⍤1⍨⍵} ⍝ sum the columns leading to: {+⌿⍤2(∘.≤)⍤1⍨⍵} ⍝ compare each row (⍤1) to itself (⍨) and count (+⌿) how many cards that beats That make sense?
- ParetoOptimal 3y agoMap/filter/reduce are just primitives. The composability built on top of them and coherent system of using these higher level combinators is where other languages fall short. Other languages support regex for example, but perl takes it to a different level that changes the language experience.
- jodrellblank 3y agoI am often in these threads on HN arguing in defense of the underdog (array languages); I do tend to agree with you here. I think the early days of APL (and Prolog) were that they were high level casually-typed REPL based interpreted languages, in a world of Fortran, PL/1, COBOL and C, and the amazing nature of them back then is stuff we take for granted writing items=[1,2,3] and lookup={'key':'value'} in Python and not worrying about allocating and freeing memory or pre-declaring the variables and their types, the string length, and compiling, and whatever. Still, there is a fun about a desk calculator on so many steroids that it's a Turing-complete programming language. One of the ArrayCast podcast guests said array languages were "like a Domain Specific Language (DSL) with an enormously wide domain". How many mainstream languages will let you fork and hook and have inverses and over and under and recursively descend into anonymous functions, without ceremony or boilerplate code? You could have that now to play with, without waiting for it to go mainstream, just by learning a two or three dozen unfamiliar glyphs - compare that to the effort of learning a whole new framework for making web applications and it's much smaller.
- Keyframe 3y agoThis is how I found out about BQN. I'm still not sure I'd use it in production somewhere. Even though I like it, most array languages seem rather foreign if it's not, I don't know R, NumPy, Julia maybe.. venturing deeper into APL, J, BQN I'd just alienate any support I could hope to recruit.
- andsoitis 3y agoI’m super curious about exploring array languages. What domains have you found it particularly effective, or at the least provides you with a new way of solving a problem?
- theusus 3y agoIsn't lisp a list language? Isn't easy to index but gives similar functionality
- ginsider_oaks 3y agolist languages and array languages are quite different. in LISP, the lists are heterogeneous i.e. each element can contain different types, which is also how you can e.g. make a tree out of lists in LISP. typically in array languages all elements are the same, and you use operations that apply to the whole array e.g. one operation for summing the array rather than a loop.
- lispm 3y agoThere are attempts to combine those... a cool example is April (Array Programming Re-Imagined in Lisp), which runs on top of Common Lisp... https://github.com/phantomics/april https://github.com/phantomics/april One can see that the Lisp macro APRIL-F compiles APL-like code to Lisp. APRIL> (macroexpand '(april-f "3r4J9r5×⍳4")) (LET ((OUTPUT-STREAM *STANDARD-OUTPUT*)) (DECLARE (IGNORABLE OUTPUT-STREAM)) (SYMBOL-MACROLET ((INDEX-ORIGIN APRIL-WORKSPACE-COMMON::*INDEX-ORIGIN*) (PRINT-PRECISION APRIL-WORKSPACE-COMMON::*PRINT-PRECISION*) (COMPARISON-TOLERANCE APRIL-WORKSPACE-COMMON::*COMPARISON-TOLERANCE*) (DIVISION-METHOD APRIL-WORKSPACE-COMMON::*DIVISION-METHOD*) (RNGS APRIL-WORKSPACE-COMMON::*RNGS*)) (A-OUT (A-CALL (APL-FN-S ×) (A-CALL (APL-FN ⍳ INDEX-ORIGIN) 4) #C(3/4 9/5)) :PRINT-PRECISION PRINT-PRECISION :PRINT-TO OUTPUT-STREAM))) T we can run that: APRIL> (april-f "3r4J9r5×⍳4") 3r4J9r5 3r2J18r5 9r4J27r5 3J36r5 #(#C(3/4 9/5) #C(3/2 18/5) #C(9/4 27/5) #C(3 36/5)) It has created a Lisp vector of complex numbers. APRIL> (describe *) #(#C(3/4 9/5) #C(3/2 18/5) #C(9/4 27/5) #C(3 36/5)) [simple-vector] Element-type: T Length: 4 ; No value
- RodgerTheGreat 3y agoIt does not. Array-oriented languages (or Vector-oriented languages, if you like) are generally distinguished by features like concise syntax, implicit conforming/mapping over homogenous collections, and a strong de-emphasis on explicit looping or recursion in favor of abstract (and often parallel) iteration. Some of these features could be implemented- to an extent- in a Lisp, but they do not reflect normal Lisp programming style.
- OskarS 3y agoI have a question about array languages that I've been wondering for a while: lets say you have the task "find all numbers smaller than N where predicate P is true". You know, like, "find all primes smaller than 1000", or "find all pythagorean triplets where Z is less than 1,000,000", something like that. The general pattern for doing this in an imperative language would be to have a for-loop that tests the predicate inside and then does something with the number. In a functional language, you'd use recursion or map/filter on a lazy list, something like that. (to be clear, this is in the general case where you don't know exactly what P is. for the specific problem of finding primes smaller than N, you could obviously use sieves and stuff, but i'm more curious about the general pattern of problems like this) Having only had limited exposure to array languages, my understanding is that in general the way to do that would be to do something like: 1. Generate an array from 1 to N 2. Test the array against the predicate, getting a new array with 0s and 1s (a mask, essentially) 3. Apply that mask to the array to get only elements where the predicate is true Which is all fine, and I imagine that it takes a stupendously small amount of characters to do this. But my question is: what if N is large, and the predicate isn't true very often? Like, what if N is a billion or something? This is not an issue in the imperative/functional cases, because they don't have to generate the array in step 1, they can just loop/recurse/lazily generate the values (i.e. memory is O(1) for this). But it seems extraordinarily wasteful in array languages both of memory and resources to generate two different billion element arrays (the 1..N array and the mask) just to get out a handful of values. That question is pretty specific, but this is one of my biggest questions about array languages in general. Aren't you always generating TONS of temporary arrays for no real reason, that will just pass through the calculation? Isn't that really slow? Or does the implementation somehow optimize this stuff away, maybe by using something like lazy evaluation?
- beaumayns 3y ago[dead]
- nickpeterson 3y agoI think you might think the array instantiation is literal. Nothing prevents an array language from handling it in chunks under the covers. If I ask it to give me an array of 10 billions integers, it may not naively do that. Not an expert , just a thought.
- andoando 3y agoLooking at the example, is there a point to this? Is it more performant in anyway? The syntax for matrix multiplication is shorter, but that’s only because there is a bunch of built in context you have to keep in your head about how the K language works.
- maest 3y agoIt's more concise, which has value. In particular, think about how maths really consists of packing more and more concepts into higher level definitions. This is similar: higher level concepts become primitives and allow you to think faster and build more complex objects.
- andoando 3y agoI can see that, though it really depends on what these abstractions are, how complete they are in solving in a variety of problems, or I suppose, how frequent the problems it makes easier in relation to ones it makes harder. I’d have to know a lot more and spend a lot of time before judging. But I am not impressed seeing a single example. I could hypothetically write a language where mergesort is the operator * and reversing an array is the operator + and showcase how in my language 30 lines of code becomes 1. That would be insanely nice if you’re doing something which just requires thinking about sorting and reversing arrays but all together, very poor thing to build a standard language on.
- jodrellblank 3y agoThat's what people do on codegolf.stackexchange.com with the dedicated golfing languages. What's interesting about APL is that it started out as linear algebra and became array programming, and the primitives are more "things which are useful transforms to do on arrays" rather than "thing people wanted to do in one problem turned into a symbol", and that's enough to be useful in many tasks. > "That would be insanely nice if you’re doing something which just requires thinking about sorting and reversing arrays but all together, very poor thing to build a standard language on." Prolog has linked lists as its main data structure, which means prepend is more performant than append. In the SWI Prolog documentation for foldl[1] it says "No implementation for a corresponding foldr is given. A foldr implementation would consist in first calling reverse/2 on each of the m input lists, then applying the appropriate foldl. This is actually more efficient than using a properly programmed-out recursive algorithm that cannot be tail-call optimized". i.e. you don't have to have a problem which involves reversing arrays, for reverse to be a useful part of a solution. In APL reverse is one character ⌽. In C# to find the first comma in a string you use IndexOf(',') and to get the last one you use LastIndexOf(','). In APL you would use the equivalent of indexOf(Reverse(string)) and then you wouldn't need the specialised LastIndexOf - and wouldn't every other function which works from the left of a string or array to have an equivalent which works from the right. https://aplcart.info/ https://aplcart.info/ is a database of idioms and samples of useful APL and it has 168 pieces of code using ⌽. [1] https://www.swi-prolog.org/pldoc/man?predicate=foldl/4 https://www.swi-prolog.org/pldoc/man?predicate=foldl/4
- cmrdporcupine 3y agoShould be interesting to see what people will do the the RISC-V (V)ector extension with array languages. I would think there is a pretty straight 1:1 conceptual correspondence there, and one could write a pretty nifty compiler to go from an APL-ish thingy to those instructions, and get some serious oomph.
- dzaima 3y agoWhile RVV still makes for a good target for arraylang implementations, RVV isn't really much closer to arraylangs than AVX-512, or really any SIMD if you squint hard enough (the scalability of RVV/SVE is unrelated as you still need to have loops; and the existence of VL, while nice, doesn't give any new fundamental possibilities). Basic list operations might have a roughly-1:1 mapping to simple RVV loops, but many others still require a decent bit of extra code (all search functions (hash- or lookup tables), narrow matrix ops (processing multiple rows at a time); even compress operations get questionable when there's multiple of them due to their variable-length nature).
- p1esk 3y agoIs Numpy or Pytorch an array language? With things like einsum and elaborate slicing/indexing mechanisms.
- 7thaccount 3y agoMore like an array framework/library on top of a general purpose Object Oriented language.
- Qem 3y agohttps://analyzethedatanotthedrivel.org/2018/03/31/numpy-another-iverson-ghost/ https://analyzethedatanotthedrivel.org/2018/03/31/numpy-anot...
- sega_sai 3y agoI don't know, my personal/biased/limited impression from solving K language problems for the job interview is that the language is on purpose obtuse. It's a nice language for puzzles and clever solutions. But IMO, working with numpy arrays in Python -- that's what teaches you array language, and how to think in arrays.
- gitonthescene 3y agoWhere were you interviewing?
- sega_sai 3y agoDB (10 years ago)
- gitonthescene 3y agoI’m guessing you had K on your CV.
- sega_sai 3y agoI did not. I was applying from academia for quant position ( I got an offer but did not take it). Solving a bunch of K problems was one of homeworks.
- Bostonian 3y agoFortran has the matmul intrinsic function, and you can define an operator .x. so that a .x. b is equivalent to matmul(a,b). APL, J, and K are not unique in being able to express linear algebra operations concisely.
- pklausler 3y agoYou can’t define a generic interface in terms of another generic interface in Fortran. To do what you suggest in actual Fortran, you would have to write specific functions for all the combinations of data types and ranks supported by MATMUL (hundreds).
- mlochbaum 3y agoPrevious discussion: https://news.ycombinator.com/item?id=31377262 https://news.ycombinator.com/item?id=31377262
- dang 3y agoThanks! Macroexpanded: Thinking in an array language - https://news.ycombinator.com/item?id=31377262 https://news.ycombinator.com/item?id=31377262 - May 2022 (66 comments)
- gcanyon 3y agoThis is based on K. Another array language is J http://jsoftware.com http://jsoftware.com In J this would be: dot =: +/ . * P =: 2 3 4 Q =: 1 0 2 P dot Q That will return 10, the dot product P and Q.
- eismcc 3y agoIn KlongPy, dot product is: dot::{+/x*y} P::[2 3 4] Q::[1 0 2] dot(P;Q)
- __marvin_the__ 3y agoTIL of KlongPy (and Klong). Looking at the project page, I think it looks really cool. https://pypi.org/project/klongpy/ https://pypi.org/project/klongpy/
- nils-m-holm 3y ago> dot(P;Q) You can also write P dot Q.
- abrudz 3y agoThe original array lang is APL: dot←+.× but why give it a name when spelling it out is as short as the shortest reasonable name for it (and then you might need to put spaces around the name).
- gcanyon 3y agoFor people who understand the language, there is little point to naming it. For illustrating to people unfamiliar with the language, it helps to break apart the "this is the thing I'm talking about" part from the "here's how you would use it" part.
- akavi 3y agoI guess I still fail to see the advantage of this over Haskell: dot = (sum.) . zipWith (\*) p = [2, 3, 4] q = [1, 0, 2] p `dot` q Like, the only difference to my eyes is using names for sum/zipWith, and not having the lifts/restructuring happen "magically"
- refset 3y agoNothing has ever convinced me about the potential for array languages in practice quite like watching Aaron Hsu describe how he develops his parallel APL compiler [0] using two Notepad.exe windows side-by-side: https://www.youtube.com/watch?v=gcUWTa16Jc0&t=860s https://www.youtube.com/watch?v=gcUWTa16Jc0&t=860s He has also written many comments (arcfide on HN) about this stuff before, e.g. discussing "semantic density" [1] > The compiler is designed so that I can see as much as possible with as little indirection as possible, so that when I see a piece of code I not only know how it works in complete detail, but how it connects to the world around it, and every single dependency related to it in basically one single half screen full of code (usually much less than that) without any jumps, paging, scrolling or any movement. [...] The idea of semantic density is critical to this point. The semantic density of the APL code I'm using to solve the problem is at a certain rate. I maintain a consistent density rate by choosing my variable names in such a way that they visually align with the expressivity per character of the built in primitive symbols. And > [...] idiomatic programming methods that are so concise, they can begin to be read as we read and chunk English phrases. By doing so, it becomes actually easier to just write out most algorithms, because the normal name for such an algorithm is basically as long as the algorithm itself written out. This means that I start to learn to chunk idioms as phrases and can read code directly, without the cost of name lookup indirection. I can get away with this because I've made reusability and abstraction less important (vastly so) because I can literally see every use case of every idiom on the screen at the same time. It literally would take more time to write the reusable abstraction than it would to just replace the idiomatic code in every place. [0] https://github.com/Co-dfns/Co-dfns https://github.com/Co-dfns/Co-dfns [1] https://news.ycombinator.com/item?id=13571159 https://news.ycombinator.com/item?id=13571159
- baseball_coach 3y agoThey have to write the crazy paragraphs because the code is ugly to look at, so they have to spend 18 hours convincing someone to use it. They could choose symbols that don't look so ugly next to each other, then they would not have to spend as much time convincing people the language does not suck.
- 3y ago
- gcanyon 3y agoSeveral people here are questioning the utility and/or the comprehensibility of this. I'm no expert on array languages -- I've played with J and solved maybe a few dozen project Euler problems with it, but here's my take: 1. Array languages aren't well suited to *every* problem. 2. They *are* ridiculously capable for many types of problem. 3. The people using array languages are generally insanely smart. 4. Learning how array languages work is a steep challenge. As shown in the linked article, it's possible to write "procedural" code in an array language, and that is a Very Bad Thing. It's like using a screwdriver as a hammer. 5. In particular, understanding how tacit programming works is an awesome mind-expander 6. Likewise when you internalize verb trains 7. Likewise when you realize how array-based languages handle *any* dimension array. 8. Likewise when you comprehend how "under" works 9. Likewise when you understand how function exponents work There are many other awesome aspects of array languages; the above are just a few.
- deleted 3y ago[deleted]
- zozbot234 3y agoTacit programming is not unique to array languages, many FP and FP-like languages support it. Generally array programming languages will be a bit terser if you're working in problem domains that are idiomatic for the language, but FP languages can be more general and extensible. The "people using these languages are so smart" attitude is often criticized as it can devolve into gatekeeping, but at least FP communities make somewhat of an attempt to be friendly to outsiders.
- rak1507 3y agoFWIW I think array language communities are generally very friendly and helpful too. Not many programming languages/companies employ people to help people in chatrooms.
- mlochbaum 3y agoComplain about the language or documentation, but array language communities are absolutely friendly to outsiders! This tutorial lists some active forums, give them a try: https://github.com/razetime/ngn-k-tutorial/tree/main?tab=readme-ov-file#how-to-use-this-tutorial https://github.com/razetime/ngn-k-tutorial/tree/main?tab=rea... Also, have to shout out the APL Orchard, where Adám will give any visitor a personal tutorial: https://chat.stackexchange.com/rooms/52405/the-apl-orchard https://chat.stackexchange.com/rooms/52405/the-apl-orchard
- pklausler 3y agoI discovered APL/APL2 in the 70's (using actual overstrikes on a paper terminal) and fell instantly in love with it. But when I later discovered functional programming with ML and Haskell, I realized that what I really enjoy about APL had little to do with arrays, but more with its ability to compose functions. Haskell is so much better at this, being completely pure and typed throughout, and is even more fun and powerful than APL; I've used it to write a lot of small and medium-sized projects, prototype LLVM Flang's parser to prove the concept of using parser combinators for Fortran, and do Advent of Code each year in a few hundred lines of code total. If you like APL, try Haskell. Today, I think that the whole "notation as a tool of thought" aspect of APL is a rationalization for excessive brevity -- which can be a good way to demonstrate the power of composition, but which can work against clarity.
- jim-jim-jim 3y agoYes. I'm a broken record on this topic, but I largely stopped messing with J and K once I got decent at writing point-free Haskell. Verb trains are even more powerful with functors in the mix. <=< is already available, and once you write an equivalent for fmap, you really start cooking. |||, +++, &&&, and *** are great too. And you can always write UTF-8 operators to make it even shorter and prettier. It's unfortunate that the serious Haskell I've seen at work/in the wild is rarely friendly to vertical screen realestate in this way.
- tsegratis 3y agoCould you link your advent of code source please?
- icsa 3y agoThe most significant Aha! moments that I've had from using array languages (mostly k) were: - Verbs are algorithms. Imperative and object-oriented languages require the implementation of common algorithms (e.g. find, sort, and group). - A sequence of verbs (or adverbs) is the most direct form of composition I've ever used. Composition is easy and fluent. - Programs are the composition of algorithms and not a collection of statements & expressions. - The concept of domain and range treated uniformly across arrays, maps, and functions simplifies my design choices. - Left of right evaluation means that my eyes don't have to "jump around" when reading code. - Sending code to data is possible & preferred. Most of my significant k projects fit in a network MTU (i.e. 1540 bytes), not including comments. K bonus: - Views (i.e. dependencies) can implement functional relationships directly. - Hot code (via the interpreter) loading makes "forever" applications possible.
- usgroup 3y agoI have some experience here and I spent 50 hours or so on J, but frankly I found the paradigm too biased. I’m not sure it helps —- as a tool of thought —- to conceive every problem as a nesting of arrays. IMO, having freedom to produce data structures which capture your problem can greatly simplify the algorithmic part. I think you do have to be smarter to use APL/J/K because of this bias: it’s often the case that a direct approach available in a more flexible language will not be available, and you have to transform the problem, which may require a lot more thought.
- oldandtired 3y agoThere are all sorts of reasons for using any language and there are just as many reasons for not using that language. But the problem is not the terseness or the relevant clarity of any language or its ability to compile to fast code. It is the ability of any programmer who comes along later to make changes to that code in support of its use. All too often, programmers want to show their [leet] skills and fail to take into consideration the poor sods who have to come after to support that code. The reality is that much [leet] code has to be discarded or completely rewritten to get something that is supportable long term. It took me a long time to understand this and after this, I made sure that my code could be supported by others - clean, simple and comprehensible. Too much throwaway code becomes the basic infrastructure used in organisations and becomes entrenched and incomprehensible to following generations.
- poulpy123 3y agoI'm still not convinced that these languages are not an evolved prank
- wslh 3y agoGood point, also not convinced while following the topic. I also received an APL manual from an economist using APL in the 70/80s. It was a gift. Regarding interesting programming paradigms I am biased toward SMTs and things alike where you just specify your problem and a solution and tests are generated. Not saying that you can build a big part of a system using logic now but I think will be evolving to more areas of expertise in the future. In a way, from a software engineer point of view, I see AI in the same vein: a black box that could produce interesting results for speeding up software development.
- tdeck 3y agoOne thing I always wondered: how do these codebases pan out in terms of maintainability? Are there J/K/APL programs with many programmers working for many years on the same code, with people joining and leaving the project? Do they continue to follow the same terse style that is beloved by most users of these languages?
- up2isomorphism 3y agoI like array language. But I do not feel this article represents why “matmul: (+/)\:” is better than matrix matmul(matrix A, matrix b) very well.