函数是否有等价物

函数是否有等价物

本文介绍了Clojure Core 或 Contrib 中的 Zip 函数是否有等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Clojure中,我想组合两个列表来给出一个对的列表,

In Clojure, I want to combine two lists to give a list of pairs,

> (zip '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))

在 Haskell 或 Ruby 中,该函数称为 zip.实现它并不困难,但我想确保我没有遗漏 Core 或 Contrib 中的某个函数.

In Haskell or Ruby the function is called zip. Implementing it is not difficult, but I wanted to make sure I wasn't missing a function in Core or Contrib.

Core 中有一个 zip 命名空间,但它是 描述为提供对 Zipper 功能技术的访问,这似乎不是我所追求的.

There is a zip namespace in Core, but it is described as providing access to the Zipper functional technique, which does not appear to be what I am after.

Core 中是否有以这种方式组合 2 个或更多列表的等效功能?

Is there an equivalent function for combining 2 or more lists, in this way, in Core?

如果没有,是不是因为有一种惯用的方法使函数变得不需要?

If there is not, is it because there is an idiomatic approach that renders the function unneeded?

推荐答案

(map vector '(1 2 3) '(4 5 6))

做你想做的事:

=> ([1 4] [2 5] [3 6])

Haskell 需要一组 zipWith (zipWith3, zipWith4, ...) 函数,因为它们都需要是特定的输入;特别是,他们接受的输入列表的数量需要固定.(zipzip2zip3、...族可以看作是zipWith族的特化用于元组的常见用例).

Haskell needs a collection of zipWith (zipWith3, zipWith4, ...) functions, because they all need to be of a specific type; in particular, the number of input lists they accept needs to be fixed. (The zip, zip2, zip3, ... family can be regarded as a specialisation of the zipWith family for the common use case of tupling).

相比之下,Clojure 等 Lisps 对可变元函数有很好的支持;map 就是其中之一,可以以类似于 Haskell 的方式用于元组"

In contrast, Clojure and other Lisps have good support for variable arity functions; map is one of them and can be used for "tupling" in a manner similar to Haskell's

zipWith (x y -> (x, y))

在 Clojure 中构建元组"的惯用方法是构建一个短向量,如上所示.

The idiomatic way to build a "tuple" in Clojure is to construct a short vector, as displayed above.

(为了完整起见,请注意具有一些基本扩展的 Haskell 确实允许可变数量函数;但是,使用它们需要对语言有很好的理解,而 vanilla Haskell 98 可能根本不支持它们,因此固定数量函数函数更适合标准库.)

(Just for completeness, note that Haskell with some basic extensions does allow variable arity functions; using them requires a good understanding of the language, though, and the vanilla Haskell 98 probably doesn't support them at all, thus fixed arity functions are preferrable for the standard library.)

这篇关于Clojure Core 或 Contrib 中的 Zip 函数是否有等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:21