David, the trick is to keep in mind: "$: denotes the longest verb that contains it." This is important because J does not provide a primitive for the scope of $: (although one can write a wicked tacit adverb to set the scope directly to keep everything tacit).
This is what I think is happening, f =: complete`f_odd`[email protected] 5 f 4 |stack error: f_even | 5 f 4 Why? Because 5 agenda 4 2 and 5 f_even 4 |stack error: f_even | 5 f_even 4 Why? Because "the longest verb that contains it." is f_even $: (, (_1 2 p. {:)) thus, 2 ($: (, (_1 2 p. {:))) 4 |stack error | 2 ($:(,(_1 2 p.{:)))4 A similar reasoning applies to, 5 (complete`f_odd`[email protected] f.) 4 |stack error | x $:(,(_1 2 p.{:))y Why does the interpreter produce, complete`f_odd`[email protected] f. ]`(3 : '$: (, (1 2 p. {:)) y' :(4 : 'x $: (, (1 2 p. {:)) y'))`(3 : '$: (, (_1 2 p. {:)) y' :(4 : 'x $: (, (_1 2 p. {:)) y'))@.((> * [: >: 2&|) #) ? A simple example can illustrate the rationale. Assume that given the recursive factorial verb, fac=. 1:`(* $:@:<:)@.* one wants to produce a verb which calculates the factorial plus one. Naturally, facplusone=. 1 + fac facplusone 5 121 facplusone f. 5 121 facplusone f. 1 + 3 : '1:`(* $:@:<:)@.* y' :(4 : 'x 1:`(* $:@:<:)@.* y') (i.e., the interpreter is using an explicit envelope to force a natural scope) whereas, facplusone=. 1 + fac f. facplusone 5 446 gives the wrong answer (in this case). I hope it helps P.D. Yours was a neat answer to the original question. On Tue, Nov 27, 2018 at 12:41 PM David Lambert <[email protected]> wrote: > Here's a recursive solution. > > ( Following the solution you'll see that I am still somewhat mystified > about $: and f. . ) > > Write an implicit recursive verb for this sequence formula: > > a1 , (a2=.1-~2*a1) , (a3=.1+~2*a2) , (a4=.1-~2*a3) , (a5=.1+~2*a4) ... > (an=.1(+-)~2*an-1 > > The result will be a vector n items long. Note the alternating sign in each > term > > > x is the number of terms > y is the current sequence > > Example: > > 5 f 4 > 4 7 15 29 59 > _____________________ > > A solution, I still love hooks > _____________________ > > > agenda =: (> * [: >: 2&|) # > > assert 2 -: 4 agenda 4 4 43 NB. odd length use 3rd verb > assert 1 -: 4 agenda 4 4 NB. even use 2nd verb > assert 0 -: 4 agenda 4 4 8 8 8 NB. complete use 1st verb > assert 0 -: 4 agenda 4 4 8 8 NB. complete > > complete =: ] > f_odd =: $: (, 1 2 p. {:) > f_even =: $: (, _1 2 p. {:) > > > f =: (complete f.)`(f_odd f.)`(f_even f.)@.agenda > assert 4 7 15 29 59 -: 5 f 4 > > > ------------------------ > > > NB. stack error because $: limits scope to the adverb > f =: complete`f_odd`[email protected] > > NB. I'm rather clueless about this fixed expansion, which also produces > stack error > complete`f_odd`[email protected] f. > ]`(3 : '$: (, (_1 2 p. {:)) y' :(4 : 'x $: (, (_1 2 p. {:)) y'))`(3 : '$: > (, (1 2 p. {:)) y' :(4 : 'x $: (, (1 2 p. {:)) y'))@.((> * [: >: 2&|) #) > > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
