代码之家  ›  专栏  ›  技术社区  ›  Robert Gould

什么是zip(函数式编程?)

  •  49
  • Robert Gould  · 技术社区  · 16 年前

    我最近看到了一些Clojure或Scala(抱歉,我不熟悉它们),它们确实出现在一个列表或类似的东西上。什么是拉链,它从哪里来的?

    5 回复  |  直到 9 年前
        1
  •  78
  •   skylize Pavel Minaev    8 年前

    zipWith (+) [1, 2, 3] [4, 5, 6]
    

    输出:

    [5, 7, 9]
    

    zip

    输入:

    zip [1, 2, 3] [4, 5, 6]
    

    输出:

    [(1, 4), (2, 5), (3, 6)]
    

    zip xs ys = zipWith (\x y -> (xs, ys)) xs ys 
    
        2
  •  22
  •   drudru    16 年前

    例如,假设你有一个带(1,2,3)的列表,另一个带“一”、“二”、“三”的列表

    scala> List(1,2,3).zip(List("one","two","three"))
    res2: List[(Int, java.lang.String)] = List((1,one), (2,two), (3,three))
    

        3
  •  10
  •   bseibold    16 年前

    zip xs ys = zipWith xs ys (\x y -> (xs, ys))
    

    zip xs ys = zipWith (\x y -> (x,y)) xs ys
    

    zip = zipWith (\x y -> (x,y))
    
        4
  •  7
  •   Geo    16 年前

    你可以在Python中使用以下代码:

    
    >>> a = [1,2]
    >>> b = [3,4]
    >>> zip(a,b)
    [(1,3),(2,4)]
    
        5
  •  6
  •   Mehrdad Afshari    16 年前

    let x = [1;2]
    let y = ["hello"; "world"]
    let z = Seq.zip x y
    

    价值 z

    [(1, "hello"); (2, "world")]