Normal J style is control on the left, data on the right, so I have redefined your spec to take the number of doublings as x and the number to be doubled as y.

   d =: ($: (, +:@{:))^:(> #)
   10 d 7
7 14 28 56 112 224 448 896 1792 3584

Dissect can be helpful in tracing through recursive execution.

In a sentence like this ^: should be read as 'if'.

Henry Rich

On 6/12/2018 2:07 AM, Skip Cave wrote:
I want to try to understand recursive functions in J. To start this
learning process, I want to investigate J verbs that will generate a
doubling sequence. For example, the verb 'd' where x = the starting
integer, and y = the number of iterations:

    2 d 6
2 4 8 16 32 64

    5 d 4
5 10 20 40

    7 d 10
7 14 28 56 112 224 448 896 1792 3584

Define three different simple recursive verbs in J, to implement this
recursive doubling function:
1. d1 - Just calling the function recursively
2. d2 - Using the Power operator ^:
3. d3 - Using Spread S: and Memo M.

Skip

Skip Cave
Cave Consulting LLC
----------------------------------------------------------------------
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

Reply via email to