【问题标题】:Zip list with list of a list in Haskell邮政编码列表和 Haskell 中的列表列表
【发布时间】:2018-03-01 15:43:36
【问题描述】:

我想用[[String]] 压缩[Int],这样我得到[[(Int, String)]]。 一个例子:

[1,2,3] and
[[a,b,c],[d,e,f]] becomes
[[(1,a),(2,b),(3,c)],[(1,d),(2,e),(3,f)]]

我怎么能这样做?我用地图尝试了一些东西,但没有成功。

【问题讨论】:

    标签: list dictionary haskell zip tuples


    【解决方案1】:

    您需要映射字符串列表并将整数列表与每个字符串一起压缩。

    Prelude> myzip ints strings = map (zip ints) strings
    Prelude> myzip [1,2,3] [["a", "b", "c"], ["d", "e", "f"]]
    [[(1,"a"),(2,"b"),(3,"c")],[(1,"d"),(2,"e"),(3,"f")]]
    

    使用柯里化,这可以缩短为

    myzip ints = map (zip ints)
    

    然后可以使用组合将其缩短为以下内容:

    myzip = map . zip
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2016-03-10
      相关资源
      最近更新 更多