-
Website
http://blog.valkertown.org/ -
Original page
http://blog.valkertown.org/2009/03/18/writting_circular_lists_in_erlang/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
David Moreno
1 comment · 6 points
-
grimborg
1 comment · 1 points
-
deepspawn
30 comments · 1 points
-
deepsabbath
1 comment · 1 points
-
iphone clone
2 comments · 1 points
-
-
Popular Threads
To move forward do this
forward({[H|T],B}) -> {T, [H|B]};
forward({[], B}) -> forward({reverse(B),[]})
bawards is the opposite
the other solution in the above comments.
%% append the Push element to the beginning of the list while dropping the last element
circular(List, Push) ->
[Push] ++ drop_last(List).
%% drop the last element from the list
drop_last([_]) ->
[];
drop_last([H|T]) ->
[H | drop_last(T)].
[Push] ++ drop_last(List)
Wouldn't it be better written as:
[ Push | drop_last(List)]
Anyway, ++ is discouraged since it makes a copy of the left operator and with long lists it would be highly inefficient, in this case it wouldn't be much of a problem but I still like more how the second one looks.
Thank you a lot for this idea, I'm guessing it's probably the best way to write it on erlang.