37 comments

[ 3.3 ms ] story [ 79.1 ms ] thread
This is the full K program:

  point: {(y;pn[f%20;0.3*x;0.1*y]*-18*sin y*pi%100)}
  line:  {((30;x)+)'point[x]'!100}
  draw:  {,(,/line'38+6*!16;cga;1)}
Not having much of an idea what I was looking at there, I asked GPT-4 to explain this to me. Quite helpful (though it didn't seem to know what "pn" was for)

----

This K program appears to be defining three functions (point, line, and draw) that are related to drawing a mathematical pattern.

point function: point: {(y;pn[f%20;0.3x;0.1y]-18sin ypi%100)} This function takes two arguments: y and a variable function pn[f%20;0.3x;0.1y]. The function computes a value based on these arguments and returns the result. The expression (f%20;0.3x;0.1y) inside the pn function seems to generate new values for x and y based on the input x and y. The final result is multiplied by -18 sin(y * pi % 100), which can create interesting patterns depending on the input values.

line function: line: {((30;x)+)'point[x]'!100} This function takes a single argument, x, and computes the result of the point function for 100 evenly spaced values of y in the range 0 to 1 (by indexing the point function with x and each value in the range '0 to 99'). The results are then shifted by (30; x) to create a new set of coordinates. The function returns a list of these transformed coordinates.

draw function: draw: {,(,/line'38+6*!16;cga;1)} This function generates a sequence of 16 values (from 0 to 15) and multiplies each by 6, then adds 38. It then computes the line function for each of these values and concatenates the results. The final expression (cga; 1) seems to be related to the color or other visual properties of the pattern, but it is not clear from the given code snippet how it is being used.

Overall, this K program appears to be generating a list of coordinates for a complex pattern by applying transformations to a series of points. The specific pattern depends on the input values and the function pn, which is not fully defined in the provided code snippet.

pn is perlin noise. It's not part of the k standard, but provided by the iKe environment here. iKe is also providing 'f' which is the frame counter, and pi which is obviously pi.

It's a bit confused by the first function. It takes two arguments, x and y, and returns a tuple. The first value is 'y' and the second value is (sin(pi % 100 * y) * -18) * perlinNoise(frameNumber % 20, 0.3 * x, ), 0.1 * y). I might be a little off on that formula, it's been awhile since I've read k code.

(comment deleted)
> The function computes a value based on these arguments and returns the result

The Eternal September of AI has come earlier than expected.

This is the kind of code golf I didn't know I needed.
(comment deleted)
Is the terseness of the programming language supposed to blow my mind? I know nothing of K, other than my quick STFW, which says it is some kind of proprietary language. What would that look like in say Python, I wonder?
for reference, golfed k code is typically 1/2 to 1/3 the size of golfed python code, which would be somewhat hard to read for a python programmer unfamiliar with golfing tricks. probably around 1/4 or 1/5 the size of ordinary python code.
Do you mean normal (idiomatic) k code is 1/2 to 1/3 of golfed Python? Because that seems more accurate. This code by John is not golfed.
the terseness isn't supposed to blow your mind, it's an artifact of k's iversonian heritage. the first iversonian language, apl, was originally intended as a kind of mathematical notation for computer programs, a compact notation for defining algorithms-as-mathematical-objects. imagine a programming teacher writing code on a chalkboard to the same degree a math teacher writes math on a chalkboard and the need for compact notation becomes obvious, i think.
I've also had animated unknown pleasures from a few lines of ketamine.
Yeah my first reading of that title caught me off guard. "I don't think anyone needs that much, but you do you and also why is this on the HN frontpage?"
Doesn't actually do the thing that makes the Unknown Pleasures cover somewhat nontrivial difficult to render, which is to let foreground "waves" obscure background waves.
Also, the original waves are much higher in the middle third and almost flat on the outer thirds:

https://i.imgur.com/nlxdkGb.jpg

Indeed they are, as they're recordings of Pulsar CP 1919:

>“EIGHTY SUCCESSIVE PERIODS of the first pulsar observed, CP1919 (Cambridge pulsar at 19 hours 19 minutes right ascension), are stacked on top of one another using the average period of 1.33730 seconds in this computer-generated illustration produced at the Arecibo Radio Observatory in Puerto Rico. Although the leading edges of the radio pulses occur within a few thousandths of a second of the predicted times, the shape of the pulses is quite irregular. Some of this irregularity in radio reception is caused by the effects of transmission through the interstellar medium. The average pulse width is less than 50 thousandths of a second.” From “The Nature of Pulsars,” by Jeremiah P. Ostriker, Scientific American, January 1971.

https://blogs.scientificamerican.com/sa-visual/pop-culture-p...

Not that nontrivial, I did that in my very first program for the bbcmicrobot

https://twitter.com/bbcmicrobot/status/1604784926032740352

    1MO.4:DIMR(4)
    2F.Y=820TO120S.-20
    3GC.0,1:MOV.0,Y:DR.1279,Y:F.L=1TO4:R(L)=RND(120):N.L
    4F.M=0TO1:GC.0,1-M:Z=Y-8*M:F.L=1TO3:A=200+L*150:MOV.A+R(L)+90,Z+160-  R(L+1):MOV.A,Z:PL.81,300,0:N.L:N.M
    5N.Y
    9PRINT TAB(13,29);"BASIC PLEASURES"
You can click through in the 'Source' link in the tweet and see how it did that - by painting the image filled in white first then filled in black slightly lower.

(Re your point about the heights of the peaks - I actually adjusted my program to do this immediately after posting, and IIRC posted that too, but it was the first attempt that got all the likes/rts. I tended to do this all the time with these-just post the very first version that barely works, then regret it. I'm still doing that, this version of the vertigo poster... https://hachyderm.io/@bbcmicrobot@mastodon.me.uk/11010756246... was quickly followed by the fixed one... https://hachyderm.io/@bbcmicrobot@mastodon.me.uk/11010797875...)

(comment deleted)
Well of course it can be solved. My point is that it's not just a matter of rendering a bunch of transposed plots.
It's a matter of rendering a bunch of transposed points as a path and then applying a fill.

Not particularly different, other than that I assume OP's challenge was to see how tiny they could make this and that might expand the code size.

Well that's exactly my point, if you simplify the rules like that, of course the program becomes very short.
Here's the author's original forum post with more information about APL/J/K.

[1] https://forums.somethingawful.com/showthread.php?threadid=37...

Although it's worth noting that the information is somewhat outdated. Notably, the most recent version of K is K9 (referred to through its proprietary implementation called Shakti) although K3 (through Kona), K4 (through Q by KX systems), K5 (through Ngnk), and K6 (through oK) are still used as every few versions is somewhat different from what came before, including being rewritten from scratch.
(comment deleted)
Maybe unrelated, by 3 mentions of Joy Division on the front page of Hackernews at the same time ? What gives ?
Topics definitely come in waves. Sometimes I see link submissions that serve as support or rebuttals to other recent links. Sometimes an interesting comment even leads to related links hitting the front page.
I've been playing through Metal Gear Solid with my son, seemed on cue to me. (Joy Division is Kojima's favorite band.)
I’ve been waiting for a guide to come and take me by the hand…
I've been wanting to get around to learning an apl-like language like K for a while. Is there any favorite starting points for anyone who's done this?
I have tried to learn J, it seems to be one of the most well documented.

And Ike itself is not a bad environment to play around with K https://johnearnest.github.io/ok/ike/ike.html

It has a repl as well in: https://johnearnest.github.io/ok/ you can find source here: https://github.com/JohnEarnest/ok and manual here: https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual....

If this is the entirety of K - https://kparc.com/k.txt it does fit on a single full-screen page.

check out oK[0] by John Earnest, who is the author of the content of this post.

it has a well-written manual and is a great jumping off point. the source is written in ~1000 lines of js. so you can look at how each primitive is implemented.

there is a k-enthusiast element.io server[1] where you can ask any question you like. folks are friendly!

EDIT: another commenter shared most of this info. oops :-) i'll add as well ngn-k browser repl[2] which has some docs and a fuller feature-set than oK. this is probably the most-used open source k right now. the author is active in the above element server, and still patches bugs and makes updates.

[0] https://github.com/JohnEarnest/ok

[1] https://matrix.to/#/#aplfarm-k:matrix.org

[2] https://ngn.codeberg.page/k/#

For anyone unaware, "Unknown Pleasures" is the name of the debut record (1979) from the English band "Joy Division".

The cover was a stylized visualization of a fast Fourier transform, which this is replicating.