Re: Double evaluation in recursive function
- To: mathgroup at smc.vnet.net
- Subject: [mg19565] Re: Double evaluation in recursive function
- From: David Bailey <db at salford-software.com>
- Date: Wed, 1 Sep 1999 23:07:01 -0400
- Organization: Salford Software
- References: <7q9h2j$o5d@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 28 Aug 1999 16:33:23 -0400, "Benoit Debaque"
<bdebaque at cybercable.fr> wrote:
>Hi all !
>
>Does somebody know how to use 2 calls at a time in a recursive function...?
>
>for example I want to evaluate the following
>
>myf[x_] : = If[ x > 5, i = x ; j = x ; i--; j--; myf[i]; myf[j] ]
>
>I get the answer from
>myf[7]
>
>i=6
>i=5
>j=5
>j=5
>
>apparently Mathematica doesn't like much the second term myf[j], because it
>start from 5 to 5 ! and not from 6 to 5 like variable i............how can I
>manage such a problem ?
>
It is not entirely clear what this code is meant to do, since your
'If' construction does not specify the result for x <= 5, however, one
obvious problem is that i and j are global variables so the value of j
will have been destroyed before the second call is executed. So write
myf[x_]:=Module[{i,j},
If[x>5,
i=x;
j=x;
i--;
j--;
myf[i];
myf[j],
<something should go here or function will return Null>
]
]
You could probably eliminate i and j by simply calling myf[x-1] - but
it is a bit difficult to figure out what this code was designed to do.
David Bailey
Salford Software
- Follow-Ups:
- Re: Re: Double evaluation in recursive function
- From: "Wolf, Hartmut" <hwolf@debis.com>
- Re: Re: Double evaluation in recursive function