RE: syntax error
- To: mathgroup at smc.vnet.net
- Subject: [mg37905] RE: [mg37901] syntax error
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 19 Nov 2002 03:51:16 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: David Afonso [mailto:david-afonso at iol.pt] >Sent: Monday, November 18, 2002 6:53 AM >To: mathgroup at smc.vnet.net >Subject: [mg37905] [mg37901] syntax error > > >there must be a syntax error.. > >Could you help me find it? > >f = Function[pp, > Module[{r, aux}, > r = new; > aux = pp; > While[emptyQ[aux] == False, > While[emptyQ[top[aux]] == False, > r = push[top[top[aux]], r]; > aux = push[pop[top[aux]] , pop[aux]] > ]; > aux = pop[aux] > ] > r]] > > > >it gives back something estrange when applied to a list of >lists. Like: {3Null,7Null...etc} > > (1) Not a syntax error, but most probably (at least!) a missing semicolon after the outermost While (before you return r). This is kind of standard typo whenever results of this sort show up. (2) It is, however, difficult to answer a question of this sort, I mean, without being told what new etc. is. So the answer certainly must be "who knows?" (3) Such, BTW, you leave out a chance for better advice! Now, if I make a guess and construe stacks as lists, and make up the most simple implementation of push, pop etc. I get with your code: In[29]:= pp = Partition[Range[9], 3] Out[29]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} In[30]:= f[pp] Out[30]= {9 Null, 8 Null, 7 Null, 6 Null, 5 Null, 4 Null, 3 Null, 2 Null, Null} Having added that semicolon mentioned it becomes In[33]:= f[pp] Out[33]= {9, 8, 7, 6, 5, 4, 3, 2, 1} Perhaps you will have been lucky too. In[34]:= Quit[] However I'd not implement stacks that way, and rather code your function more like this: f[ss : stack[__stack]] := Module[{r, s = ss, t}, r = stack[new]; While[! stackEmptyQ[s], t = top[s]; pop[s]; While[! stackEmptyQ[t], push[r, top[t]]; pop[t]] ]; r] Let it run: In[45]:= pp = stack[new] Out[45]= \[SkeletonIndicator]empty stack\[SkeletonIndicator] In[46]:= With[{s = Unique[p]}, s = stack[new]; Scan[push[s, #] &, #]; s] & /@ Partition[Range[9, 1, -1], 3] Out[46]= {\[SkeletonIndicator]stack\[SkeletonIndicator], \[SkeletonIndicator]stack\ \[SkeletonIndicator], \[SkeletonIndicator]stack\[SkeletonIndicator]} In[47]:= Scan[push[pp, #] &, %] In[48]:= Apply[List, pp, {0, 1}] Out[48]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} In[49]:= f[pp] Out[49]= \[SkeletonIndicator]stack\[SkeletonIndicator] In[50]:= List @@ % Out[50]= {9, 8, 7, 6, 5, 4, 3, 2, 1} In[51]:= Apply[List, pp, {0, 1}] Out[51]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} This is consistent with your function, however I had to create and initialize stack objects using their methods. You might like to guess what this implementation of push, pop etc. was. -- Hartmut Wolf