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]
几乎卡住了,这就是我目前所拥有的:
Pretty much stuck and this is what I have currently:
(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 and 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屋!
查看全文