5 ms·
APL in R
- sevensor 10y agoAPL is a fascinating language -- it keeps popping up in odd corners like this, and there are echoes of APL in the design of a lot of popular libraries. Numpy is a good example: it's almost another language embedded in Python.
- ghosthamlet 10y agoagree with you, apl is great for ML, see this 6 lines Neural Network Example by apl with no deps: https://github.com/ghosthamlet/ann.apl https://github.com/ghosthamlet/ann.apl all code here: l0 ← 4 3 ⍴ 0 0 1 0 1 1 1 0 1 1 1 1 y ← 4 1 ⍴ 0 0 1 1 syn0 ← 3 1 ⍴ 0.5069797399 0.5703818579 0.9265880619 i ← 0 e ← 2.718281828459045 :While i < 10000 ⋄ i ← i + 1 ⋄ d ← l0 +.× syn0 ⋄ l1 ← 1 ÷ 1 + e * 0 - d ⋄ l1_err ← y - l1 ⋄ l1_delta ← l1_err × l1 × 1 - l1 ⋄ syn0 ← syn0 + (⍉ l0) +.× l1_delta ⋄ :EndWhile l1
- ghosthamlet 10y agoby the way, it is a ports from 11 lines python version here: http://iamtrask.github.io/2015/07/12/basic-python-network/ http://iamtrask.github.io/2015/07/12/basic-python-network/, isn't it more readable concise and beautiful than python version? if our all ML codes use apl, it will be a cool world.
- cwyers 10y ago> isn't it more readable concise and beautiful than python version? No.
- minimaxir 10y agoTo add to this, the which-loop-in-one-line-of-code is not self-documenting, which is important for data/ML in particular when others need to verify your code.
- cwyers 10y agoAnd it's not any shorter in terms of statements, they're just using the diamond operator to put all the statements in the loop on one line.
- klibertp 10y agoIt could simply be a badly written bit of APL. Although I don't know APL, I have some experience with J and I think the `:while` construct used here suggest more or less literal translation of the Python code.
- Sean1708 10y agoIs it possible to put that while loop on multiple lines? It looks like there are 6 different assignments in there, and trying to pick out what is happening is very hard when it's all on one line like that.
- Avshalom 10y agoYou can break it up at each diamond but then you'd need to wrap it in a function which would be harder to cut and paste into a repl
- rebootthesystem 10y agoHmmmm. This is a bad example of APL. Source: I used APL professionally for over ten years. For one thing, real APL does not have constructs like "while". That's the whole point of the language, to remove the need for the programmer to have to think this way. Also, this isn't, strictly speaking, six lines of code. The diamond is a statement separator. This is more like 15 or 20 lines (didn't count). Not that this is an important metric at all.
- sevensor 10y agoI'd be interested to know: for what kind of application did you use APL?
- rebootthesystem 10y agoQuite a range, actually. From business applications to robotics and DNA sequencing.
- agumonkey 10y agognu apl + emacs dyalog mode is a cute way to test
- sevensor 10y agoWhat font do you use for APL symbols?
- i_don_t_know 10y agoI've used this on the Mac: http://www.dyalog.com/apl-font-keyboard.htm http://www.dyalog.com/apl-font-keyboard.htm Font and keyboard settings work independently of Dyalog APL. (Thank you, btw, for that!) For example, I've used them with GNU APL.
- sndean 10y agoJust for the possible person using linux: the easiest way I've found to get that Dyalog font working is to install as it instructs you to, then run the bash script provided in this stackoverflow answer (http://stackoverflow.com/a/27955014/3430492 http://stackoverflow.com/a/27955014/3430492).
- agumonkey 10y agoemacs dyalog comes with a visual (textual) apl keyboard with the official symbols. There's also alternative bindings for more direct input.
- jghn 10y agoThe APL keyboard is the reason R's assignment operator, the classic one at least, is <-
- radford-neal 10y agoActually, I think that while there might have been some inspiration from APL, the basic reason is that ASR33 teletype machines have a backarrow character, whose ASCII code is the one more commonly associated with underscore. That explains why for many years the underscore character was accepted as an assignment operator in R and S. The <- glyph is a synonym useful if for some bizarre reason you are not programming with an ASR33 teletype machine.
- jghn 10y agoThat's an interesting point and likely correct. The story I gave here was told to me way back in the day by RG himself, although the real reason R uses `<-` is "because that's what S does" and it's possible that the reason he was given wasn't 100% correct itself.
- riffraff 10y agoFWIW, the left arrow is also what smalltalk used for assignment, from wikipedia[0] In the original Parc Place image, the glyph of the underscore character (_) appeared as a left-facing arrow (like in the 1963 version of the ASCII code). Smalltalk originally accepted this left-arrow as the only assignment operator. [0] https://en.wikipedia.org/wiki/Smalltalk#Assignment https://en.wikipedia.org/wiki/Smalltalk#Assignment
- PhantomGremlin 10y agothe basic reason is that ASR33 teletype machines have a backarrow character, whose ASCII code is the one more commonly associated with underscore. When I read this I said "huh?" because I used ASR33's quite a bit in my youth and I didn't remember that. But, sure enough, the 1963 version of ASCII had the backarrow. It was replaced by underscore in the 1965 version. The backarrow would have been current when Iverson first developed APL. https://en.wikipedia.org/wiki/ASCII#Printable_characters https://en.wikipedia.org/wiki/ASCII#Printable_characters
- jtcond13 10y agoCan someone provide a motivating example here? I'm not opposed to reading this, but don't understand why I should.
- sevensor 10y agoI wouldn't recommend reading the whole thing -- it's a manual. Read the introduction and maybe the "core" chapter. The intro is really short. I don't have an example, but here are some motivations: Read it because you're generally interested in how people use programming languages to solve their problems. Read it because you use R and would like to have a really nice way of talking about array operations. Read it because you use APL and you want access to all of the neat stats functions R provides.