Here's how I wrote the Haskell splitAt
function:
splitAt' :: Int -> [a] -> ([a], [a])
splitAt' n ys
| n < 0 = ([], ys)
| otherwise = splitAt'' n ys []
where splitAt'' a (x:xs) acc
| a == 0 = (acc, x:xs)
| null xs = (acc ++ [x], [])
| otherwise = splitAt'' (a-1) xs (acc ++ [x])
I don't like I'm using the append (++
) function to add an element to the end of my acc(umulator)
.
But, given the importance of ordering, I'm not sure how to avoid using it.
Please review this code as well.