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&|) #)



> Message: 1
> Date: Tue, 27 Nov 2018 01:09:30 -0600
> From: Skip Cave <[email protected]>
> To: "[email protected]" <[email protected]>
> Subject: [Jprogramming] Recursive verbs
> Message-ID:
>         <
> caj8lg_ejjn3lbmvhppawbdox0s65vffvja4shpj_nmv-ehp...@mail.gmail.com>
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to