Henry Rich wrote:
> It could just be that the poster wanted to see a recursive version ...

Recursive functions have some inherent complexities - to properly
understand them, you need to think about the base case and the
recursive case, but you also have to see how that connects to the idea
as a whole.

So let's contrive an example base case and an example recursive case
and see if we can get anywhere with that:

We can start with the beginning of our sequence (4), but we also are
going to need to know when to stop (length 9 here).

(Or we could set up an infinite loop and some extra infrastructure so
that an independent subprogram can cut us off when it has had enough -
but infinite loops tend to bog things down and that extra
infrastructure would be yet another complication. People do that kind
of thing a lot nowadays, because computers are so cheap, but if you
want that you really want a generator rather than a recursion.
https://en.wikipedia.org/wiki/Generator_(computer_programming) )

In the middle of processing our example, we're going to want to have
the prefix of the sequence which we have generated (for example: 4 9
15) and the next value which we will be adding to the last number our
sequence (for example: 7) and the length of the sequence (that will
not have changed - it would still be 9).

That's three different arguments, one of which does not change (the
desired sequence length), for the recursive case.

So this seems like it is almost begging for an adverb implementation -
an adverb gives us three arguments in the "dyadic" case, and two for
the "monadic" case. ["dyadic" and "monadic" referring to the derived
verb which the adverb generates - which is a subtlety that we tend to
glide past most of the time.] The sequence length seems a natural fit
for the adverbial 'm' argument.

Thinking about this, though - the "monadic" case would need to have 4
as the sequence prefix, and 5 as the next number to add to the last
number in our sequence (and 9 for the length). We can easily generate
the value 5 by adding 1 to the 4 that we start out with... so let's
rearrange our recursive case example so that we also add 1 to it (in
other words, with 4 9 15 the other verb argument would be 6).

This means we can use ~ in our definition. When we use verb~ the
monadic case passes the same value as both the left and right
arguments to verb. It also swaps the args for the dyadic case.

That gives us:

sumofincseq=:1 :0~
:
  n=. x+1
  s=. y,n+{:y
  if. m>#s do. s m sumofincseq n end.
)

Example use:

   9 sumofincseq 4
4 9 15 22 30 39 49 60 72

"Left argument" tells us how long the generated sequence will be.
"Right argument" is where the sequence starts. (Perhaps that should
have been hard coded - that would simplify things - but we don't know
enough from the problem statement to know if it should or should not
have been hardcoded.)

I hope this makes sense?

Nowhere near as simple as +/\4+i.9 but that's recursion for you.

Thanks,

-- 
Raul


On Fri, Sep 29, 2017 at 11:46 PM, Henry Rich <[email protected]> wrote:
> It could just be that the poster wanted to see a recursive version of this
> problem (and one using ^:) so that he could learn how to apply those methods
> to other problems that really needed them.
>
> Henry Rich
>
>
> On 9/29/2017 11:01 PM, Don Kelly wrote:
>>
>> So it is a game- fair enough- it shows that in J there are more ways than
>> one to skin a cat. BUT does it really bring out the reason for a language
>> like J or APL? Does it bring out the power of J to make so many things
>> simpler and more elegant for the user? You have used APL and so have I -and
>> the benefit has been the ability to express a solution to a problem-in APL
>> it has bee explicit but in J there is a choice and ,for some things,
>> explicit is clearer and more understandable -where for other things tacit J
>> has advantages. I have an explicit script for finding electric or magnetic
>> fields in the region of a powerline. This can involve some internal calls on
>> a tacit operation. The balance is dependent on the problem  and the
>> preferences of the user.
>>
>> Don
>>
>>
>> On 2017-09-28 4:41 PM, Erling Hellenäs wrote:
>>>
>>> It's not better. Skip asked for a recursive version too so  we gave him
>>> one. Very complicated way to write a simple thing. Lol. /Erling
>>>
>>> On 2017-09-29 01:24, Don Kelly wrote:
>>>>
>>>> why is this better than the easy to read version that Roger Hui sent?
>>>>
>>>> quote:
>>>>
>>>>    +/\4+i.10
>>>> 4 9 15 22 30 39 49 60 72 85
>>>>
>>>> unquote: This is also the way APL does it Sure it is fun to try
>>>> different ways such as below, just as it is fun to make a 1 liner to
>>>> generate a series for sin x. It is also nice to come back to a piece of 
>>>> code
>>>> written a year earlier and be able to recognize quickly what it did. In the
>>>> end- the simpler expression uses the power of J to do it more neatly and
>>>> faster Don Kelly
>>>>
>>>> On 2017-09-28 1:02 PM, Xiao-Yong Jin wrote:
>>>>>
>>>>>
>>>>>> On Sep 28, 2017, at 2:45 PM, Erling Hellenäs
>>>>>> <[email protected]> wrote:
>>>>>>
>>>>>>     4 [`(] (] , [ + [: {: ]) [ $: [: <: ])@.(<) 8
>>>>>> 4 9 15 22 30
>>>>>
>>>>> Super!
>>>>>
>>>>> 4 [`(] (] , (+{:)) ($:<:) )@.< 8
>>>>>
>>>>>> /Erling
>>>>>>
>>>>>> On 2017-09-28 20:33, Skip Cave wrote:
>>>>>>>
>>>>>>> What is a good way to generate this sequence in J?
>>>>>>>
>>>>>>>   4,(4+5),(4+5+6),(4+5+6+7),(4+5+6+7+8)+.....
>>>>>>>
>>>>>>> Show one way using i. and insert.
>>>>>>>
>>>>>>> Show another way using recursion.
>>>>>>>
>>>>>>> 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
>>>>>
>>>>> ----------------------------------------------------------------------
>>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>>
>>>>
>>>> ----------------------------------------------------------------------
>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>
>>>
>>>
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>
>
>
> ---
> This email has been checked for viruses by AVG.
> http://www.avg.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