Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Making a stack.
Replies: 3   Last Post: Oct 24, 2012 3:31 AM

 Messages: [ Previous | Next ]
 Brentt Posts: 60 Registered: 9/24/11
Re: Making a stack.
Posted: Oct 24, 2012 3:31 AM

Thanks guys.

I ended up figuring out the issue was with evaluation order and figuring
out how use hold first for functions which Set an argument. I found some
examples in one of the "applications" sections of the docs implementing
just this sort of semantics.

I always get tripped up on evaluation order but then it seems obvious once
I realize what is going on. I probably should give the advanced sections of
documentation. I've perused those portions of the docs but it tends not to
stick until the issues actually come up.

On Mon, Oct 22, 2012 at 9:56 PM, Bob Hanlon <hanlonr357@gmail.com> wrote:

> SetAttributes[enqueue, HoldRest]
>
> enqueue[x_, queue_Symbol] :=
> AppendTo[queue, x], queue = {x}];
>
> enqueue[3, q1]
>
> {3}
>
> x = 5;
>
> enqueue[x, q1]
>
> {3, 5}
>
> Or if you want to require manual creation of queue
>
> enqueue[x_, queue_Symbol] :=
> AppendTo[queue, x];
>
> q2 = {};
>
> enqueue[7, q2]
>
> {7}
>
> enqueue[x, q2]
>
> {7, 5}
>
>
> Bob Hanlon
>
>
> On Mon, Oct 22, 2012 at 2:03 AM, Brentt <brenttnewman@gmail.com> wrote:

> >
> > Hi, I'm trying to make stack functions like enqueue and dequeue to
> > demonstrate algorithms. I know enqueue would do pretty much the same

> thing
> > as AppendTo, but I want the arguments reversed and it to be called
> > "enqueue". I figured this would work:
> >
> > enqueue[x_, queue_] := AppendTo[queue, x];
> >
> > But it spits out red. I tried to tinker with the evaluation order but to

> no
> > avail. Is anyway to make this work? I want the functions to work like
> they
> > would in a procedural language (I'm not using a procedural language
> because
> > I want to accompany the algorithms with visualizations---i.e. it's for a
> > demonstration project). I know I can just use Set when I want to que
> > something, but I was hoping to figure out a way to make the code look

> like
> > procedural code, and I'm guessing Mathematica is flexible enough to do
> > that.
> >
> > Any ideas?

>
>

Date Subject Author
10/23/12 Simons, F.H.
10/23/12 Bob Hanlon
10/24/12 Brentt