Here's a thing about recursion: integers have a recursive definition
(google: peano axioms).  Basically, if you have zero and increment you
can get all the positive integers (and you can work with them that
way, even if it's a bit slow).

So $: with >: and an equality test could be used to implement
primitive addition, and multiplication, and if you add a sign test you
could do subtraction, ...

But what this also suggests is that a lot of "recursive algorithms"
are really just ways of doing simple arithmetic. (Or: their values
often map to numeric sequences with relatively simple expressions.)

That said, the actual details of $: (anonymous recursion) are a bit
subtle, and stack errors and/or long waits can be annoying to deal
with - so maybe it's good to abandon $: when you're not working with
simple, well understood examples...

Anyways... philosophizing aside, here's a rephrasing of Cliff Reiter's approach:

   4 p.~ (2&^ (%&_3x@-,.[) _1&^) >: i. 10
7 15 29 59 117 235 469 939 1877 3755

Or, if you prefer:

   4 p.~(%&_3x@:- ,. [)/2  _1^/ >: i. 10
7 15 29 59 117 235 469 939 1877 3755

(But maybe, in retrospect, it's kind of confusing that the left /
there is the insert operation and the right / there is the outer
product operation... and that the combination is sort of analogous to
the use of parentheses...)

Thanks,

-- 
Raul
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&|) #)
>
>
>
> > 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to