在haskell。如何将splitList解析为元组

时间:2015-10-31 22:35:53

标签: haskell tuples

splitList n xs:根据提供的索引xs将列表n拆分为两个列表的元组。

示例:

splitList 3 [1..5] ⇒ ([1,2,3], [4,5])
splitList 3 [1..] ⇒ ([1,2,3], [4..])
splitList 9 [] ⇒ ([], [])

我该如何解决?

2 个答案:

答案 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])