什么是 zip(函数式编程?) [英] What is zip (functional programming?)

查看:27
本文介绍了什么是 zip(函数式编程?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到了一些 Clojure 或 Scala(抱歉我不熟悉它们),它们确实压缩了一个列表或类似的东西.什么是 zip,它来自哪里?

I recently saw some Clojure or Scala (sorry I'm not familiar with them) and they did zip on a list or something like that. What is zip and where did it come from ?

推荐答案

Zip 是当您采用两个输入序列,并生成一个输出序列,其中使用某个函数组合来自同一位置的输入序列的每两个元素.Haskell 中的一个例子:

Zip is when you take two input sequences, and produce an output sequence in which every two elements from input sequences at the same position are combined using some function. An example in Haskell:

输入:

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

输出:

[5, 7, 9]

以上是一个更通用的定义;有时,zip 特指将元素组合为元组.例如.再次在 Haskell 中:

The above is a more generic definition; sometimes, zip specifically refers to combining elements as tuples. E.g. in Haskell again:

输入:

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

输出:

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

更通用的版本称为zip with".您可以将zip"视为zipWith"的特例:

And the more generic version is called "zip with". You may consider "zip" as a special case of "zipWith":

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

这篇关于什么是 zip(函数式编程?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆