Queue/Definition: Difference between revisions
Content deleted Content added
replace Template:BoxImage with wiki markup |
Added Erlang. |
||
Line 691: | Line 691: | ||
return [reader, writer] |
return [reader, writer] |
||
}</lang> |
}</lang> |
||
=={{header|Erlang}}== |
|||
The standard way to manage fifo in functional programming is to use a pair of list for the fifo queue, one is the input, the other is the output. When the output is empty just take the input list and reverse it. |
|||
<lang Erlang> |
|||
-module(fifo). |
|||
-export([new/0, push/2, pop/1, empty/1]). |
|||
new() -> {fifo, [], []}. |
|||
push({fifo, In, Out}, X) -> {fifo, [X|In], Out}. |
|||
pop({fifo, [], []}) -> erlang:error('empty fifo'); |
|||
pop({fifo, In, []}) -> pop({fifo, [], lists:reverse(In)}); |
|||
pop({fifo, In, [H|T]}) -> {H, {fifo, In, T}}. |
|||
empty({fifo, [], []}) -> true; |
|||
empty({fifo, _, _}) -> false. |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |