Skip to content

Instantly share code, notes, and snippets.

@Swoorup
Created January 7, 2020 23:19
Show Gist options
  • Save Swoorup/ae591e02bac602efbb79cf9dfd596684 to your computer and use it in GitHub Desktop.
Save Swoorup/ae591e02bac602efbb79cf9dfd596684 to your computer and use it in GitHub Desktop.
Reverse Nodes in k-Group
{-# 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