:: [a] -> Int -> ([a], [a]) -package:general-games

splitAt n xs returns a tuple where first element is xs prefix of length n and second element is the remainder of the list: splitAt is an instance of the more general genericSplitAt, in which n may be of any integral type.

Laziness

It is equivalent to (take n xs, drop n xs) unless n is _|_: splitAt _|_ xs = _|_, not (_|_, _|_)). The first component of the tuple is produced lazily:
>>> fst (splitAt 0 undefined)
[]
>>> take 1 (fst (splitAt 10 (1 : undefined)))
[1]

Examples

>>> splitAt 6 "Hello World!"
("Hello ","World!")
>>> splitAt 3 [1,2,3,4,5]
([1,2,3],[4,5])
>>> splitAt 1 [1,2,3]
([1],[2,3])
>>> splitAt 3 [1,2,3]
([1,2,3],[])
>>> splitAt 4 [1,2,3]
([1,2,3],[])
>>> splitAt 0 [1,2,3]
([],[1,2,3])
>>> splitAt (-1) [1,2,3]
([],[1,2,3])
splitAtExact n xs =
| n >= 0 && n <= length xs = splitAt n xs
| otherwise                = error "some message"
splitAtEnd n xs returns a split where the second element tries to contain n elements.
splitAtEnd 3 "hello" == ("he","llo")
splitAtEnd 3 "he"    == ("", "he")
\i xs -> uncurry (++) (splitAt i xs) == xs
\i xs -> splitAtEnd i xs == (dropEnd i xs, takeEnd i xs)
splitAt n xs returns a tuple where first element is xs prefix of length n and second element is the remainder of the list:
>>> splitAt 6 "Hello World!"
("Hello ","World!")

>>> splitAt 3 [1,2,3,4,5]
([1,2,3],[4,5])

>>> splitAt 1 [1,2,3]
([1],[2,3])

>>> splitAt 3 [1,2,3]
([1,2,3],[])

>>> splitAt 4 [1,2,3]
([1,2,3],[])

>>> splitAt 0 [1,2,3]
([],[1,2,3])

>>> splitAt (-1) [1,2,3]
([],[1,2,3])
It is equivalent to (take n xs, drop n xs) when n is not _|_ (splitAt _|_ xs = _|_). splitAt is an instance of the more general genericSplitAt, in which n may be of any integral type.
splitAtRev n xs == (dropRev n xs, takeRev n xs).
\n xs -> splitAtRev n (xs::String) == (dropRev n xs, takeRev n xs)
\n xs -> (xs::String) == uncurry (++) (splitAtRev n xs)
splitAt n xs returns a tuple where first element is xs prefix of length n and second element is the remainder of the list:
splitAt 6 "Hello World!" == ("Hello ","World!")
splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5])
splitAt 1 [1,2,3] == ([1],[2,3])
splitAt 3 [1,2,3] == ([1,2,3],[])
splitAt 4 [1,2,3] == ([1,2,3],[])
splitAt 0 [1,2,3] == ([],[1,2,3])
splitAt (-1) [1,2,3] == ([],[1,2,3])
It is equivalent to (take n xs, drop n xs) when n is not _|_ (splitAt _|_ xs = _|_). splitAt is an instance of the more general genericSplitAt, in which n may be of any integral type.
The genericSplitAt function is an overloaded version of splitAt, which accepts any Integral value as the position at which to split.
O(1) Yield the first n elements paired with the remainder, without copying. Note that splitAt n v is equivalent to (take n v, drop n v), but slightly more efficient.
O(1) Yield the first n elements paired with the remainder without copying. Note that splitAt n v is equivalent to (take n v, drop n v) but slightly more efficient.
Split position must be at most the length of the list, otherwise the end of the first list and the second list are undefined.
Split at the given index. Equivalent to (take n xs, drop n xs) and therefore total.