Best construct for control structure
- To: mathgroup at smc.vnet.net
- Subject: [mg60063] Best construct for control structure
- From: Usenet poster <look at end.of.post>
- Date: Wed, 31 Aug 2005 00:24:56 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Dear Mathematica gurus,
I'm trying to learn the right way to code a homebrew control structure. As a
example, I've tried coding the following (of course, a For could also do
it):
In[77]:=
myRepeat[expr_,cond_]:=(ReleaseHold[expr];
While[ReleaseHold[cond],ReleaseHold[expr]])
Attributes[myRepeat]={HoldAll};
In[79]:=
x=0;
Trace[myRepeat[x++,x<2]]
x
Out[80]=
{myRepeat[x++,x<2],ReleaseHold[x++];
While[ReleaseHold[x<2],ReleaseHold[x++]],{{x++,{x,0},{x=1,1},0},
ReleaseHold[0],
0},{While[ReleaseHold[x<2],ReleaseHold[x++]],{{{x,1},1<2,True},
ReleaseHold[True],True},{{x++,{x,1},{x=2,2},1},ReleaseHold[1],
1},{{{x,2},2<2,False},ReleaseHold[False],False},Null},Null}
Out[81]=
2
While (pun intended) the result is indeed correct as the trace shows, I
wonder if there isn't a much better way to have cond and expr somehow hold
without having to call ReleaseHold that much times. I may be completely
misguided, but I see this as a inelegant inefficency.
Or, are Mathematica control structures (For, While, ...) totally
different from other
functions, inherently, that we just can't think about building ours
(efficiently and robustly, I mean).
--
You usenet
can at
reach q-e-d
me dot
here org