Last active
June 5, 2016 05:27
-
-
Save oboff/566a4cbd586d91163a0c1d8b2f5533a3 to your computer and use it in GitHub Desktop.
rotate list around list of Nth elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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