[erlang-questions] Using a queue

Richard A. O'Keefe ok@REDACTED
Tue Aug 26 02:15:26 CEST 2008

On 26 Aug 2008, at 11:53 am, yoursurrogategod@REDACTED 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"],[]}

This says "Make a new value with "foo" added to T1."
That's what happened.  IT DID NOT IN ANY WAY CHANGE T1.
> 36> queue:in("bar", T1).

This says "Make a new value with "bar" added to the old,
original, completely unchanged T1."
And that's what it did.
> {["bar"],[]}

T1 was, and must at all times necessarily remain, empty.

> 37> queue:out(T1).
> {empty,{[],[]}}

Since T1 was empty, and since variables and values in Erlang
do not and cannot change, T1 is still empty.

> What confuses me here is that I expected foo and bar to be somehow
> next to one another after inserting bar.

What you _meant_ to do was something like

	Q0 = queue:new(),
	Q1 = queue:in("foo", Q0),
	Q2 = queue:in("bar", Q1),

Erlang data structures are *values*; you can no more
*change* a queue value than you can change the number 1.

> 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?

The STL list class offers doubly linked lists, which are a MAJOR
pain to deal with.

Erlang has lists as primitive built-in types:
    [] is an empty list
    [H|T] is a list with head H and tail T
    [A,B,C] is an abbreviation for [A|[B|[C|[]]]]
This is all in the old Erlang book and in Joe Armstrong's new book.
Both of them are good value.

> 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).

Why do you want your language to lie to you?
Lists aren't arrays and don't act like arrays and if you
try to use them like arrays you are likely to end up
with really bad performance problems.

More information about the erlang-questions mailing list