Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

Re: implementing a stack

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16052] Re: [mg15922] implementing a stack
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 22 Feb 1999 01:44:32 -0500
  • References: <7alphl$no8@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Clemens Frey wrote in message <7alphl$no8 at smc.vnet.net>...
>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
specific
>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[];
>
>SetAttributes[push,HoldFirst];
>push[ s_Symbol/;Head[ReleaseHold[s]]==TStack, elem_ ] :=
> (s = Flatten[TStack[elem,s]];)
>
>SetAttributes[pop,HoldAll];
>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;
>
>push[s,2]
>
>push[s,xx]
>
>pop[s]
>xx
>
>pop[s]
>2
>
>pop[s] //FullForm
>Null
>
>Bye
>Clemens
>
>------------------------------------------------------------
>Clemens Frey
>Doctoral Student at the
>Department of Mathematics/BITOEK
>University of Bayreuth(Germany)
>
>clemens.frey at uni-bayreuth.de
>http://www.bitoek.uni-bayreuth.de/~Clemens.Frey
>------------------------------------------------------------


Clemens:

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

ClearAll[f]
SetAttributes[f,HoldFirst]
f[y_/;Head[y]==H]:= 3;
z = H[8];

{f[y],f[8]}

     {3,f[8]}

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

{f[z],f[p]}

    {4,f[p]}



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

u = 6;
Attributes[Hold]
{HoldAll,Protected}

{ReleaseHold[Hold[u]],Evaluate[Hold[u]]}

    {6,Hold[u]}

{Hold[ReleaseHold[u]],Hold[Evaluate[u]]}

    {Hold[ReleaseHold[u]],Hold[6]}

Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
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