Notation as a Tool of Thought (1979) [pdf]
eecg.toronto.edu
eecg.toronto.edu
I wish more people were familiar with Iverson's work (APL, J) and its descendents (mostly K). Everything else is so needlessly complex in comparison.
There is a steep learning curve, true - but it is well worth it.
∇a←norm;uu;vv;s
⍝ Normal distribution around 0 (using the Box-Muller transform in polar form
⍝ Local variables go in the header after semicolons
CHOOSE:
uu←u
vv←u
s←(uu * 2)+vv * 2
→(s≥1)/CHOOSE
→(s=0)/CHOOSE
m←(((¯2×⍟s)÷s) * 0.5)
a←uu×m
∇
Edit: forgot to formatI have to confess that I don't find this sort of example very compelling; Box-Muller isn't much more than a one-liner in any language. I mean, here it is in C, not noted for its richly expressive terseness:
double norm(void) {
while (1) {
double u=rand01(), v=rand01();
double s = u*u+v*v;
if (0 < s && s <= 1) return u * sqrt(-2*log(s)/s);
}
}
(I assumed that we already had a rand01() function equivalent to your "u"; I suggest that in any nontrivial program the gain in brevity from the single-character name is outweighed by being unable to call a local variable "u" and, if you have a bunch of these, the cognitive load of remembering what all the single-character names mean.)I bet the C implementation is distinctly easier to make sense of for most mathematicians and most programmers, and it's not even substantially longer. (APL: 94 non-blank characters. C: 111 non-blank characters. I remark that counting non-blank characters is about as favourable a metric for APL as one can reasonably ask for.) One can code-golf it further: combine the declaration of u,v with that of s (saves 5 non-blank characters --> 106); switch to your "u" instead of my "rand01" and rename u,v to (say) x,y (saves 10 characters --> 96); convert to do/while and use commas instead of {;;;} for body (saves 4 characters -> 92). That looks like this, and I think is still pretty easy to read:
double norm(void) {
double x,y,s;
do x=u(), y=u(), s=x*x+y*y; while (s==0 || s>1);
return x*sqrt(-2*log(s)/s);
}
(Note: character-counting is by hand and it's entirely possible I'm out by a couple of chars either way.)[EDITED to fix formatting screwage; aargh, I hate HN's treatment of asterisks]
∇a←n hist x
⍝ Histogram data from x in n buckets
s←x[⍋ x]
mx←¯1 ↑ s
mn←1 ↑ s
bk←mn+(⍳n)×(mx-mn)÷n
a←+/(~(↑bk)<s)
s←((↑bk)<s)/s
bk←1↓bk
LOOP:→(0=⍴bk)/END
a←a,+/(~(↑bk)<s)
s←((↑bk)<s)/s
bk←1↓bk
→LOOP
END:
∇
Not brilliant, and even at the time of writing (5 or 6 months ago) I had the feeling it may be possible to write it loopless or close enough. Variables in this one are still "global" since (even if it works) I was still checking it for possible bugs. 💉foo() // dependency injection
🌏eggs // geospatial value
I'm not advocating standardization [ala APL at the language level]. I am thinking about making sections of code better diagramatically.http://www.eecg.toronto.edu/~jzhu/csc326/readings/iverson.pd...
Of all the organizations that go out of their way to make the web suck, ACM is one of the few for which the choice of behavior is a tragedy..