【问题标题】:Getting the first and second element from a 3 tuple in a list - Haskell从列表中的 3 个元组中获取第一个和第二个元素 - Haskell
【发布时间】:2014-11-20 23:47:08
【问题描述】:

所以我将一个 3 元组列表传递给该函数,并希望返回该元组的第一个和第三个元素,为什么这里的代码不起作用?

remove :: (a, b, c) -> (a,c)
remove (x, _, y) = (x,y)

我得到的错误是

*** Expression     : remove (sortScore b h)
*** Term           : sortScore b h
*** Type           : [(Val,Int,End)]
*** Does not match : (a,b,c)

【问题讨论】:

    标签: haskell tuples


    【解决方案1】:

    sortScore 正在返回一个三元组列表,但 remove 只接受一个。

    您可以使用mapremove 应用于从sortScore 返回的每个元素

    map remove (sortScore b h)
    

    【讨论】:

      【解决方案2】:

      您需要使用map 来转换列表sortScore b h 中的每个元组:

      map remove $ sortScore b h
      

      您不能仅将remove 应用于sortScore b h,因为最后一个是列表,但remove 适用于元组。

      【讨论】:

      • 好吧,我是想说第一和第三,让我编辑一下
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 2013-04-29
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2016-07-27
      相关资源
      最近更新 更多