1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Function Recursion

Discussion in 'Support' started by joshjeppson, Jul 18, 2008.

  1. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    Is function recursion not supported?

    I seem to be able to define recursive functions, but upon execution I get a TCC: Variable loop error. Even multi-function recursion fails, ie, A > B > A > B ...

    The consummate example:
    Code:
     
    C:\>function factorial=`%@if[%1 gt 1,%@eval[%1 * %@factorial[%@dec[%1]]],1]`
    C:\>echo %@factorial[5]
    TCC: Variable loop
    

    - Josh
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,949
    Likes Received:
    30
    On Fri, 18 Jul 2008 17:06:19 -0500, joshjeppson <> wrote:


    The problem is that both IF consequents are evaluated regardless if the value of
    IF's antecedent. So, even when %1 EQ 1 (and thereafter), %@factorial[%@dec[%1]]
    is evaluated.
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,949
    Likes Received:
    30
    On Fri, 18 Jul 2008 17:06:19 -0500, joshjeppson <> wrote:


    Yes, to some level, but it's cumbersome. Here's an example (albeit very
    contrived) of a UDF which may call itself.

    v:\> function abv `%@execstr[iff %1 GE 0 then & echo %1 & else & echo
    %@abv[%@eval[%1 * -1]]]`

    v:\> echo %@abv[3]
    3

    v:\> echo %@abv[-3]
    3
     
  4. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,949
    Likes Received:
    30
    It can be prevented like this:


    v:\> function factorial=`%@if[%1 gt 1,%%@eval[%1 * %%@factorial[%@dec[%1]]],1]`

    v:\> echo %@factorial[5]
    120

    v:\> echo %@factorial[6]
    720
     
  5. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    The lack of short circuit evaluation in %@if, if and iff seems to trip me up every so often. Although, this time it is a little different in that the conditional wasn't the problem but the consequents.

    Thanks again Vince, as always!


    - Josh
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,856
    Likes Received:
    83
    You can't recurse more than 16 levels.

    Rex Conn
    JP Software
    ----- Original Message -----
    From: joshjeppson
    To: rconn@jpsoft.com
    Sent: Friday, July 18, 2008 6:06 PM
    Subject: [Support-t-332] Function Recursion


    Is function recursion not supported?

    I seem to be able to define recursive functions, but upon execution I get a TCC: Variable loop error. Even multi-function recursion fails, ie, A > B > A > B ...

    The consummate example:

    Code:

    C:\>function factorial=`%@if[%1 gt 1,%@eval[%1 * %@factorial[%@dec[%1]]],1]`
    C:\>echo %@factorial[5]
    TCC: Variable loop
    - Josh
     
  7. joshjeppson

    Joined:
    Jun 2, 2008
    Messages:
    42
    Likes Received:
    0
    In my original function I actually only needed a single level of recursion. The factorial function was contrived because of its ubiquity as an example of recursion and to demonstrate that in the simplest case, recursion, as I was using it, was not working.

    Between 32 levels of batch nesting, 22 levels of gosub nesting and 16 levels of function nesting, I think most of my batch file and command line recursion needs will be met.


    - Josh
     

Share This Page