TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Suggestivity and Idioms in APL

74 pointsby mr_tyzicabout 2 years ago

3 comments

mk12about 2 years ago
For anyone looking to get into array programming, I&#x27;d recommend <a href="https:&#x2F;&#x2F;mlochbaum.github.io&#x2F;BQN&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mlochbaum.github.io&#x2F;BQN&#x2F;</a>. I&#x27;m no expert but I had a lot of fun using it for Advent of Code last year. I found it to be a lot more sensible and modern feeling than J (the only other one I&#x27;ve tried).
mikercampbellabout 2 years ago
I walked in, realized just how advanced this was and then proceeded to read through the same way I pan through the Abstract Art section of the museum.<p>I freaking love APL and would love to learn it, but it is so incredibly jarring not knowing anything about the syntax.
评论 #35867318 未加载
评论 #35870758 未加载
评论 #35869494 未加载
评论 #35870293 未加载
jcritesabout 2 years ago
Not knowing APL, I unfortunately find the beginning code completely unreadable:<p><pre><code> ⍸(⊃⍵)@0⊣¯1⌽0 1⍷⍵ </code></pre> [Edit: My thought process here is: &quot;This syntax is so difficult that I can&#x27;t even begin to reason about it even using web search. Can GPT understand it? Yep, it seems to.&quot;<p>&quot;Could this algorithm be more clearly expressed in another language? Yes, it looks like it.&quot;]<p>----<p>I asked ChatGPT (4) to explain this expression (Prompt: &quot;Explain what this APL code does: ___&quot;). In case others find it helpful, the response is below.<p>First, let&#x27;s break down each symbol:<p>1. ⍵ represents the right argument of a function.<p>2. ⊃ is the disclose or first function, which picks the first item of its argument.<p>3. ⍷ is the &quot;where&quot; function, which returns a boolean mask of where its left argument appears in its right argument.<p>4. ⌽ is the reverse function. With the argument ¯1, it rotates its argument one place to the right.<p>5. @ is the &quot;at&quot; function, which modifies certain elements of an array. (⊃⍵)@0⊣ replaces zeros in its right argument with the first element of ⍵.<p>6. ⊣ is the left function, which returns the left argument, ignoring the right one.<p>7. ⍸ is the &quot;where&quot; function, which returns the indices where its argument is true (or in the case of numeric data, non-zero).<p>Now, let&#x27;s combine these to understand the whole expression:<p>0 1⍷⍵ returns a boolean mask of where the sequence 0 1 appears in ⍵.<p>¯1⌽ then rotates this mask one place to the right.<p>(⊃⍵)@0⊣ replaces zeros in the rotated mask with the first element of ⍵.<p>Finally, ⍸ returns the indices of non-zero elements in the result.<p>So, this code is finding the indices in the array ⍵ where a 0 is immediately followed by a 1, and replaces these 0s with the first element of ⍵. It then returns the indices of the non-zero elements in the result.<p>[Edit: I&#x27;m not entirely sure but this explanation might contain an off-by-one error. I don&#x27;t <i>quite</i> follow APL enough to tell. The translated alternative does for sure though.]<p>----<p>On that note, I&#x27;ll remark that I find APL&#x27;s syntax to have distinct disadvantages. I know a number of programming languages well, having written production code in them for decades. I&#x27;ve come across basically every mainstream programming language and find them generally readable, even though I don&#x27;t have much domain knowledge, because they share so many concepts that are typically reflected with similar syntax (for loops, assignments, functions, etc.) Even relatively more &quot;out there&quot; languages like Haskell and Perl are somewhat readable without a ton of domain knowledge.<p>I find it very difficult to get on board with languages that value terseness of this sort over readability (like in K and APL – especially with APL&#x27;s special symbols). Code is read so much more than it&#x27;s written, in general, that transparency is valuable and important – as well as the ability to search for code fragments to understand their meaning.<p>I asked ChatGPT4 to translate this code into Rust, for example, to see what it would come up with. Here was its literal translation, that relies on mutating the input (analogous to the APL code). This is similar to what you&#x27;d get transliterating the code into C&#x2F;C++&#x2F;Java:<p><pre><code> fn find_zero_one_and_replace(input: &amp;mut Vec&lt;i32&gt;) -&gt; Vec&lt;usize&gt; { let first_element = *input.first().unwrap_or(&amp;0); let mut indices = vec![]; let mut prev_zero_index = None; for (i, &amp;item) in input.iter().enumerate() { if item == 0 { prev_zero_index = Some(i); } else if item == 1 { if let Some(index) = prev_zero_index { input[index] = first_element; indices.push(index); prev_zero_index = None; } } } indices } </code></pre> [Edit: I haven&#x27;t checked whether this is correct]<p>After some back-and-forth, asking it to write a function that doesn&#x27;t mutate the input, and uses iterators, it comes up with some code that I think is probably readable even if you don&#x27;t know the language well (barring some particulars of the syntax):<p><pre><code> fn find_zero_one_indices&lt;&#x27;a&gt;(input: &amp;&#x27;a [i32]) -&gt; impl Iterator&lt;Item = usize&gt; + &#x27;a { input.windows(2) .enumerate() .filter_map(move |(i, window)| { if window == &amp;[0, 1] { Some(i+1) } else { None } }) } </code></pre> [Edit: This code fails to detect the leading group of `1s` in the input and return a response beginning with `0`. Corrected code might be:]<p><pre><code> fn find_zero_one_indices&lt;&#x27;a&gt;(input: &amp;&#x27;a [i32]) -&gt; impl Iterator&lt;Item = usize&gt; + &#x27;a { let mut previous = 0; input.iter().enumerate().filter_map(move |(i, &amp;x)| { let result = if previous == 0 &amp;&amp; x == 1 { Some(i) } else { None }; previous = x; result }) } </code></pre> [Edit: The corrected form is arguably less readable than the original due to the need to detect a leading window that doesn&#x27;t start with `0`. This version somewhat weakens my argument about the clarity of the alternative, but this is still readable. Perhaps someone else with more Rust experience can suggest a simplification.]<p>I find this readable since &quot;windows&quot; here is a direct reference to the concept of windowing, which is collecting sequences of items from a stream into groups. &quot;Enumerate&quot; iterates over a pair of sequence elements and their indexes: `(i, window)`. These concepts exist in a number of languages.<p>But if you didn&#x27;t know what these code elements meant, you could search for them: &quot;rust windows&quot;, &quot;rust enumerate&quot;, &quot;rust filter_map&quot; and get useful results. The stdlib documentation will be the first or second link. For example, if you don&#x27;t know what &quot;filter_map&quot; does: <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;trait.Iterator.html#method.filter_map" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;trait.Iterator.html#metho...</a> (And the same would be true for Java, Python, etc.)<p>Each of these elements (windows, enumerate, filter) is syntactically joined into a conceptual pipeline, providing a framework for reasoning about how they work, in a paradigm used by many languages.<p>Finally we have the detection logic: `if window == &amp;[0, 1]`. Hopefully that&#x27;s readable, and I expect makes this solution easier to understand than any of the other variants.<p>I may have somewhat missed the point of the article: it goes on to describe how the solution can be generalized in various ways. But this is also likely true of the canonical solutions in many languages: iterators (in particular), anonymous functions, etc. enable code snippets to compose and generalize in many ways as well.
评论 #35867839 未加载