[erlang-questions] Using a queue

Colm Dougan <>
Tue Aug 26 02:17:39 CEST 2008


On Tue, Aug 26, 2008 at 12:53 AM, 
<> wrote:
> I'm trying to use the queue module in Erlang by trying it out in the
> console. Here is what I tried and here are the results:
>
> 34> T1 = queue:new().
> {[],[]}
> 35> queue:in("foo", T1).
> {["foo"],[]}
> 36> queue:in("bar", T1).
> {["bar"],[]}

>
> What confuses me here is that I expected foo and bar to be somehow
> next to one another after inserting bar. Why is it that it's only foo?

You are performing all operations on the original queue, T1.  You need
to assign the return value of each queue operation to something as
this is the new queue.

e.g. :

Eshell V5.6.1  (abort with ^G)
1> Q1 = queue:new().
{[],[]}
2> Q2 = queue:in("foo", Q1).
{["foo"],[]}
3> Q3 = queue:in("bar", Q2).
{["bar"],["foo"]}

>
> Also, I have another question. I would like a linked list in Erlang
> (similar to the STL list class in C++), is there something like that?
> It would be also nice if the individual elements in the said list
> could be accessed in a similar fashion as an array :) (don't know if
> this is possible).

As far as I know, the closest thing to the stl list in erlang is the
built in list type.  You can use lists:nth(I, List) to emulate array
random access, or just use the 'array' API.

Colm



More information about the erlang-questions mailing list