Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Waiting for someone to post a 4-symbol APL version (which makes use of a language feature added by an eccentric hedge fund millionaire in 2002 and removed in 2017, to ensure nobody ever runs it).


According to GPT-4, an equivalent APL one-liner is:

(⊢⌷⍨∘⍋∘≢¨)↑1<≢¨⊢⌸(⍋⊢)¨⎕NGET'/usr/share/dict/words'1


AI-assisted APL could totally be a thing.

Why shouldn’t my programs be intense neutron stars of weird symbols, if there’s a superhuman intelligence always at hand to explain and improve the code?


Assembly language generation by symbolic AI (compilers) has been here for 60 years. The "prompts" are very precise and predictably behaved, but requiring the users to learn a specialized language. (Most such languages tend to be concerned with the "how" rather than "what".)

With the new AI, you just specify the "how". Then you get a buggy program, in one of the above specialized languages for the old AI, and the rest of the prompts in the chat are edit instructions on how the AI should fix the code to make it work.


That doesn't work in Dyalog APL, comes back with RANK ERROR.

It's idiomatic to pick ⊃ the first result of ⎕NGET to get just the lines to work on, and not the other things like file encoding.

Then grade-up-right-train-each (⍋⊢)¨ is redunant, it's the same as grade-up-each ⍋¨

The grade is an array of which indices to take to put the argument in sorted order and I don't think it makes sense to group ⌸ by that since the grade isn't the same for different arrangements of letters, so the whole approach breaks down there. I think the words have to be sorted, e.g to pick out 'bat' and 'tab' as the same letters:

          {⍺, ≢⍵}⌸{⍵[⍋⍵]}¨words ← 'bat' 'dog' 'tab' 'cow' 'wok'

    abt 2
    dgo 1
    ...
Then 1<≢¨ would be "1 is less than the count (tally) of each" which fits somewhere in the solution, but not there and won't work on my array. We don't need to know the actual sorted letters so we can inline the bitmask of which answers matter or not in the first column:

          {(1<≢⍵),⍵}⌸{⍵[⍋⍵]}¨words ← 'bat' 'dog' 'tab' 'cow' 'woka'
    ┌→────┐
    ↓1 1 3│
    │0 2 0│
    │0 4 0│
    │0 5 0│
    └~────┘
Any with a 1 in the first column are anagrams, and 0 in the first column are not. And the other columns are indices into the wordlist where the matching words are, so words[1,3] picks out 'bat' and 'tab' and it's probably possible to filter rows with 1 in the first column:

          {⍵[;1]⌿⍵}{(1<≢⍵),⍵}⌸{⍵[⍋⍵]}¨words←'bat' 'dog' 'tab' 'cow' 'bta' 'racecar' 'carrace'
and then drop the first column:

          1↓[2]{⍵[;1]⌿⍵}{(1<≢⍵),⍵}⌸{⍵[⍋⍵]}¨words
Then uhh filter out the zeros to avoid index error, and index into the word list, so:

          {words[⍵/⍨⍵>0]}¨⊂[2]1↓[2]{⍵[;1]⌿⍵}{(1<≢⍵),⍵}⌸{⍵[⍋⍵]}¨words←⊃⎕NGET 'wordlist.txt' 1

    ┌→───────────────────────────────────────────────────
    │ ┌→────────────┐ ┌→────────────────┐ ┌→────────────┐
    │ │ ┌→──┐ ┌→──┐ │ │ ┌→────┐ ┌→────┐ │ │ ┌→──┐ ┌→──┐ │
    │ │ │aah│ │aha│ │ │ │aahed│ │ahead│ │ │ │aal│ │ala│ │ [...]
    │ │ └───┘ └───┘ │ │ └─────┘ └─────┘ │ │ └───┘ └───┘ │
    │ └∊────────────┘ └∊────────────────┘ └∊────────────┘
    └∊───────────────────────────────────────────────────
    
It can probably be done shorter and cleaner with more skill than I have. It's a lot quicker to execute than the PowerShell version I commented, but took a lot longer to code.


(⊢⊢⍤/⍨1<≢¨)({⍵[⍋⍵]}¨⊢∘⊂⌸⊢)words or something like that maybe


Ah!

(That expands to {⍵[⍋⍵]}¨words to sort each word, then use that on the left of Key ⌸ with words on the right, and Key feeds the count and indices into the custom function which is ⊢∘⊂ that takes the indicies with right-tack ⊢ and throws away the count, and encloses them with ⊂. That gives nested arrays of words which sort the same, including invididual words that sort like nothing else. Then (⊢⊢⍤/⍨1<≢¨) added to the left is counting the words in each nesting and filtering out the single ones, and I think ⊢⍤/ is a bodge to use compress in a train without it being misread as reduce when both use the same symbol / ?)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: