[erlang-questions] Circular list

Matt Williamson <>
Fri Aug 22 20:39:43 CEST 2008


This should at least be better than a double reverse:

1> L = ["A", "B", "C", "D", "E"].
["A","B","C","D","E"]
2> [_|L2] = L.
["A","B","C","D","E"]
3> L2 ++ ["F"].
["B","C","D","E","F"]


2008/8/22 Hynek Vychodil <>

> Look at queue module. Copy is performed, but only once if you work only one
> way.
>
>
> On Fri, Aug 22, 2008 at 3:34 PM, Colm Dougan <>wrote:
>
>> Hi,
>>
>> If I have a list of a fixed size (say 10,000 elements).  What is the
>> most efficient way to add a new item to the list and force out the old
>> first element.  By most efficient I mostly mean without having to copy
>> the entire list each time, if that is even possible.
>>
>> I'm currently doing a double reverse trick, e.g. :
>>
>> Eshell V5.6.3  (abort with ^G)
>> 1> L1 = ["A", "B", "C", "D", "E"].
>> ["A","B","C","D","E"]
>> 2> [_ | L2] = L1.
>> ["A","B","C","D","E"]
>> 3> lists:reverse(["F" | lists:reverse(L2)]).
>> ["B","C","D","E","F"]
>>
>> Is there a more efficient way?  I tried a benchmark and the reverse
>> seems more costly than I expected.
>>
>> Colm
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> --
> --Hynek (Pichi) Vychodil
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080822/75dc2092/attachment.html>


More information about the erlang-questions mailing list