【问题标题】:In haskell. how to solve splitList into a tuple在哈斯克尔。如何将 splitList 解决为元组
【发布时间】:2015-10-31 22:35:52
【问题描述】:

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

例子:

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

我该如何解决?

【问题讨论】:

  • 您正在寻找Data.List.splitAt
  • 你为什么要在这里破坏你自己的问题?

标签: haskell tuples


【解决方案1】:

这是一种可能的解决方案:

splitList :: Int -> [a] -> ([a], [a])
splitList _ [] = ([], [])
splitList n xs = (take n xs, drop n xs)

【讨论】:

    【解决方案2】:
    splitAt :: Int -> [a] -> ([a], [a])
    
    λ> splitAt 3 [1..5]
    ([1,2,3],[4,5])
    

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多