[erlang-questions] Using a queue

Robert Virding <>
Tue Aug 26 03:05:02 CEST 2008


2008/8/26  <>

> Ok, based on your suggestions, here is what I have.
>
> Eshell V5.5.5  (abort with ^G)
> 1> Q1 = queue:new().
> {[],[]}
> 2> Q2 = queue:in("foo", Q1).
> {["foo"],[]}
> 3> Q3 = queue:in("bar", Q2).
> {["bar"],["foo"]}
> 4> Q4 = queue:in("blah", Q3).
> {["blah","bar"],["foo"]}
> 5> Q5 = queue:in("stuff", Q4).
> {["stuff","blah","bar"],["foo"]}
> 6> Q6 = queue:out(Q5).
> {{value,"foo"},{["stuff","blah"],["bar"]}}
> 7> Q7 = queue:in("stuff1", Q6).
>
> =ERROR REPORT==== 25-Aug-2008::20:23:40 ===
> Error in process <0.31.0> with exit value: {badarg,[{queue,in,
> ["stuff1",{{value,"foo"},{["stuff","blah"],["bar"]}}]},
> {erl_eval,do_apply,5},{erl_eval,expr,5},{shell,exprs,6},
> {shell,eval_loop,3}]}
>
> ** exited: {badarg,[{queue,in,
>                           ["stuff1",
>                            {{value,"foo"},{["stuff","blah"],
> ["bar"]}}]},
>                    {erl_eval,do_apply,5},
>                    {erl_eval,expr,5},
>                    {shell,exprs,6},
>                    {shell,eval_loop,3}]} **
>
> For some reason, when I tried to add on to Q7 after removing a value,
> it crashed. One thing that I discovered was when I did > Q6., I got
> the below:
>
> {{value,"foo"},{["stuff","blah"],["bar"]}}
>
> I managed to get at the list that contains stuff and blah (which is a
> little odd).


queue:out returns {{value, Item}, Queue}  or  {empty, Queue}, i.e. it
returns both the value and the remaining queue after the value has been
removed. If there was a value it is returned as {value,Value} otherwise
'empty' is returned.

Returning multiple values in a tuple is common practice in erlang, actually
it is almost the only way to do it.

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080826/a8804fc9/attachment.html>


More information about the erlang-questions mailing list