Clojure - 如何在向量中添加连续对? [英] Clojure-How to add successive pairs in vector?
本文介绍了Clojure - 如何在向量中添加连续对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试编写在向量中添加连续对的递归函数。
Trying to write a recursive function that adds successive pairs in a vector.
[1 2 3 4] => [3 5 7]
这是我目前的:
(def tmp [ 1 2 3 4])
user> (map #(+ (second %) (first %)) (partition-all 2 tmp ))
这是错误的,因为它只添加对&不是连续对。我得到 [3 7]
而不是 [3 5 7]
This is wrong as it adds just the pairs & not successive pairs. I get[3 7]
instead of [3 5 7]
推荐答案
这是另一种可能的解决方案:
Here is another possible solution:
(def tmp [1 2 3 4])
(map + tmp (rest tmp))
这篇关于Clojure - 如何在向量中添加连续对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文