Fear and Loathing with APL
theburningmonk.com
theburningmonk.com
- Philip Greenspun
From http://www.paulgraham.com/quotes.html
I adore languages with a high thinking-to-googling ratio. I've been typing the code in this example. Even though I use my mouse to type APL characters, it feels fast. Really, really enjoyable.
Thanks to the author; this is readable and fun.
Open source and even available on Android so it can be run on a phone.
I think K is a bit more pleasant to read than J because it relies far less on digraphs, but naturally it's a matter of taste.
If anyone knows APL/J/K well, and could show some programs where idiomatic APL/J/K does not directly translate into idiomatic numpy, I'd love to see them.
Q=: 1:`(+&$:/@:- $:@-& 1 2)@.(>&2)"0 M.
ODAT ← {(⊂[1] ¯1 0 1 ⌽ (3,⍴⍵)⍴⍵) ∊ ((8 ⍴ 2) ⊤ ⍺) / ⊂[1] 2 2 2 ⊤ ⌽ 0,⍳7 }
as a traditional function with comments and stuff its: NG←R ODAT S;N;P;PS
⍝ First construct the eight *P*ossible states:
⍝ Creates an array 7 6 5 4 3 2 1 0 (⌽0,⍳7) and converts to binary (2 2 2⊤)
⍝ 1 1 1 1 0 0 0 0
⍝ 1 1 0 0 1 1 0 0
⍝ 1 0 1 0 1 0 1 0
⍝ Then encloses along vertical axis (⊂[1])
P←⊂[1]2 2 2⊤⌽0,⍳7
⍝ Convert the *R*ule from wolfram number to binary ((8⍴2)⊤R)
⍝ 110 becomes 0 1 1 0 1 1 1 0
⍝ Then choose *P*ropogating *S*tates (/P)
PS←((8⍴2)⊤R)/P
⍝ Take *S*tate and triplicates ((3,⍴S)⍴S) and rotates (¯1 0 1⌽) to create a matrix
⍝ each colume is a cell and it's *N*eighbors
⍝ A state 1 0 1 1 0 0 becomes:
⍝ 0 1 1 0 0 1
⍝ 1 0 1 1 0 0
⍝ 0 1 1 0 1 0
⍝ Then encloses vertically again
N←⊂[1]¯1 0 1⌽(3,⍴S)⍴S
⍝ Now we have two arrays:
⍝ *N* which is the current generation of state/neighbor triplets
⍝ *PS* which is the triplets that produce a live cell in the *N*ext *G*eneration.
⍝ we create a new array by testing for membership (∊) of current generation
⍝ and *P*ropogating *S*tates
NG ← N∊PS
Some of that might be idiomatic python but the thing is I didn't write it as the multi-line function. I wrote it by modifying the one line function. Which I did because that's how APL's data-flowy syntax/semantics encourages you write things and doing that in one line sure as hell isn't idiomatic python.The determined programmer can write any language in any language. It's what we write when we aren't being determined fortran/smalltalk/whatever programmers that speaks to the difference in programming languages.
Still, while I met J author Roger Hui years ago in college, I've never run a line of J so I probably would be the one to create such a system.
It's certainly esoteric, but I wouldn't say it's unreadable. Given that APL is like a programming alphabet of sorts, it'd be interesting to see how quickly and tersely one could put together infrastructure with it. Too bad it has virtually no ecosystem.
It may not be a meaningful comparison, but the commenting on the side is more reminiscent of assembly than a high-level language (i.e. the code itself is not sufficient to tell you what it's attempting to do). Is this how well-commented most APL turns out, or is this due to being a teaching piece of sorts?
I don't think typing was slower then. And most programming time isn't spent typing.
As CS developed in the math world Iverson realized that the existing math was crap for describing computational processes. So he made his own and as it turned out it, with a little modification it was executable.