weirdness in clojure map function

前端 未结 1 690
天涯浪人
天涯浪人 2021-01-11 18:24

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

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

The result is surprising for m

相关标签:
1条回答
  • 2021-01-11 18:46

    (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)).

    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.).

    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)).

    0 讨论(0)
提交回复
热议问题