MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MyPrependTo

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15979] Re: [mg15921] MyPrependTo
  • From: "Wolf, Hartmut" <hwolf at debis.com>
  • Date: Fri, 19 Feb 1999 03:27:01 -0500
  • Organization: debis Systemhaus
  • References: <199902180433.XAA11530@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Will

Will Self schrieb:
> 
> In trying to create my own version of PrependTo, I ran into some strange
> stuff that I don't understand.  I am calling my function "push".  Please
> look at the session below and explain these mysterious happenings.
> It seems clear that there is more than one symbol e kicking around,
> but I can't make head or tail of it.  See also my next message for
> the larger context.
> 
> In[2]:=
> push[someStack_, elt_] := (someStack = Prepend[someStack, elt];)
> 
> In[3]:=
> e=stack["expression"]
> Out[3]=
> stack["expression"]
> 
> In[4]:=
> push[e,1]
> 
> In[5]:=
> e
> Out[5]=
> stack[1,"expression"]
> 
> In[6]:=
> push[e,2]
> 
> In[7]:=
> e
> 
> Out[7]=
> stack[2,1,"expression"]
> 
> In[8]:=
> Rest[e]
> Out[8]=
> stack[2,1,"expression"]    (* WHAT!!!!???? *)
> 
> In[9]:=
> ?e
> "Global`e"
> e = stack["expression"]   (* DOUBLE WHAT!!!!???? *)
> 
> Will Self

You will understand, if you carefully track down, what you really did.

The basic misunderstanding probably comes from the evaluation of push[e,
1] *after* you had given a value to e! Executing

> push[e,1]

means executing

  Evaluate[e] = Prepend[Evaluate[e], 1]

which effectively defines a *rule* for stack !!

Repeating

> push[e,2]

defines *another* rule for stack, and everything else is a consequence
of evaluating those rules!!

Will, I have sent you a nb with explanations too detailed to give here.
(Whoever wants to have it, be free to request from
[mailto:hwolf at debis.com])

For a nice little tool use

allValues={{"attributes:",Attributes[#]},
{"default values:",DefaultValues[#]},
{"down values:",DownValues[#]},
{"format values:",FormatValues[#]},
{"messages:",Messages[#]},
{"N values:",NValues[#]},
{"options:", Options[#]},
{"own values:", OwnValues[#]},
{"up values:",UpValues[#]}}&;

allValues[Unevaluated[e]]//TableForm

To come close to what I suppose you intended to do, I suggest:

makeStack::nosymbol = "`1` is not a symbol";

makeStack[x_] := Message[makeStack::nosymbol, x]

makeStack[ss_Symbol] := 
 (Module[{stack},
     ss[push] := (stack = Prepend[stack, #];)&;
     ss[pop] := 
       Block[{s}, 
          If[Length[stack]>0,s=First[stack]; stack=Rest[stack]; s, 
            Print["stack empty"] ]];
     ss[clear] := (stack = {};); 
     Print["new stack ", ss ]
  ]; 
  ss[clear])

And call as:

a=.;makeStack[a]

---your's Hartmut

Hartmut Wolf, debis Systemhaus, Darmstadt, Germany
=======[mailto:hwolf at debis.com]=======



  • References:
  • Prev by Date: Re: MyPrependTo
  • Next by Date: Re: MyPrependTo
  • Previous by thread: MyPrependTo
  • Next by thread: Re: MyPrependTo