Return index of for loop



On Fri, Nov 28, 2008 at 8:42 AM, Adam Majewski <adammaj1 at o2.pl> wrote:

> > GivePeriodOfAngle(n0,d):=
> >   block([ni:n0],
> >    for i thru 200 do if (ni:doubling_map(ni,d))=n0 then return(i));
>
> Yes it really works, but what ( and why ) makes that your program works
> and my do not ?
>
> /* my gives i */
> G1(n0,d):=block(
> [ni:doubling_map(n0,d)],
> for i:1 step 1 thru 200 unless (n0=ni) do ni:doubling_map(ni,d),
> return(i)
> );
>

"for" declares the loop variable as local:

      ( for i thru 5 do 0, i ) =>  i     (not 5!)


> > This returns 'done' if it doesn't terminate successfully.
>
> If the function will be used inside another function it can be a problem
>  because done is not a numerical value ( if I'm not wrong ).


Try:

GivePeriodOfAngle(n0,d):=
   catch(
      block([ni:n0],
          for i thru 200 do if (ni:doubling_map(ni,d))=n0 then throw(i),
          0 ) )

But it can be changed to
>
> G(n0,d):=
>   block([ni:n0],
>    for i thru 200 do if (ni:doubling_map(ni,d))=n0 then
> if (ni=n0)then return(i) else
>   return(0));
>

This does not do the same thing at all!

                 -s