Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
19 lines (16 sloc) 463 Bytes
-- P19 Rotate a list N places to the left.
-- Using "splitAt"
f1 :: Int -> [a] -> [a]
f1 _ [] = []
f1 n xs = let (a, b) = splitAt (mod n $ length xs) xs in b ++ a
-- Without using "let"
f2 :: Int -> [a] -> [a]
f2 _ [] = []
f2 n xs = uncurry (flip (++)) $ splitAt (mod n $ length xs) xs
-- Tail recursion
f3 :: Int -> [a] -> [a]
f3 _ [] = []
f3 0 xs = xs
f3 n xs
| n > 0 = f3 (pred n) (tail xs ++ [head xs])
| otherwise = f3 (succ n) (last xs : init xs)