On 6/5/2022 4:08 PM, Brian McGuinness wrote:
> I have been experimenting with simple lambda functions to see how they work. But I ran into behavior that I don't understand, and I haven't found good documentation to explain what is going on.
>
> This works as expected:
>
> ({⍵,+/¯2↑⍵}⍣10) 0 1
> 0 1 1 2 3 5 8 13 21 34 55 89
>
> But this doesn't work:
>
> ∇
> [0] A←FIBONACCI N
> [1] A←({⍵,+/¯2↑⍵}⍣N-2) 0 1
> ∇
>
> FIBONACCI 12
> ¯2 ¯2 ¯4 ¯6 ¯10 ¯16 ¯26 ¯42 ¯68 ¯110 ¯178 ¯288 ¯466 0 1
>
> If I tried to do this using only normal defined functions I would have to use two separate functions, so it seems to me that using a lambda function should be a cleaner way to perform the operation. But I don't see how to make this work.
>
> --- Brian McGuinness
If I understand correctly, it doesn't look to me like doing the same
thing with a defined function is doing anything differently. In
immediate execution mode:
({⍵,+/¯2↑⍵}⍣12-2) 0 1
¯2 ¯2 ¯4 ¯6 ¯10 ¯16 ¯26 ¯42 ¯68 ¯110 ¯178 ¯288 ¯466 0 1
But:
({⍵,+/¯2↑⍵}⍣(12-2)) 0 1
0 1 1 2 3 5 8 13 21 34 55 89
I'm not familiar with the power function, but perhaps it's a precedence
issue, which the parentheses solves. So change your defined function to:
∇
[0] A←FIBONACCI N
[1] A←({⍵,+/¯2↑⍵}⍣(N-2)) 0 1
∇
/ Rav
|
|