MathGroup Archive 1999

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

Search the Archive

Re: implementing a stack

  • To: mathgroup at
  • Subject: [mg16052] Re: [mg15922] implementing a stack
  • From: "Allan Hayes" <hay at>
  • Date: Mon, 22 Feb 1999 01:44:32 -0500
  • References: <7alphl$>
  • Sender: owner-wri-mathgroup at

Clemens Frey wrote in message <7alphl$no8 at>...
>Hi Will!
>Although Bob has already posted a solution, here is mine. I strongly use
>the Hold-Attribute to keep the function arguments from being evaluated.
>But as I want to implement a little own data type, I want to test if the
>right stuff is being given to the push/pop functions, so I have to use a
>conditioned pattern together with a ReleaseHold just to evaluate the
>argument when I want. Be careful to define the pop fct exactly in the
>given order, since Mathematica cannot decide which condition is more
>here. The first pop-rule just returns Null if an empty stack is given to
>it. Instead of the very inefficient Prepend-fct I use a Flatten-construct.
>init := TStack[];
>push[ s_Symbol/;Head[ReleaseHold[s]]==TStack, elem_ ] :=
> (s = Flatten[TStack[elem,s]];)
>pop[ s_Symbol /;ReleaseHold[s]==TStack[] ] = Null;
>pop[ s_Symbol /;Head[ReleaseHold[s]]==TStack ] :=
> {First[s],s = Rest[s]} [[1]]
>Now you can use these definitions:
>s = init;
>pop[s] //FullForm
>Clemens Frey
>Doctoral Student at the
>Department of Mathematics/BITOEK
>University of Bayreuth(Germany)
>clemens.frey at


We do not need to release holding in conditions and tests - it is done

f[y_/;Head[y]==H]:= 3;
z = H[8];



f[y_?(Head[#]==H&)]:= 4



Also,  ReleaseHold is for cancelling the hold functions (Hold, HoldComplete
..), not the hold attributes (HoldFirst, ...). The latter are cancelled by

u = 6;






Allan Hayes
Mathematica Training and Consulting
Leicester UK
hay at
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

  • Prev by Date: Re: Early out with Map?
  • Next by Date: Mathlink and JAVA
  • Previous by thread: Re: implementing a stack
  • Next by thread: Pure Functions in rules