奇怪的clojure地图功能 [英] weirdness in clojure map function

查看:92
本文介绍了奇怪的clojure地图功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

clojure中map的第一个奇怪的事情是在以下代码片段:

the first strange thing about map in clojure is in following snippet:

(apply map list '((1 a) (2 b) (3 c)))

结果令我惊讶:

((1 2 3) (a b c))

任何人都可以解释它的工作原理。

Anyone could explain how it works?

推荐答案

'(yz))等效于(fxyz),因此您的代码等效于

(apply f x '(y z)) is equivalent to (f x y z), so your code is equivalent to (map list '(1 a) '(2 b) '(3 c)).

当使用多个列表调用时, map 并行迭代列表,并为每个元素从每个列表的一个元素调用给定的函数(即,结果列表的第一个元素是调用每个列表的第一个元素作为其函数的结果第二个是第二个元素的结果等)。

When called with multiple lists, map iterates the lists in parallel and calls the given function with one element from each list for each element (i.e. the first element of the result list is the result of calling the function with the first element of each list as its arguments, the second is the result for the second elements etc.).

因此(map list'(1a)'(2b) '(3 c))首先调用 list 与列表的第一个元素数字)作为参数,然后与第二个元素(字母)。所以你得到((list 1 2 3)(list'a'b'c))

So (map list '(1 a) '(2 b) '(3 c)) first calls list with the first elements of the lists (i.e. the numbers) as arguments and then with the second elements (the letters). So you get ((list 1 2 3) (list 'a 'b 'c)).

这篇关于奇怪的clojure地图功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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