splitList n xs
:基于提供的索引n
将列表xs
拆分为两个列表的元组。在haskell中。如何将拆分列表解析为元组
例子:
splitList 3 [1..5] ⇒ ([1,2,3], [4,5])
splitList 3 [1..] ⇒ ([1,2,3], [4..])
splitList 9 [] ⇒ ([], [])
如何解决这个问题呢?
splitList n xs
:基于提供的索引n
将列表xs
拆分为两个列表的元组。在haskell中。如何将拆分列表解析为元组
例子:
splitList 3 [1..5] ⇒ ([1,2,3], [4,5])
splitList 3 [1..] ⇒ ([1,2,3], [4..])
splitList 9 [] ⇒ ([], [])
如何解决这个问题呢?
这是一个可能的解决方案:
splitList :: Int -> [a] -> ([a], [a])
splitList _ [] = ([], [])
splitList n xs = (take n xs, drop n xs)
splitAt :: Int -> [a] -> ([a], [a])
λ> splitAt 3 [1..5]
([1,2,3],[4,5])
换句话说......'splitAt'已经存在 - 使用它! :) – vikingsteve
你要找的'Data.List.splitAt'。 – Bergi
为什么你在这里破坏你自己的问题? – Carsten