Clojure - 在向量中添加数字对

rbb*_*rbb 1 clojure

我有一个矢量[1 2 3 4].有没有办法添加每一对来获得[(+ 1 2) (+ 2 3) (+ 3 4)]

我试过了

(loop [x 0] 
  (when (< x (count y)) 
    (+ (nth y x) (nth y (+ 1 x))) 
    (recur (+ x 1))))
Run Code Online (Sandbox Code Playgroud)

但是我得到了一个出界的错误 IndexOutOfBoundsException clojure.lang.PersistentVector.arrayFor (PersistentVector.java:158)

ama*_*loy 5

(fn [xs]
  (map + xs (rest xs)))
Run Code Online (Sandbox Code Playgroud)