Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #12144 > unrolled thread
| Started by | Fred Simons <f.h.simons@tue.nl> |
|---|---|
| First post | 2012-10-23 05:02 +0000 |
| Last post | 2012-10-23 05:02 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Making a stack. Fred Simons <f.h.simons@tue.nl> - 2012-10-23 05:02 +0000
| From | Fred Simons <f.h.simons@tue.nl> |
|---|---|
| Date | 2012-10-23 05:02 +0000 |
| Subject | Re: Making a stack. |
| Message-ID | <k658cs$h68$1@smc.vnet.net> |
The function AppendTo has attribute HoldFirst; you want to pass the name
queue into the body and not the value. Therefore your function enqueue
must have attribute HoldRest:
In[3]:= Attributes[enqueue] = {HoldRest};
enqueue[x_,queue_]:=AppendTo[queue,x];
In[5]:= queue={1,2,3}; enqueue[4, queue]
Out[6]= {1,2,3,4}
Kind regards,
Fred Simons
Eindhoven University of Technology
Op 22-10-2012 8:03, Brentt schreef:
> 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?
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web