Though, I should add, induction and recursion are very similar.

There's a significant difference, though. Induction is much more like
what recursive people call "tail recursion". In other words, once a
function is done executing and you go on to the next function
evaluation, you never come back to the first. This has some obvious
advantages, especially when it's what you wanted in the first place.

Thanks,

-- 
Raul


On Tue, Sep 27, 2016 at 3:55 AM, Raul Miller <[email protected]> wrote:
> ^: is induction, not recursion. You should use either $: or a named
> routine if you want recursion.
>
> Also, ^: is defined such that the left argument is constant and the
> right argument is the result of the previous function evaluation. (Or,
> initially, the initial right argument.)
>
> Still, you can sort of do what you are asking for by including the
> left argument with the right argument. Though, of course, if you do
> that, you'll need to pick out just the right argument from your final
> result. Like this, for example:
>
>    {: (>:@{., +/)^:(3)2 2
>
> That gives you:
>
>    (>:@{., +/) 2 2
> 3 4
>    (>:@{., +/) 3 4
> 4 7
>    (>:@{., +/) 4 7
> 5 11
>    {: 5 11
> 11
>
> I hope that makes sense,
>
> --
> Raul
>
>
> On Tue, Sep 27, 2016 at 3:35 AM, Skip Cave <[email protected]> wrote:
>> NB. I am attempting to learn recursion using ^:
>>
>>
>> NB. A simple recursion:
>>
>>
>>    2+^:(3) 2
>>
>> 8
>>
>>
>> NB. This is equivalent to
>>
>>
>> 2+2+2+2
>>
>> 8
>>
>>
>> NB. What if I want to change the left argument on each recursion?
>>
>> NB. Instead of 2 each time, I want to add 2, then 3, then 4.
>>
>> NB. I know there are much easier ways to do this, but I want to see
>>
>> NB. if I can avoid do. while. loops if the left argument changes on each
>> iteration
>>
>>
>> NB. I tried
>>
>>
>> (2 3 4)+^:(3) 2
>>
>> 8 11 14
>>
>>
>> NB. Nope. That didn't work.
>>
>> NB. What I want to achieve is:
>>
>>
>> 2+3+4+2
>>
>> 11
>>
>> NB. but do it recursively using ^:
>>
>> Skip
>>
>> Skip Cave
>> Cave Consulting LLC
>> ----------------------------------------------------------------------
>> 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