Skip to main content
Replaced n with _, as pointed out by @KevinMeredith
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' n_ []     = ([], [])
splitAt' n (y:ys)
  | n < 0     = ([], (y:ys))
  | otherwise = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' n []     = ([], [])
splitAt' n (y:ys)
  | n < 0     = ([], (y:ys))
  | otherwise = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' _ []     = ([], [])
splitAt' n (y:ys)
  | n < 0     = ([], (y:ys))
  | otherwise = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys
Corrected handling of negative n, as pointed out by @KevinMeredith
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

The standard splitAt function produces an error when n is negative. Your splitAt' is lenient in that regard.

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' n []     = ([], [])
splitAt' n (y:ys)
  | n < 0     = ([], (y:ys))
  | otherwise = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys

The standard splitAt function produces an error when n is negative. Your splitAt' is lenient in that regard.

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' n []     = ([], [])
splitAt' n (y:ys) = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' n []     = ([], [])
splitAt' n (y:ys)
  | n < 0     = ([], (y:ys))
  | otherwise = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

The standard splitAt function produces an error when n is negative. Your splitAt' is lenient in that regard.

You should be able to implement it without ++.

splitAt' :: Int -> [a] -> ([a], [a])
splitAt' 0 ys     = ([], ys)
splitAt' n []     = ([], [])
splitAt' n (y:ys) = ((y:a), b)
  where (a, b) = splitAt' (n - 1) ys