How to write N N N .... N using operators (or other things)?
Well.. I have started to learn APL since yesterday. I'm watching youtube videos teaching about various symbols from basic, and I'm using NARS2000.
What I want is to print the Fibonacci sequence. I know there's several codes, but since I haven't studied advanced things, I started to write my own code.
First I made this array:
The idea is simple : the element at (1,1) in Nⁿ is (n+1)th fibonacci sequence.
What I did was:
and
开发者_开发知识库Well, it works. However, if I want 16th term, then I should do
What I need is to write arbitary amount of Ns. Of course I know about {rho}. However,
(bottom was cut)
And I noticed that (i 2 2){rho}N and i Ns are different.
What operator I should use to do same thing as N N N...N does?
You were almost there. ⍴
("reshape") is the right operator to use; however, you want it to treat your matrix N not as a matrix, but as a single, scalar element. For this purpose, you wrap it using the "enclose" operator ⊂
:
4⍴⊂N
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
If we wrap this up, we arrive at (e.g.) the following expression:
↑¨+.×\16⍴⊂2 2⍴1 1 1 0
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
(Allow me one remark, though: by definition, the Fibonacci sequence starts with 0
and 1
.)
If you want to start with 0 and 1, just use 0 1 1 1 instead of 1 1 1 0
↑¨+.×\16⍴⊂2 2⍴0 1 1 1
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
精彩评论