splitList n xs
:根据提供的索引xs
将列表n
拆分为两个列表的元组。
示例:
splitList 3 [1..5] ⇒ ([1,2,3], [4,5])
splitList 3 [1..] ⇒ ([1,2,3], [4..])
splitList 9 [] ⇒ ([], [])
我该如何解决?
答案 0 :(得分:2)
这是一种可能的解决方案:
splitList :: Int -> [a] -> ([a], [a])
splitList _ [] = ([], [])
splitList n xs = (take n xs, drop n xs)
答案 1 :(得分:0)
splitAt :: Int -> [a] -> ([a], [a])
λ> splitAt 3 [1..5]
([1,2,3],[4,5])