How does this APL golden ratio code work?

Viewed 306

This is code for the golden ratio in Dyalog APL:

1+∘÷⍣=1

I don't get it. "1 plus compose rec repeat equals 1" does not make any sense.

3 Answers

The operands of ⍣ are +∘÷ (the left) and = (the right). Its arguments are both 1.

⍣ with a function right operand is "until", which repeats the left operand until the right operand, called with the last two results, gives 1. So, here ⍣= is "repeat until equal" - find the fixed point.

⍣ calls its left operand with the same left argument as its left argument (so always 1), but cycles the right one with the new results.
So, +∘÷ is called dyadically, which is the same as the dfn {⍺ + ÷⍵}. ⍺ here is always the same 1 because that's the left argument of ⍣.

Altogether, this code does 1 + ÷last until the last result and this one are equal. Try it online!

Dyalog APLs ]box on -trains=tree:

  ⍣ 
 ┌┴┐
 ∘ =
┌┴┐ 
+ ÷ 

+∘÷

The reciprocal is applied monadically to the right operand, and the result is added to the left operand. 10+∘÷4 → 10.25 and 1+∘÷1 → 2.

⍣number

When we repeat a dyadic function, the right operand is "updated" but the left operand stays the same. 1+∘÷⍣2⊢1 → 1+∘÷2 → 1.5

⍣function

Repeat again and again, as long as (current result) f (previous result) is false. This way, ⍣= repeats until convergence, as if "∞ times".

Now I get it, thanks to you! Lemme summarize! x+∘÷y is a composed function. First, you calculate 1/y, then, it is composed with addition, and x is added. Now, here is where our sour-candy-loving (he puckers all the time) repeat guy comes in! A test: 1 (+⍣10) 1. He starts at 1, and adds 1, and does this again to the result, until he does it ten times. If the 10 becomes an =, he will stop when the previous result is equal to the current result! x (f⍣u) y, in this case, would have x, f, u and y, be 1, +∘÷, = and 1. This "reciprocal + 1" method is a classic method, I think.

Related