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

查看:412
本文介绍了什么是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". Obviously "zip" is a special case of "zipWith":

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

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

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