I should also add an implementation of the "tco" at the very bottom of that
post:

tco=:(0 :0)(2 :0)
:
  context=. cocreate'' NB. context
  active__context=: 0
  accumulated__context=: ''
  v 2 :m context
)
:
  accumulated__n=: x,&<y
  if. 0=active__n do.
    active__n=: 1
    while. #accumulated__n do.
      pair=. accumulated__n
      accumulated__n=:''
      value__n=: u&>/pair
    end.
    active__n=. 0
    value__n
  end.
)

sum=: (4 :0) tco
  if. y>0 do.
    (x + 1) sum y-1
  else.
    x
  end.
)


   1 sum 10000
10001

Coding for tail call optimization gives you the opportunity to debug
infinite loops when you make mistakes.

Thanks,

-- 
Raul


On Tue, Aug 26, 2014 at 12:05 PM, Raul Miller <[email protected]> wrote:

> P.S. example use:
>
>     trampoline 3 peano 5
> 8
>
> Thanks,
>
> --
> Raul
>
>
>
> On Tue, Aug 26, 2014 at 12:04 PM, Raul Miller <[email protected]>
> wrote:
>
>> (I thought I sent this message, this morning, but I see that it has not
>> yet been sent. I have been having this happen a lot recently. I have a
>> hunch about what is happening, but for now I'll just note that I am having
>> this issue, and I apologize if it seems I am ignoring anyone.)
>>
>> --------------------------------------------------------
>>
>> Maybe like this:
>>
>> trampoline=:3 :0
>>    try.
>>       while. 1 do.
>>          y=. y`:6''
>>       end.
>>    catch.
>>       y
>>    end.
>> )
>>
>> peano=:4 :0
>>   if. 0=y do. x return. end.
>>   op=. <@,&<
>>   noun=. '0' op ]
>>   const=. '"' op (noun _),~ noun
>>   (const x+1)`peano`(const y-1)
>> )
>>
>> Note that if you instead define op, noun and const globally you should
>> rename 'noun' so it does not conflict with the existing definition for that
>> name.
>>
>> op is a utility gerund construction mechanism.
>> noun constructs a noun gerund from a value
>> const constructs a constant verb gerund from a value
>>
>> I hope that's clear enough?
>>
>> Thanks,
>>
>> --
>> Raul
>>
>>
>>
>> On Tue, Aug 26, 2014 at 6:10 AM, Joe Bogner <[email protected]> wrote:
>>
>>> On Mon, Aug 25, 2014 at 9:51 PM, Raul Miller <[email protected]>
>>> wrote:
>>> >
>>> http://www.integralist.co.uk/posts/understanding-recursion-in-functional-javascript-programming/#the-solution
>>> > looks like it is describing this:
>>> >
>>> > trampoline=:3 :0
>>> >    try.
>>> >       while. 1 do.
>>> >          y=. y`:6''
>>> >       end.
>>> >    catch.
>>> >       y
>>> >    end.
>>> > )
>>> >
>>>
>>> The linked url includes a sum function that is applied against the
>>> trampoline. How would that be done in J? I tried some things and could
>>> not figure out how to bind the x/y to the gerund, which is what I
>>> assumed I needed to do
>>>
>>> Don't run this code as it will crash J
>>>
>>> trampoline=:3 :0
>>>    try.
>>>       while. 1 do.
>>>          y=. y`:6''
>>>       end.
>>>    catch.
>>>       y
>>>    end.
>>> )
>>>
>>> NB. warning, crashes J
>>> recur=: 4 : 0
>>>   if. y > 0
>>>     do. trampoline recur&(y+1)`''
>>>     else. x
>>>   end.
>>> )
>>>
>>> sum=: 4 : 0
>>>     trampoline (x recur y)
>>> )
>>> ----------------------------------------------------------------------
>>> 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