Created
January 7, 2020 23:19
-
-
Save Swoorup/ae591e02bac602efbb79cf9dfd596684 to your computer and use it in GitHub Desktop.
Reverse Nodes in k-Group
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
{-# LANGUAGE NumDecimals #-} | |
swapNodes :: Int -> [a] -> [a] | |
swapNodes _ [] = [] | |
swapNodes k z = swapNodes' z [] | |
where | |
swapNodes' [] acc = acc | |
swapNodes' x acc = | |
let (a,b) = splitAt k x | |
firstHalf = if (length a < k) then | |
a | |
else | |
reverse a | |
in | |
swapNodes' b (acc ++ firstHalf) | |
main :: IO () | |
main = do | |
print $ swapNodes 3 [1..1e9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment