Skip to content

Instantly share code, notes, and snippets.

@oboff
Last active June 5, 2016 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oboff/566a4cbd586d91163a0c1d8b2f5533a3 to your computer and use it in GitHub Desktop.
Save oboff/566a4cbd586d91163a0c1d8b2f5533a3 to your computer and use it in GitHub Desktop.
rotate list around list of Nth elements
-module(rotate).
-export([rotate/2]).
rotate(N, List) ->
if
is_number(N) ->
rot([N], List);
true -> rot(N, List)
end.
rot([], List) -> List;
rot([0|Ns], List) -> rot(Ns, List);
rot([1|Ns], List) -> rot(Ns, List);
rot([N|Ns], List) ->
if
N > length(List) -> rot(Ns, List);
N < 0 -> rot(Ns, List);
N >= 0 ->
{List1, List2} = lists:split(N-1, List),
rot(Ns, List2 ++ List1)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment