[erlang-questions] Using a queue

Richard A. O'Keefe ok@REDACTED
Tue Aug 26 05:19:39 CEST 2008


On 26 Aug 2008, at 12:53 pm, yoursurrogategod@REDACTED wrote:
> 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}]}

Well, yes.  Of course.
queue:out/1 DOES NOT RETURN A QUEUE.

Here's the documentation:

out(Q1) -> Result

     Types:

         Result = {{value, Item}, Q2} | {empty, Q1}
         Q1 = Q2 = queue()

     Removes the item at the front of queue Q1.
     Returns the tuple {{value, Item}, Q2},
     where Item is the item removed and Q2 is the resulting queue.
     If Q1 is empty, the tuple {empty, Q1} is returned.



So you use it like

     case queue:out(Q5)
       of {empty,Q6} ->
          what to do if Q5 is empty
        ; {{value,Item},Q6} ->
          what to do if Q5 had a first element Item
          and remaining elements Q6
     end

Or, if you know that Q5 is not empty (and are willing to have
it treated as an error if it isn't), as

     {{value,Elem6},Q6} = queue:out(Q5).

If, as seems to be the case in your example, you don't
actually want Elem6, but simply to remove the first
element and throw it away, there is a function in the
queue interface that does exactly what you want:

	Q6 = queue:tail(Q5)

(It's in the documentation below 'Okasaki API'.)
You could also queue

	Q6 = queue:drop(Q5)

As far as I can see there is no difference between
these functions; use whatever you feel is clearer.






More information about the erlang-questions mailing list