Re: Nesting / Repetitive Functions
- To: mathgroup at smc.vnet.net
- Subject: [mg36099] Re: Nesting / Repetitive Functions
- From: hartmut.wolf at t-systems.com (Hartmut Wolf)
- Date: Wed, 21 Aug 2002 05:52:08 -0400 (EDT)
- References: <ajfitb$iq9$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Ashraf El Ansary" <Elansary at btopenworld.com> wrote in message news:<ajfitb$iq9$1 at smc.vnet.net>... > Dear All, > I was wondering if there was any method besides the Nest[] function to > repeat function more than once during the same Input/Output cycle .. > Actually what I'm trying to reach is as follows: > Defining X= ToString[HoldForm[Cat"Basic Algebra" None TM"Expand" > TM"Factor" ]]; > Defining U=Part[Flatten[StringPosition[X," > "]],Range[1,Length[Flatten[StringPosition[X," "]]],2]] > That's giving the location of Blank spaces within X. > > Then > One=StringDrop[X,{U[[1]],U[[1]]}]; > Two[i_]:=StringDrop[%,{U[[i+1]]-i,U[[i+1]]-i}]; > > > I'm Trying to apply > In[32]:= > One > > Out[32]= > CatBasic Algebra None TM Expand TM Factor > > In[33]:= > Two[1] > > Out[33]= > CatBasicAlgebra None TM Expand TM Factor > > In[34]:= > Two[2] > > Out[34]= > CatBasicAlgebraNone TM Expand TM Factor > > In[35]:= > Two[3] > > Out[35]= > CatBasicAlgebraNoneTM Expand TM Factor > > In[36]:= > Two[4] > > Out[36]= > CatBasicAlgebraNoneTMExpand TM Factor > ------------------ > It is not just that I'd like to remove the blanks but rather interested in > the idea.. I tried to build a loop but % didn't work since % doesn't work > unless the result of the whole loop is out, I know I can use Nest[] but > don't know how to do it... Any suggestions.... Thanks. > > > I know I might have a silly answer.. I just had Mathematica for less than a > couple of weeks... Thanks You ALL Ashraf El Ansary, of course you'll find the answers to all your questions in reading the Book, esp. part 2, and this is indispensible. As you have found out yourself, the problem with your function Two is its reference to Out[], and in the course of repetitive execution of Two, Out will not be set. The answer to this Problem is simple: instead use a reference to a symbol, a "variable", that can be set, e.g. In[26]:= Two[i_] := StringDrop[aString, {U[[i + 1]] - i, U[[i + 1]] - i}] and then execute In[27]:= Block[{aString = One}, Do[aString = Two[i], {i, 1, Length[U] - 1}]; aString] Out[27]= "CatBasicAlgebraNoneTMExpandTMFactor" Block localizes the variable aString that is referred to in your function Two. You don't need One of course, and, instead of looping, you may Scan or Map a list of indices: In[28]:= Block[{aString = X}, Scan[(aString = Two[#]) &, Range[0, Length[U] - 1]]; aString] Out[28]= "CatBasicAlgebraNoneTMExpandTMFactor" Instead of Mapping over the indices, you may Map directly over U; but now, as you had set things up, also the index is involved in the calculation, such you have to use MapIndexed (which gives the index as second argument): In[41]:= Block[{aString = X}, MapIndexed[(aString = StringDrop[aString, {#1 - First[#2] + 1, #1 - First[#2] + 1}]) &, U]; aString] Out[41]= "CatBasicAlgebraNoneTMExpandTMFactor" We might be incontent with this complication. Why was it necessary? It was because the blanks got shifted away to the left from the positions found first. Now if we work back form the end... In[43]:= Module[{aString = X}, Scan[(aString = StringDrop[aString, {#1, #1}]) &, Reverse[U]]; aString] Out[43]= "CatBasicAlgebraNoneTMExpandTMFactor" We replaced Block by Module, since Block only was necessary because of the reference of Two to the symbol "aString". Now everything is explicit. Also, as we see, we might use a better U (avoiding all complications in calculating that): In[45]:= Module[{U = StringPosition[X," "], aString = X}, Scan[(aString = StringDrop[aString, #])&, Reverse[U]]; aString] Out[45]= "CatBasicAlgebraNoneTMExpandTMFactor" We might be content with this, but better learning is to play. How would we code in a procedural way? (It's better to have a working solution first, and then try to improve it.) In[30]:= Module[{aString = X, pos}, While[(pos = StringPosition[aString, " ", 1]) =!= {}, aString = StringDrop[aString, First[pos]]]; aString] Out[30]= "CatBasicAlgebraNoneTMExpandTMFactor" We just repeatedly remove the first blank, until no more is found. Now we have a structure like x = f[x], so we might use Nest: In[31]:= Module[{pos}, NestWhile[StringDrop[#, First[pos]] &, X, ((pos = StringPosition[#, " ", 1]) =!= {}) &]] Out[31]= "CatBasicAlgebraNoneTMExpandTMFactor" Also, we might have went into another direction, and calulate all positions first and then Map over those positions: In[32]:= Module[{aString = X, posns = StringPosition[X, " "]}, ((aString = StringDrop[aString, #]) &) /@ Reverse[posns]; aString] Out[32]= "CatBasicAlgebraNoneTMExpandTMFactor" (We already have seen that, In[43]), but again looking close at the structure we see this is a candidate for Fold with aString becoming #1, the positions #2: In[33]:= Module[{ posns = StringPosition[X, " "]}, Fold[(StringDrop[#1, #2] &), X, Reverse[posns]]] Out[33]= "CatBasicAlgebraNoneTMExpandTMFactor" The posns are use only once, such we arrive at: In[21]:= Fold[StringDrop[#1, #2] &, X, Reverse[StringPosition[X, " "]]] Out[21]= "CatBasicAlgebraNoneTMExpandTMFactor" Again, we might be content with that. But no way! Before doing any coding, we should have had a look into Help, searching for a direct solution. So scan Programming > String Manipulation to find In[20]:= StringReplace[X, " " -> ""] Out[20]= "CatBasicAlgebraNoneTMExpandTMFactor" -- Hartmut Wolf